1

I want to know how to forget a widget made instead another function? My code is quiet messy.

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)

I want to be able forget these widgets from another function like so:

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

Thanks in advance.

alienware13user
  • 129
  • 1
  • 8
  • What do you mean by "forget"? What are you trying to accomplish? Just remove them from view so you can make them appear later, or do you want to actually destroy them? – Bryan Oakley Apr 14 '17 at 14:24

1 Answers1

3

If you find yourself needing this a lot, you should re-structure your program using classes.

You can move creation of widgets to global scope and grid/forget them whenever you needed.

#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()
Community
  • 1
  • 1
Lafexlos
  • 7,618
  • 5
  • 38
  • 53