tkinter中的按钮在被按下之前正在调用一个函数

2024-04-29 03:45:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我用Python2.7用tkinter编程。使用这个语法很多次,但由于某种原因,在我声明按钮后,它调用函数而不按它。。 代码如下:

def uninstall_win():
    verify = Tk()
    verify.title("Uninstall")
    recheck = make_entry(verify, "Please Re-enter password:", 14, show='*')
    b = Button(verify, borderwidth=4, text="Uninstall", pady=8, command=uninstall(recheck.get()))
    b.pack()
    verify.mainloop()

def make_entry(parent, caption, width=None, **options):
    Label(parent, text=caption).pack(side=TOP)
    entry = Entry(parent, **options)
    if width:
        entry.config(width=width)
    entry.pack(side=TOP, padx=10, fill=BOTH)
    return entry

任何见解都将受到赞赏


Tags: textmaketopdefwidthsidepackparent
2条回答

因为您调用的是函数,而不是作为可调用函数传递函数。在

lambda: uninstall(recheck.get())

传递uninstall(recheck.get())将命令设置为此函数返回的内容

将带参数的函数放入Button中时应使用lambda。在

b = Button(verify, borderwidth=4, text="Uninstall", pady=8, command=lambda: uninstall(recheck.get()))

相关问题 更多 >