How to write an error code for my tkinter program

Question:

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 number 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.

Asked By: Danxs

||

Answers:

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
Answered By: LucasBorges-Santos
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.