簡體   English   中英

您如何在python tkinter中說“如果按下按鈕,然后執行此操作”?

[英]How do you say “If button is pressed, then do this” in 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()

只需檢查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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM