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

时间:2017-04-14 13:48:42

标签: python python-3.x tkinter

我想知道如何忘记一个小部件而不是另一个功能? 我的代码很安静。

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()

提前致谢。

1 个答案:

答案 0 :(得分:3)

如果你发现自己需要这么多,你应该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()