python tkinter按钮按一下继续执行_在pythontkinter中,如何说“如果按下了按钮,则执行此操作”?...

博客围绕Python Tkinter展开,探讨按相等键时整数与字符串的处理方式。给出了使用Tkinter创建计算器界面的代码,定义了按钮点击、计算结果、清空等功能函数,并创建了数字、运算符、三角函数等按钮,最后通过主循环运行界面。
摘要由CSDN通过智能技术生成

我想说的是“如果你要按相等键的东西是一个整数,那就按我的方法做,但是如果是字符串,那么就打印“cos(无论数字)”、“tan(任意数字)”或“sin(任意数字)”。请帮助澄清如何做到这一点。谢谢您。在from tkinter import*

root = Tk()

def btnPress(num):

global result

result = result+str(num)

equation.set(result)

def equalPress():

global result

total = str(eval(result))

equation.set(total)

result = total

def clear():

global result

result = ""

equation.set("")

result = ""

btnWidth = 5

equation = StringVar()

equation.set("Enter your equation.")

calculation = Label(root, textvariable = equation, width = btnWidth*4)

calculation.grid(row = 0, column = 0, columnspan = 4)

Button0 = Button(root, text = "0", command=lambda:btnPress(0), width = btnWidth)

Button0.grid(row = 4, column = 2)

Button1 = Button(root, text = "1", command=lambda:btnPress(1), width = btnWidth)

Button1.grid(row = 1, column = 1)

Button2 = Button(root, text = "2", command=lambda:btnPress(2), width = btnWidth)

Button2.grid(row = 1, column = 2)

Button3 = Button(root, text = "3", command=lambda:btnPress(3), width = btnWidth)

Button3.grid(row = 1, column = 3)

Button4 = Button(root, text = "4", command=lambda:btnPress(4), width = btnWidth)

Button4.grid(row = 2, column = 1)

Button5 = Button(root, text = "5", command=lambda:btnPress(5), width = btnWidth)

Button5.grid(row = 2, column = 2)

Button6 = Button(root, text = "6", command=lambda:btnPress(6), width = btnWidth)

Button6.grid(row = 2, column = 3)

Button7 = Button(root, text = "7", command=lambda:btnPress(7), width = btnWidth)

Button7.grid(row = 3, column = 1)

Button8 = Button(root, text = "8", command=lambda:btnPress(8), width = btnWidth)

Button8.grid(row = 3, column = 2)

Button9 = Button(root, text = "9", command=lambda:btnPress(9), width = btnWidth)

Button9.grid(row = 3, column = 3)

Button10 = Button(root, text = "tan", command=lambda:btnPress("tan "), width = btnWidth)

Button10.grid(row = 3, column = 0)

Button11 = Button(root, text = "sin", command=lambda:btnPress("sin "), width = btnWidth)

Button11.grid(row = 2, column = 0)

Button12 = Button(root, text = "cos", command=lambda:btnPress("cos "), width = btnWidth)

Button12.grid(row = 1, column = 0)

plus = Button(root, text = "+", command = lambda:btnPress("+"), width = btnWidth)

plus.grid(row = 1, column = 4)

plus = Button(root, text = "-", command = lambda:btnPress("-"), width = btnWidth)

plus.grid(row = 2, column = 4)

plus = Button(root, text = "*", command = lambda:btnPress("*"), width = btnWidth)

plus.grid(row = 3, column = 4)

plus = Button(root, text = "/", command = lambda:btnPress("/"), width = btnWidth)

plus.grid(row = 4, column = 4)

plus = Button(root, text = ".", command = lambda:btnPress("."), width = btnWidth)

plus.grid(row = 4, column = 1)

if ==

equal = Button(root, text = "=", command = equalPress, width = btnWidth)

equal.grid(row = 4, column = 3)

equal = Button(root, text = "C", command = clear, width = btnWidth)

equal.grid(row = 4, column = 0)

root.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值