我想说的是“如果你要按相等键的东西是一个整数,那就按我的方法做,但是如果是字符串,那么就打印“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()