Tkinter 计算器程序在我导入时不起作用

Tkinter calculator program not working when i import it

这是我第一次使用 Whosebug,我一直在使用这个 python 计算器程序。

当我 运行 它独立时,代码工作得很好,但如果我将这段代码导入另一个也有 Tk() 实例的脚本,它就什么都不做。当我用 print 语句检查它们时,按钮工作正常,我认为条目或 StringVar() 变量有问题。 我尝试寻找解决方案,但找不到与此相关的任何内容。

代码如下:

from tkinter import *
exp=""
def press(num):
    global exp
    exp=exp + str(num)
    equation.set(exp)
def clrscr():
    global exp
    exp=""
    equation.set("")
def equalpress():
    try:
        global exp
        total=str(eval(exp))
        equation.set(total)
        exp=""
    except:
        equation.set('ERROR!')
        exp=""
win=Tk()
win.title('Calculator')
win.geometry('450x575')
win.configure(bg="#EAF0F1")
win.resizable(width=False, height=False)
equation=StringVar()
equation.set('Enter Value')
disp=Frame(win, width=500, height=100, bg="#dfe4ea")
entry=Entry(disp, width=33, textvariable=equation, font="Roboto 20", bg='#f1f2f6', foreground="#1e272e" )
entry.grid(padx=5, pady=5, ipady=20)
disp.grid(row=0, column=0, sticky="nsew")
btns=Frame(win, width=500, height=400, bg="white")
button1 = Button(btns, text=' 1 ', fg='black', bg='red', command=lambda: press(1), height=6, width=12) 
button1.grid(row=2, column=0) 

button2 = Button(btns, text=' 2 ', fg='black', bg='red',  command=lambda: press(2), height=6, width=12) 
button2.grid(row=2, column=2) 

button3 = Button(btns, text=' 3 ', fg='black', bg='red', command=lambda: press(3), height=6, width=12) 
button3.grid(row=2, column=4) 

button4 = Button(btns, text=' 4 ', fg='black', bg='red', command=lambda: press(4), height=6, width=12) 
button4.grid(row=3, column=0) 

button5 = Button(btns, text=' 5 ', fg='black', bg='red', command=lambda: press(5), height=6, width=12) 
button5.grid(row=3, column=2) 

button6 = Button(btns, text=' 6 ', fg='black', bg='red', command=lambda: press(6), height=6, width=12) 
button6.grid(row=3, column=4) 

button7 = Button(btns, text=' 7 ', fg='black', bg='red', command=lambda: press(7), height=6, width=12) 
button7.grid(row=4, column=0) 

button8 = Button(btns, text=' 8 ', fg='black', bg='red', command=lambda: press(8), height=6, width=12) 
button8.grid(row=4, column=2) 

button9 = Button(btns, text=' 9 ', fg='black', bg='red', command=lambda: press(9), height=6, width=12) 
button9.grid(row=4, column=4) 

button0 = Button(btns, text=' 0 ', fg='black', bg='red', command=lambda: press(0), height=6, width=12) 
button0.grid(row=5, column=0) 

plus = Button(btns, text=' + ', fg='black', bg='red', command=lambda: press("+"), height=6, width=12) 
plus.grid(row=2, column=6) 

minus = Button(btns, text=' - ', fg='black', bg='red', command=lambda: press("-"), height=6, width=12) 
minus.grid(row=3, column=6) 

multiply = Button(btns, text=' * ', fg='black', bg='red',command=lambda: press("*"), height=6, width=12) 
multiply.grid(row=4, column=6) 

divide = Button(btns, text=' / ', fg='black', bg='red', command=lambda: press("/"), height=6, width=12) 
divide.grid(row=5, column=6) 

equal = Button(btns, text=' = ', fg='black', bg='red', command=lambda: equalpress(), height=6, width=12) 
equal.grid(row=5, column=4) 

clear = Button(btns, text='Clear', fg='black', bg='red', command=lambda: clrscr(), height=6, width=50) 
clear.grid(row=6, columnspan=8) 

Decimal= Button(btns, text='.', fg='black', bg='red', command=lambda: press('.'), height=6, width=12) 
Decimal.grid(row=5, column=2)
btns.grid(sticky="nsew")

虽然您可以拥有多个 master/root window,但您不应该。 如您所见,它引入了问题。最好有一个master/root。因为就是这样对待的。

而是按照 acw1668 的建议使用 TopLevel()Effbot's Documentation of TopLevel

否则,问题出在条目上,而不是按钮上。这些按钮适当地设置了 stringvar,但该条目不会更新为该新值。

一个廉价的可怕解决方案是把

def updateEntry():
    entry.delete(0,END)
    entry.insert(0, equation.get())
equation.trace_add("write", lambda *_ : updateEntry())

输入后。但是,当您 运行 计算器本身时,这会增加更多问题。不要这样做。请。

我不确定其他程序的作用,但将其设置为顶级是最好的解决方案。