如何通过 Tkinter 按钮中的命令发送值?

How do i send value via command in Tkinter Button?

我正在编写我的第一个“大”项目 - 使用 Tkinter 的简单计算器 UI。完成视觉后,我遇到了将 digit/math 运算符发送到计算器屏幕的问题。当我尝试通过参数将数字发送到 adder() 时,它什么都不做(adder 是一个创建字符串的函数,该字符串将被发送到屏幕和数学求解器)。 def eq 没有​​完成,但屏幕应该可以工作 w/o 它。请帮忙

顺便说一句,我不明白为什么 screen_out 似乎不在加法器中(这就是我使用全局的原因)

代码:

from tkinter import *

# logical part
screen_out = ''


def adder(ele):
    global screen_out
    screen_out += str(ele)
    screen.configure(text=f"{screen_out}")


def deleter():
    global screen_out
    screen_out = screen_out[:-1]
    screen.configure(text=f"{screen_out}")


def cleaner():
    global screen_out
    screen_out = ''
    screen.configure(text=f"{screen_out}")


def eq():
    global task
    task = int(screen_out)


# main window
window = Tk()
window.title('Basic Calculator')
window.geometry('400x350')

# output screen
black_lbl = Label(window, width=1, height=2, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8')
black_lbl.place(x=390, y=0)
screen_former = Label(window, text="", width=6, height=2, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8')
screen_former.grid(column=0, row=0)
screen = Label(window, text="", width=27, height=2, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', anchor="e")
screen.place(x=-21, y=0)

# keyboard interface
btn9 = Button(window, text="9", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=adder('9'))
btn8 = Button(window, text="8", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('8'))
btn7 = Button(window, text="7", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('7'))
btn6 = Button(window, text="6", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('6'))
btn5 = Button(window, text="5", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('5'))
btn4 = Button(window, text="4", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('4'))
btn3 = Button(window, text="3", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('3'))
btn2 = Button(window, text="2", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('2'))
btn1 = Button(window, text="1", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('1'))
btn0 = Button(window, text="0", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('0'))
btn_dot = Button(window, text=".", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('.'))
btn_eq = Button(window, text="=", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=eq)
btn_plus = Button(window, text="+", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('+'))
btn_minus = Button(window, text="-", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('-'))
btn_mult = Button(window, text="*", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('*'))
btn_div = Button(window, text="/", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('/'))
btn_par_open = Button(window, text="(", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder('('))
btn_par_clos = Button(window, text=")", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=command=adder(')'))
btn_clear = Button(window, text="C", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=cleaner)
btn_del = Button(window, text="DEL", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=deleter)

u = 0
if u == 0:
    cleaner()
    u += 1

btn_par_open.grid(column=0, row=1)
btn_par_clos.grid(column=1, row=1)
btn_clear.grid(column=2, row=1)
btn_del.grid(column=3, row=1)
btn7.grid(column=0, row=2)
btn8.grid(column=1, row=2)
btn9.grid(column=2, row=2)
btn_plus.grid(column=3, row=2)
btn4.grid(column=0, row=3)
btn5.grid(column=1, row=3)
btn6.grid(column=2, row=3)
btn_minus.grid(column=3, row=3)
btn1.grid(column=0, row=4)
btn2.grid(column=1, row=4)
btn3.grid(column=2, row=4)
btn_mult.grid(column=3, row=4)
btn_eq.grid(column=0, row=5)
btn0.grid(column=1, row=5)
btn_dot.grid(column=2, row=5)
btn_div.grid(column=3, row=5)

window.mainloop()

您在创建按钮时调用了 adder() 函数。 command 参数必须是用户单击按钮时将调用的函数。为此使用 lambda。

btn9 = Button(window, text="9", width=6, height=1, font=("Consolas", "20", "bold"), bg='#424242', fg='#E8E8E8', command=lambda: adder('9'))

此外,您的其他按钮有错字:command=command=