【问题标题】:Tkinter calculator program not working when i import itTkinter 计算器程序在我导入时不起作用
【发布时间】: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


【解决方案1】:

虽然您可以拥有多个主/根窗口,但您不应该这样做。 如您所见,它引入了问题。最好有一个主/根。因为这就是它的处理方式。

改为使用 TopLevel() 作为 acw1668 的建议。 Effbot's Documentation of TopLevel

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

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

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

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

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

【讨论】:

  • 您可以将Tk()更改为Toplevel()(推荐方式)或将equation的master设置为win,即equation = StringVar(master=win)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
  • 2019-10-31
  • 1970-01-01
  • 2019-03-02
  • 2017-04-11
  • 1970-01-01
  • 2020-12-03
相关资源
最近更新 更多