【发布时间】:2021-02-19 06:07:20
【问题描述】:
这是我第一次使用 StackOverflow,我被这个 python 计算器程序卡住了。
当我独立运行代码时,它工作得很好,但是如果我在另一个脚本中导入这段代码,也有一个 Tk() 的实例,它就什么也不做。当我使用打印语句检查它们时,这些按钮工作正常,我认为条目或 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")
【问题讨论】:
-
如果你从另一个具有
Tk()实例的python脚本导入这个文件,那么这是一个问题。 -
是的,我实际上正在将它导入另一个具有 Tk() 的程序中
-
最好只有一个
Tk()实例。所以你需要把这个Tk()改成Toplevel()。
标签: python tkinter import calculator