简体   繁体   English

Thonny 如何修复这个堆叠的标签

[英]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?当我多次按 OK 打印出答案时,答案只是相互堆叠但没有相互替换,我该如何解决这个错误?

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()

It is better to create the result label once inside main() and update its text inside button2_click() :最好在main()中创建一次结果标签并在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()

Edit: I moved the button2_click () to the top.编辑:我将button2_click ()移到顶部。 I comment out in line 17.我在第 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM