Hey so i've got this program which takes some input from the user and saves it in a CSV file: Now i want to make an error code so that every time when the message of a user exceeds 140 characters, it tells them that the max ammount of characters is 140. How could i program this?
import tkinter
import csv
import random
from tkinter import *
import datetime
from tkinter import messagebox
window = tkinter.Tk()
window.geometry("1920x1080")
def on_click():
messagebox.showerror("Error", 'Error: Het bericht mag maximaal 140 tekens bevatten')
label = Label(window, text="Click this button to show the message",
font=('Calibri 15 bold'))
label.pack(pady=20)
b = Button(window, text='Click me', command=on_click)
b.pack(pady=20)
def get_data():
global button
Message_data = Message.get()
Name_data = Name.get()
vandaag = datetime.datetime.today()
s = vandaag.strftime("%a %d %b %Y %H:%M")
stations = ['Arnhem', 'Almere', 'Amersfoort', 'Almelo', 'Alkmaar', 'Apeldoorn', 'Assen', 'Amsterdam', 'Boxtel',
'Breda', 'Dordrecht', 'Delft', 'Deventer', 'Enschede', 'Gouda', 'Groningen', 'Den Haag', 'Hengelo',
'Haarlem', 'Helmond', 'Hoorn', 'Heerlen', 'Den Bosch', 'Hilversum', 'Leiden', 'Lelystad', 'Leeuwarden',
'Maastricht', 'Nijmegen', 'Oss', 'Roermond', 'Roosendaal', 'Sittard', 'Tilburg', 'Utrecht', 'Venlo',
'Vlissingen', 'Zaandam', 'Zwolle', 'Zutphen']
station = random.choice(stations)
with open('file.csv', 'a') as csvfile:
w = csv.writer(csvfile, quoting=csv.QUOTE_ALL)
#w.writerow([Message_data ", " Date_data ", " Name_data ", " station])
if Name_data == '':
w.writerow([Message_data ", " s ", " 'anoniem' ", " station])
else:
w.writerow([Message_data ", " s ", " Name_data ", " station])
Message_Text = tkinter.Label(window, text="Vul hier een bericht in", font=("Calibri", 20),bg="#a9a9a9", fg="#ffffff")
Message = tkinter.Entry(window, font=("Calibri", 20),width=45, bg="#ffffff", fg="black")
Name_label = tkinter.Label(window, text="Vul hier je naam in", font=("Calibri", 20), fg="#ffffff")
Name = tkinter.Entry(window, font=("Calibri", 20), bg="#ffffff", fg="black")
button = tkinter.Button(window, text="Submit", width =20,height=5, font=("Calibri", 20), command=get_data, bg="#a9a9a9", fg="#ffffff")
Message_Text.pack()
Message.pack()
Name_label.pack()
Name.pack()
button.pack()
window.mainloop()
#root.mainloop()
I tried to use the len() function to determine if Message_Data is longer than 140 characters and then program the submit button differently to call the function on_click, but that didn't work unfortunately.
CodePudding user response:
Try it:
def get_data():
global button
Message_data = Message.get()
if len(Message_data) > 140:
messagebox.showerror("Error", 'Error: Het bericht mag maximaal 140 tekens bevatten')
return