首页 > 解决方案 > 传递单击的按钮以在 Tkinter 中运行

问题描述

我正在使用 Tinker制作一个简单的四函数(、、和addsub计算器。multdiv

这是我定义计算器按钮的方式:

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函数,以便它知道要计算什么?

标签: pythonbuttontkinter

解决方案


正如我所说,您可以将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()


推荐阅读