【发布时间】:2020-10-21 01:52:27
【问题描述】:
我正在尝试根据教程为计算器制作 GUI(链接为 https://www.youtube.com/watch?v=YXPyB4XeYLA),但我想自己创建计算器逻辑。
我所有的代码都在这里:
import tkinter as tk
root = tk.Tk()
root.title('Calculator')
ans = tk.Entry(root, borderwidth=4, width=40)
ans.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
def typeNum(number):
current = ans.get()
ans.delete(0, tk.END)
global firstNum
ans.insert(0, str(current) + str(number))
firstNum = int(str(current) + str(number))
def eq():
eqClicked = True
def operation(op):
opClicked = True
secNum = 0
if op == '+':
ans.delete(0, tk.END)
ans.wait_var(secNum)
secNum = int(ans.get())
eq()
ans.delete(0, tk.END)
ans.insert(0, firstNum + secNum)
elif op == '-':
ans.delete(0, tk.END)
ans.wait_var(secNum)
secNum = int(ans.get())
eq()
ans.delete(0, tk.END)
ans.insert(0, firstNum - secNum)
elif op == '*':
ans.delete(0, tk.END)
ans.wait_var(secNum)
secNum = int(ans.get())
eq()
ans.delete(0, tk.END)
ans.insert(0, firstNum * secNum)
elif op == '/':
ans.delete(0, tk.END)
ans.wait_var(secNum)
secNum = int(ans.get())
eq()
ans.delete(0, tk.END)
ans.insert(0, firstNum / secNum)
def clearAll():
ans.delete(0, tk.END)
button1 = tk.Button(root, text='1', padx=40, pady=20, command=lambda: typeNum(1))
button2 = tk.Button(root, text='2', padx=40, pady=20, command=lambda: typeNum(2))
button3 = tk.Button(root, text='3', padx=40, pady=20, command=lambda: typeNum(3))
button4 = tk.Button(root, text='4', padx=40, pady=20, command=lambda: typeNum(4))
button5 = tk.Button(root, text='5', padx=40, pady=20, command=lambda: typeNum(5))
button6 = tk.Button(root, text='6', padx=40, pady=20, command=lambda: typeNum(6))
button7 = tk.Button(root, text='7', padx=40, pady=20, command=lambda: typeNum(7))
button8 = tk.Button(root, text='8', padx=40, pady=20, command=lambda: typeNum(8))
button9 = tk.Button(root, text='9', padx=40, pady=20, command=lambda: typeNum(9))
button0 = tk.Button(root, text='0', padx=40, pady=20, command=lambda: typeNum(0))
equals = tk.Button(root, text='=', padx=40, pady=20, command=eq)
clear = tk.Button(root, text='CLEAR', padx=23, pady=20, command=clearAll)
add = tk.Button(root, text='+', padx=30, pady=20, command=lambda: operation('+'))
sub = tk.Button(root, text='-', padx=30, pady=20, command=lambda: operation('-'))
mult = tk.Button(root, text='x', padx=30, pady=20, command=lambda: operation('*'))
div = tk.Button(root, text='÷', padx=30, pady=20, command=lambda: operation('/'))
button1.grid(row=1, column=0)
button2.grid(row=1, column=1)
button3.grid(row=1, column=2)
button4.grid(row=2, column=0)
button5.grid(row=2, column=1)
button6.grid(row=2, column=2)
button7.grid(row=3, column=0)
button8.grid(row=3, column=1)
button9.grid(row=3, column=2)
button0.grid(row=4, column=0)
equals.grid(row=4, column=1)
clear.grid(row=4, column=2)
add.grid(row=1, column=3)
sub.grid(row=2, column=3)
mult.grid(row=3, column=3)
div.grid(row=4, column=3)
root.mainloop()
在“操作”方法中,我无法让我的程序等待用户输入他们想在计算器中使用的第二个数字。
我不断收到错误
ValueError: invalid literal for int() with base 10: ''
我认为这是因为程序只是获取输入框中的任何内容并尝试将其转换为可以添加到第一个数字的整数,并且得到一个空格。
因此,我尝试使用
time.sleep(20)
这样程序会等待我输入另一个正整数,但这只是弄乱了 GUI。
有谁知道如何让 tkinter 等待一个条目?谢谢!
这是我第一次使用 Stack Overflow 作为问题,所以请原谅任何错误。
【问题讨论】:
-
构建计算器的最佳方法通常是使用二叉树,尽管您可以使用数组制作更原始的计算器(如果您将方程式转换为后缀符号,它可以支持优先级)。您的代码按原样存在一些问题。当我尝试执行时,我实际上收到了
AttributeError: 'Entry' object has no attribute 'wait_var'。我的建议是在输入运算符时存储每个操作数以及运算符,然后在输入=时评估存储的信息(而不是在输入运算符后尝试获取另一个数字)。 -
@h0r53 感谢您的建议!我对数据结构了解不多;我只用 Python 编码一周,所以我必须先了解二叉树是什么。另外,我尝试使用
ans.wait_var(),但后来我知道它仅用于按钮。
标签: python tkinter calculator