[英]How to fix a bug where a tkinter button command calling itself does not work?
I want to have a button that has a command which quit/destroys a few other objects, including itself:我想要一个带有命令的按钮,该命令可以退出/销毁其他一些对象,包括它本身:
example:例子:
B1 = Button(root, command= delete) # contains other settings too
def delete():
B1.destroy()
# contains destruction of other objects too
example code of it in the script(incomplete):脚本中的示例代码(不完整):
def mainpage():
signup_btn = Button(root,text="Sign-Up", width=10, height=3, font=("courier", 16, "bold"), command=signup_page())
signup_btn.place(relx=0.5 , rely=0.4, anchor=(CENTER))
login_btn = Button(root,text = "Log-In", width=10, height=3, font=("courier", 16, "bold"), command=login_page())
login_btn.place(relx=0.5 , rely=0.6, anchor=(CENTER))
def signup_page():
# destroy previous page
signup_btn.destroy()
login_btn.destroy()
# setup new page
title = Label(root, text="Enter your username and password, then press save and proceed to the log-in page!", height= 4, width=30, font=("courier", 16, "bold"))
title.place(relx=0.5, rely=0.2, anchor=TOP)
def login_page():
signup_btn.destroy()
login_btn.destroy()
in this case, the login_btn contains a function, calling to destroy itself and another button.在这种情况下,login_btn 包含一个 function,调用销毁自身和另一个按钮。 this causes an error because if I place the UDF above the button's initialisation, the UDF has an error because the button being called inside is not initialised and vice versa if I put the button above the UDF这会导致错误,因为如果我将 UDF 放在按钮的初始化之上,UDF 就会出错,因为在内部调用的按钮未初始化,反之亦然,如果我将按钮放在 UDF 之上
this causes an error in which when the button is initialised, the command does not exist.这会导致错误,当按钮被初始化时,命令不存在。 and if i initiate the command first, the button does not exist.如果我先启动命令,按钮不存在。 its not just this specific scenario, it happen many times throughout the script where i an trying to call a variable/udf that is not yet created but its the same the other way round.它不仅仅是这个特定的场景,它在整个脚本中多次发生,我试图调用一个尚未创建的变量/udf,但反过来也是如此。
for some reason,e it works in 1 part of the script but the same logic works nowhere else.出于某种原因,它在脚本的 1 部分中有效,但相同的逻辑在其他任何地方都无效。 so i think im missing something about the part that works所以我想我遗漏了一些有用的部分
i tried classes, different structures, or editing the button afterwards but it doesn't work.我试过类、不同的结构,或者之后编辑按钮,但它不起作用。
Try changing command=signup_page()
to command=lambda: signup_page()
and command=login_page()
to command=lambda: login_page()
尝试将command=signup_page()
更改为command=lambda: signup_page()
并将command=login_page()
更改为command=lambda: login_page()
You could also write your program using classes, it should look somewhat like this:你也可以使用类来编写你的程序,它应该看起来像这样:
from tkinter import *
class App(Tk):
def __init__(self):
super().__init__()
self.geometry("720x480")
self.title('Test')
self.signup_btn = Button(self, text="Sign-Up", width=10, height=3,
font=("courier", 16, "bold"), command=self.signup_page)
self.signup_btn.place(relx=0.5, rely=0.4, anchor=(CENTER))
self.login_btn = Button(self, text="Log-In", width=10, height=3,
font=("courier", 16, "bold"), command=self.login_page)
self.login_btn.place(relx=0.5, rely=0.6, anchor=(CENTER))
def signup_page(self):
# destroy previous page
self.signup_btn.destroy()
self.login_btn.destroy()
# setup new page
title = Label(self, text="Enter your username and password, then press save and proceed to the log-in page!",
height=4, width=30, font=("courier", 16, "bold"))
title.place(relx=0.5, rely=0.2, anchor=(N))
def login_page(self):
self.signup_btn.destroy()
self.login_btn.destroy()
if __name__ == "__main__":
app = App()
app.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.