将按钮连接到基于Python tkin构建的计算器中的操作

2024-09-29 01:30:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我们的任务是在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()

Tags: textdefbuttonleftwinsidebdcommand
2条回答

我已经修正了所有的语法错误。我已经把你的错误注释掉了。还有一些逻辑错误。你知道吗

from tkinter import Tk,Frame,Entry,Button

#import tkinter

n1=0
n2=0
operator=''

win=Tk()
frame=Frame(win)
frame.pack()
win.title('Justin Calculator')

END='end'
txtdisplay=Entry(frame,textvariable=n1,bd=30,insertwidth=1,font=30)
#why have you added unnecessary indentation?
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):

    txtdisplay.insert(END,str(addition)(n1+n2))
    return x+y #the statement after return statement will never get executed


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')

# side= LEFT or TOP does not work. follw the dicumentation

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='=',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

n1=0
# ...
txtdisplay=Entry(frame,textvariable=n1, ...

使用textvariable是解决这个问题的有效方法,但是将直接访问Entry与使用textvariable混合使用可能不是最佳方法,所以现在让我们暂时抛开textvariable。你知道吗

这个程序看起来像是你在构建界面时没有提前考虑如何让它运行。下面是对计算器的修改,使“+”、“=”和“清除”现在可以工作了。我还加了一个有效的小数点。你应该能把数字加起来,再加上那些和,你想要的。但您需要根据以下示例自己找出如何实现其余操作:

import operator
from tkinter import *

def zero():
    txtdisplay.insert(END, '0')
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 decimal():
    if '.' not in txtdisplay.get():
        txtdisplay.insert(END, '.')

def add():
    global n1, operation
    operation = operator.add
    n1 = float(txtdisplay.get())
    txtdisplay.delete(0, END)

def equal():
    global operation
    n2 = float(txtdisplay.get())
    txtdisplay.delete(0, END)

    if operation:
        txtdisplay.insert(0, str(operation(n1, n2)))
        operation = None

def clear():
    global n1, operation
    n1 = 0.0
    operation = None
    txtdisplay.delete(0, END)

n1 = 0.0
operation = None

win = Tk()
win.title('StackOverflow Calculator')

options = {'padx':6, 'pady':6, 'bd':5}
sticky = {'sticky': (N, S, E, W)}

frame = Frame(win)
frame.pack()

txtdisplay = Entry(frame, bd=30, insertwidth=1, font=30)
txtdisplay.grid(row=0, columnspan=4)

Button(frame, text='-', fg='black', **options).grid(row=1, column=3, **sticky)
Button(frame, text='*', fg='black', **options).grid(row=1, column=2, **sticky)
Button(frame, text='/', fg='black', **options).grid(row=1, column=1, **sticky)

Button(frame, text='7', command=seven, fg='black', **options).grid(row=2, column=0, **sticky)
Button(frame, text='8', command=eight, fg='black', **options).grid(row=2, column=1, **sticky)
Button(frame, text='9', command=nine, fg='black', **options).grid(row=2, column=2, **sticky)

Button(frame, text='4', command=four, fg='red', **options).grid(row=3, column=0, **sticky)
Button(frame, text='5', command=five, fg='red', **options).grid(row=3, column=1, **sticky)
Button(frame, text='6', command=six, fg='red', **options).grid(row=3, column=2, **sticky)

Button(frame, text='1', command=one, fg='blue', **options).grid(row=4, column=0, **sticky)
Button(frame, text='2', command=two, fg='blue', **options).grid(row=4, column=1, **sticky)
Button(frame, text='3', command=three, fg='blue', **options).grid(row=4, column=2, **sticky)

Button(frame, text='0', command=zero, fg='black', **options).grid(row=5, column=1, **sticky)
Button(frame, text='.', command=decimal, fg='black', **options).grid(row=5, column=2, **sticky)
Button(frame, text='Exit', command=win.destroy, fg='black', **options).grid(row=5, column=0, **sticky)

Button(frame, text='+', command=add, fg='black', **options).grid(row=2, column=3, rowspan=2, **sticky)
Button(frame, text='=', command=equal, fg='black', **options).grid(row=4, column=3, rowspan=2, **sticky)
Button(frame, text='Clear', command=clear, **options).grid(row=1, column=0, **sticky)

win.mainloop()

最后,我在单个帧中使用grid()管理器而不是pack()和多个帧来重建您的接口。我并不是建议你的计算器应该这样布置,只是grid()可能是一个更好的方式来使用这样的接口。你知道吗

enter image description here

相关问题 更多 >