python - 传递单击的按钮以在 Tkinter 中运行
问题描述
我正在使用 Tinker制作一个简单的四函数(、、和add
)sub
计算器。mult
div
这是我定义计算器按钮的方式:
b1 = tk.Button(frame, text = '1', command = lambda: click(1))
b1.place(relheight = BUTTON_RELHEIGHT, relwidth = BUTTON_RELWIDTH, relx = 0, rely = (1 - BUTTON_RELHEIGHT - BUTTON_RELHEIGHT))
b2 = tk.Button( ...
b_divide = tk.Button(frame, text = '/')
b_divide.place(relheight = BUTTON_RELHEIGHT, relwidth = BUTTON_RELWIDTH, relx = 3 * BUTTON_RELWIDTH, rely = (1 - BUTTON_RELHEIGHT - 3*BUTTON_RELHEIGHT))
我还有一个equals
计算操作的函数:
def equals():
num2 = int(e.get())
clear()
e.insert(0, (num1 + num2))
目前,equals
仅添加数字。如何将操作传递给equals
函数,以便它知道要计算什么?
解决方案
正如我所说,您可以将eval()
其用于您的计算器。见示例代码:
from tkinter import*
from tkinter import messagebox as m
top = Tk()
expression = ''
def press(num):
global expression
expression = expression + str(num)
equation.set(expression)
def equalpress():
try:
global expression
expression = str(eval(equation.get()))
equation.set(expression)
except:
m.showwarning("ERROR", "ERROR : SOMETHING'S WRONG", parent=top)
def h():
global expression
expression = expression[:-1]
len(expression) - 1
equation.set(expression)
def help1():
m.showinfo("HELP",
":)",
parent=top)
def clean():
global expression
expression = ""
equation.set("")
if __name__ == "__main__":
top.resizable(0, 0)
top.title("CALCULATOR")
top.geometry("")
equation = StringVar()
z = font.Font(family='Helvetica', size=25, weight='bold')
expression_field = Entry(top, textvariable=equation, font=z, width=8)
expression_field.grid(columnspan=200, ipadx=90, ipady=40)
y = font.Font(family='Helvetica', size=9, weight='bold')
button = Button(top, text=' HELP ', fg='BLUE', bg='white', font=y,
command=help1, height=3, width=9, bd=6, activebackground="sky blue")
button.grid(row=1, column=3)
button1 = Button(top, text=' 1 ', fg='black', bg='white', font=y,
command=lambda: press(1), height=3, width=9, bd=6)
button1.grid(row=2, column=0)
button2 = Button(top, text=' 2 ', fg='black', bg='white', font=y,
command=lambda: press(2), height=3, width=9, bd=6)
button2.grid(row=2, column=1)
button3 = Button(top, text=' 3 ', fg='black', bg='white', font=y,
command=lambda: press(3), height=3, width=9, bd=6)
button3.grid(row=2, column=2)
button4 = Button(top, text=' 4 ', fg='black', bg='white', font=y,
command=lambda: press(4), height=3, width=9, bd=6)
button4.grid(row=3, column=0)
button5 = Button(top, text=' 5 ', fg='black', bg='white', font=y,
command=lambda: press(5), height=3, width=9, bd=6)
button5.grid(row=3, column=1)
button6 = Button(top, text=' 6 ', fg='black', bg='white', font=y,
command=lambda: press(6), height=3, width=9, bd=6)
button6.grid(row=3, column=2)
button7 = Button(top, text=' 7 ', fg='black', bg='white', font=y,
command=lambda: press(7), height=3, width=9, bd=6)
button7.grid(row=4, column=0)
button8 = Button(top, text=' 8 ', fg='black', bg='white', font=y,
command=lambda: press(8), height=3, width=9, bd=6)
button8.grid(row=4, column=1)
button9 = Button(top, text=' 9 ', fg='black', bg='white', font=y,
command=lambda: press(9), height=3, width=9, bd=6)
button9.grid(row=4, column=2)
button0 = Button(top, text=' 0 ', fg='black', bg='white', font=y,
command=lambda: press(0), height=3, width=9, bd=6)
button0.grid(row=5, column=1)
plus = Button(top, text=' + ', fg='black', bg='white', font=y,
command=lambda: press("+"), height=3, width=9, bd=6)
plus.grid(row=2, column=3)
minus = Button(top, text=' - ', fg='black', bg='white', font=y,
command=lambda: press("-"), height=3, width=9, bd=6)
minus.grid(row=3, column=3)
multiply = Button(top, text=' * ', fg='black', bg='white', font=y,
command=lambda: press("*"), height=3, width=9, bd=6)
multiply.grid(row=4, column=3)
divide = Button(top, text=' / ', fg='black', bg='white', font=y,
command=lambda: press("/"), height=3, width=9, bd=6)
divide.grid(row=5, column=3)
equal = Button(top, text=' = ', fg='black', bg='white', font=y,
command=equalpress, height=3, width=9, bd=6)
equal.grid(row=1, column=0)
clear = Button(top, text=' C ', fg='blue', bg='white', font=y,
command=h, height=3, width=9, bd=6)
clear.grid(row=1, column='1')
button10 = Button(top, text=' AC ', fg='red', bg='white', font=y,
command=clean, height=3, width=9, bd=6)
button10.grid(row=1, column=2)
button11 = Button(top, text=' . ', fg='black', bg='white', font=y,
command=lambda: press("."), height=3, width=9, bd=6)
button11.grid(row=5, column=2)
button12 = Button(top, text=' ^ (POWER) ', fg='black', bg='white', font=y,
command=lambda: press("**"), height=3, width=9, bd=6)
button12.grid(row=5, column=0)
top.mainloop()
推荐阅读
- python - 如何添加显示文本的速度选项?
- r - 如何检查r中实现的期望最大化算法的正确性?
- css - React :根据父母的道具设置孩子的样式
- python - Pyspark 错误:py4j.protocol.Py4JNetworkError:Java 端的答案为空
- c# - 为什么只有一些按钮图标显示
- python - Seaborn“无法解释输入”错误
- azure-durable-functions - 持久功能终止时的清理
- php - ParseUser 问题注册(PHP SDK)
- python-3.x - pyttsx3.init() 在 python 3.9 中给出错误
- python - 如何将 .py 转换为 .exe ?Pyinstaller 不工作