我们的任务是在tkinter中创建一个计算器,在清除计算器中的值以及使用按钮执行加法(x,y)减法(x,y)积(x,y)除(x,y)运算时遇到问题。如果您能帮助我解决问题并改进代码,我将不胜感激:
from tkinter import*
n1=0
n2=0
operator=''
win=Tk()
frame=Frame(win)
frame.pack()
win.title('Justin Calculator')
txtdisplay=Entry(frame,textvariable=n1,bd=30,insertwidth=1,font=30)
txtdisplay.pack(side=TOP)
def one():
txtdisplay.insert(END,'1')
def two():
txtdisplay.insert(END,'2')
def three():
txtdisplay.insert(END,'3')
def four():
txtdisplay.insert(END,'4')
def five():
txtdisplay.insert(END,'5')
def six():
txtdisplay.insert(END,'6')
def seven():
txtdisplay.insert(END,'7')
def eight():
txtdisplay.insert(END,'8')
def nine():
txtdisplay.insert(END,'9')
def action(arg):
txtdisplay.insert(END,arg)
def add():
global n1
operator='+'
n1=float(txtdisplay.get())
txtdisplay.delete(0,END)
def addition(x,y):
return x+y
txtdisplay.insert(END,str(addition)(n1+n2)
Topframe=Frame(win)
Topframe.pack(side=TOP)
num1=Button(Topframe,padx=6, pady=6, bd=5, text='1',command=one,fg='blue')
num1.pack(side=LEFT)
num2=Button(Topframe,padx=6,pady=6,bd=5,text='2',command=two,fg='blue')
num2.pack(side=LEFT)
num3=Button(Topframe,padx=6,pady=6,bd=5,text='3',command=three,fg='blue')
num3.pack(side=LEFT)
centerframe=Frame(win)
centerframe.pack(side=TOP)
num4=Button(centerframe,padx=6,pady=6,bd=5,text='4',command=four,fg='red')
num4.pack(side=LEFT)
num5=Button(centerframe,padx=6,pady=6,bd=5,text='5',command=five,fg='red')
num5.pack(side=LEFT)
num6=Button(centerframe,padx=6,pady=6,bd=5,text='6',command=six,fg='red')
num6.pack(side=LEFT)
centerframe=Frame(win)
centerframe.pack(side=TOP)
num7=Button(centerframe,padx=6,pady=7,bd=5,text='7',command=seven,fg='black')
num7.pack(side=LEFT)
num8=Button(centerframe,padx=6,pady=7,bd=5,text='8',command=eight,fg='black')
num8.pack(side=LEFT)
num9=Button(centerframe,padx=6,pady=7,bd=5,text='9',command=nine,fg='black')
num9.pack(side=LEFT)
centerframe=Frame(win)
centerframe.pack(side=TOP)
subtraction=Button(centerframe,padx=6,pady=7,bd=5,text='-',fg='black')
subtraction.pack(side=LEFT)
num0=Button(centerframe,padx=6,pady=7,bd=5,text='0',fg='black')
num0.pack(side=LEFT)
ExitBtn=Button(centerframe,padx=6,pady=7,bd=5,text='Exit',command=win.destroy,fg='black')
ExitBtn.pack(side=LEFT)
centerframe=Frame(win)
centerframe.pack(side=TOP)
_add=Button(centerframe,padx=6,pady=7,bd=5,text='+',command=add,fg='black')
_add.pack(side=LEFT)
subtraction=Button(centerframe,padx=6,pady=7,bd=5,text='-',fg='black')
subtraction.pack(side=LEFT)
multiplication=Button(centerframe,padx=6,pady=7,bd=5,text='*',fg='black')
multiplication.pack(side=LEFT)
division=Button(centerframe,padx=6,pady=7,bd=5,text='/',fg='black')
division.pack(side=LEFT)
_equal=Button(centerframe,padx=6,pady=7,bd=5,text='=',command=equal,fg='black')
_equal.pack(side=LEFT)
bottomframe=Frame(win)
bottomframe.pack(side=TOP)
clear=Button(bottomframe,padx=6,pady=6,bd=5,text='Clear')
clear.pack(side=LEFT)
win.mainloop()
我已经修正了所有的语法错误。我已经把你的错误注释掉了。还有一些逻辑错误。你知道吗
不能这样使用
textvariable
:使用
textvariable
是解决这个问题的有效方法,但是将直接访问Entry
与使用textvariable
混合使用可能不是最佳方法,所以现在让我们暂时抛开textvariable
。你知道吗这个程序看起来像是你在构建界面时没有提前考虑如何让它运行。下面是对计算器的修改,使“+”、“=”和“清除”现在可以工作了。我还加了一个有效的小数点。你应该能把数字加起来,再加上那些和,你想要的。但您需要根据以下示例自己找出如何实现其余操作:
最后,我在单个帧中使用
grid()
管理器而不是pack()
和多个帧来重建您的接口。我并不是建议你的计算器应该这样布置,只是grid()
可能是一个更好的方式来使用这样的接口。你知道吗相关问题 更多 >
编程相关推荐