gpt4 book ai didi

python - Tkinter 忘记在另一个函数中创建的小部件

转载 作者:行者123 更新时间:2023-11-30 22:38:49 24 4
gpt4 key购买 nike

我想知道如何忘记一个小部件而不是另一个功能?我的代码非常困惑。

def page2(cur1):
label2 = Label(root, text="How much would you like to convert?")
entry1 = Entry(root)
buttonSubmit = Button(root, text="Submit", command=lambda: get_entry(entry1, cur1))

label2.grid(row=1, columnspan=5)
entry1.grid(row=2, sticky="w")
buttonSubmit.grid(row=2, column=1)

我希望能够从另一个函数中忘记这些小部件,如下所示:

def forget():
label2.grid_forget()
entry1.grid_forget()

提前致谢。

最佳答案

如果您发现自己非常需要这个,您应该 re-structure your program using classes .

您可以将小部件的创建移至全局范围,并在需要时网格/忽略它们。

#create your widgets in global scope so you can reach them in any function
label2 = Label(root, text="How much would you like to convert?")
entry1 = Entry(root)

def page2(cur1):
buttonSubmit = Button(root, text="Submit", command=lambda: get_entry(entry1, cur1))

label2.grid(row=1, columnspan=5)
entry1.grid(row=2, sticky="w")
buttonSubmit.grid(row=2, column=1)

def forget():
label2.grid_forget()
entry1.grid_forget()

关于python - Tkinter 忘记在另一个函数中创建的小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43412898/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com