How do you say"如果按下按钮,那么这样做"在python tkinter中?

时间:2015-10-22 15:28:00

标签: python tkinter

我想说"如果你试图按的东西是一个整数,那么就做我所拥有的,但如果它是一个字符串,那么打印" 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()

1 个答案:

答案 0 :(得分:0)

只需检查num的内容:

def btnPress(num):
    global result
    if num == "tan ":
        result = tan(float(result))
    elif num == "cos ":
        result = tan(float(result))
    ...
    else:
        result = result+str(num)