我在Python 3中有一个计算器但由于某些原因我的代码不起作用。它工作正常,直到我按下" ="按钮来计算我的东西(使用tkinter为gui)。这是我的代码(很长,对不起,它只是我代码中的一部分)
from tkinter import *
from tkinter.ttk import *
def SMATH():
#PUT TEXT IN ENTRY FUNCTION
def puttext(text):
sm_ent.insert(0,text)
return
#CLEAR THE ENTRY FUNCTION
def cleartext():
sm_ent.delete(0, END)
return
#GET THE OUTCOME FUNCTION
def Coutcome():
calc = caltext.get()
answer = eval(calc)
sm_ent = Entry(e_frame, textvariable=caltext, text=answer)
smathW = Tk()
smathW.title("Simple Math")
smathW.resizable(0,0)
smathW.wm_iconbitmap('icon.ico')
def quitSmath():
smathW.destroy()
#ENTRY FRAME
e_frame = Frame(smathW)
e_frame.pack()
#BUTTONS FRAME
b_frame = Frame(smathW)
b_frame.pack()
#ENTRY
caltext= StringVar()
sm_ent = Entry(e_frame, textvariable=caltext)
sm_ent.pack(fill=Y)
#LABEL
sm_inf = Label(smathW, text="Here is the Simple Math mode from CalfoWin")
sm_inf.pack()
#BUTTON 7
sm_butt0 = Button(b_frame, text="7", command=lambda: puttext('7'))
sm_butt0.grid(row=1, column=0)
#BUTTON 8
sm_butt1 = Button(b_frame, text="8", command=lambda: puttext('8'))
sm_butt1.grid(row=1, column=1)
#BUTTON 9
sm_butt2 = Button(b_frame, text="9", command=lambda: puttext('9'))
sm_butt2.grid(row=1, column=2)
#BUTTON 4
sm_butt3 = Button(b_frame, text="4", command=lambda: puttext('4'))
sm_butt3.grid(row=2, column=0)
#BUTTON 5
sm_butt4 = Button(b_frame, text="5", command=lambda: puttext('5'))
sm_butt4.grid(row=2, column=1)
#BUTTON 6
sm_butt5 = Button(b_frame, text="6", command=lambda: puttext('6'))
sm_butt5.grid(row=2, column=2)
#BUTTON 1
sm_butt6 = Button(b_frame, text="1", command=lambda: puttext('1'))
sm_butt6.grid(row=3, column=0)
#BUTTON 2
sm_butt7 = Button(b_frame, text="2", command=lambda: puttext('2'))
sm_butt7.grid(row=3, column=1)
#BUTTON 3
sm_butt8 = Button(b_frame, text="3", command=lambda: puttext('3'))
sm_butt8.grid(row=3, column=2)
#BUTTON 0
sm_butt9 = Button(b_frame, text="0", command=lambda: puttext('0'))
sm_butt9.grid(row=4, column=0)
#BUTTON KOMMA
sm_buttKomma = Button(b_frame, text=".", command=lambda: puttext('.'))
sm_buttKomma.grid(row=4, column=1)
#BUTTON EQUALSIGN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
sm_buttEsign = Button(b_frame, text="=", command=Coutcome())
sm_buttEsign.grid(row=4, column=2)
#BUTTON ADDITION
sm_buttPlus = Button(b_frame, text="+", command=lambda: puttext('+'))
sm_buttPlus.grid(row=2, column=3)
#BUTTON SUBTRACKTION
sm_buttMin = Button(b_frame, text="-", command=lambda: puttext('-'))
sm_buttMin.grid(row=3, column=3)
#BUTTON DIVISION
sm_buttDiv = Button(b_frame, text="/", command=lambda: puttext('/'))
sm_buttDiv.grid(row=3, column=4)
#BUTTON MULTIPLICATION
sm_buttMult = Button(b_frame, text='x', command=lambda: puttext('*'))
sm_buttMult.grid(row=2, column=4)
#BUTTON CLEAR
sm_buttClear = Button(b_frame, text="Clear", command=lambda: cleartext())
sm_buttClear.grid(row=1, column=3)
#BUTTON QUIT
sm_buttquit = Button(b_frame, text="Quit", command=lambda: quitSmath())
sm_buttquit.grid(row=1, column=4)
答案 0 :(得分:1)
您使用command = Coutcome()
传递Coutcome
的返回值作为按下按钮时要调用的功能。所以现在发生的是在创建按钮时调用Coutcome
,caltext
仍为空。这意味着您正在运行eval('')
,这会让您看到错误。
这不是您想要的,您希望在按下按钮时调用Coutcome
。因此,您应该将函数名称(不带括号)作为命令
sm_buttEsign = Button(b_frame, text="=", command=Coutcome)
对于你的退出和清除按钮,你应该做同样的事情,摆脱括号和lambda。有时需要使用lambda的原因是因为当你不得不省略括号时,你不能将变量传递给命令。当您不需要传递变量时,lambda是多余的。
此外,您应该小心使用eval
。如果输入错误,可以be dangerous。因此,请注意让用户在没有适当限制的情况下将内容输入eval
。