Search code examples
pythontkinterthonny

Thonny how to fix this stacked label


When I press OK to print out the answer multiple times, the answers just stack on each other but are not replaced by each other, how do I fix this bug?

from tkinter import *
from tkinter import messagebox
def main():
    global window
    window = Tk()
    window.title("Calculator")
    window.geometry("540x540")

    label1 = Label(window, text="Calculator", font=("Windsor", 30), fg="light blue")
    label1.pack()

    global entry1
    entry1 = Entry(window, width=20, font=("Arial 20"))
    entry1.pack()
    entry1.focus()

    label2 = Label(window, text="+", font=("Windsor", 30), fg="light blue")
    label2.pack()

    global entry2
    entry2 = Entry(window, width=20, font=("Arial 20"))
    entry2.pack()
    entry2.focus()

    global label3
    label3 = Label(window, text="=", font=("Windsor", 30), fg="light blue")
    label3.pack()

    button2 = Button(window, text="OK", fg="blue",
                 font="Arial 16 bold", command =button2_click)
    button2.pack()

    window.mainloop()


def button2_click():
    Label.after(0, label.master.destroy)
    try:
        a = int(entry1.get())
        b = int(entry2.get())
        Label(window, text=f"{a+b}", font=("Windsor", 30), fg="light blue").pack()  
    except:
        messagebox.showwarning("Error", "Invalid Entry or Entry Missing!")

if __name__=='__main__':
    main()

Solution

  • It is better to create the result label once inside main() and update its text inside button2_click():

    from tkinter import *
    from tkinter import messagebox
    
    def main():
        global entry1, entry2, result
    
        window = Tk()
        window.title("Calculator")
        window.geometry("540x540")
    
        label1 = Label(window, text="Calculator", font=("Windsor", 30), fg="light blue")
        label1.pack()
    
        entry1 = Entry(window, width=20, font=("Arial 20"))
        entry1.pack()
        entry1.focus()
    
        label2 = Label(window, text="+", font=("Windsor", 30), fg="light blue")
        label2.pack()
    
        entry2 = Entry(window, width=20, font=("Arial 20"))
        entry2.pack()
    
        label3 = Label(window, text="=", font=("Windsor", 30), fg="light blue")
        label3.pack()
    
        # create result label
        result = Label(window, font=("Windsor", 30), fg="light blue")
        result.pack()
    
        button2 = Button(window, text="OK", fg="blue", font="Arial 16 bold", command=button2_click)
        button2.pack()
    
        window.mainloop()
    
    
    def button2_click():
        # below line will raise exception
        #Label.after(0, label.master.destroy)
        try:
            a = int(entry1.get())
            b = int(entry2.get())
            # update result label
            result.config(text=f"{a+b}")
        except:
            messagebox.showwarning("Error", "Invalid Entry or Entry Missing!")
    
    if __name__=='__main__':
        main()