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()
CodePudding user response:
Edit: I moved the button2_click ()
to the top. I comment out in line 17.
from tkinter import *
from tkinter import messagebox
window = Tk()
window = Tk()
def main():
global window
window.title("Calculator")
window.geometry("540x540")
label1 = Label(window, text="Calculator", font=("Windsor", 30), fg="light blue")
label1.pack()
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!")
global entry1
entry1 = Entry(window, width=20, font=("Arial 20"))
entry1.pack(ipadx =10, ipady= 10)
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(ipadx =20, ipady= 20)
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()
CodePudding user response:
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()