In my GUI application I have a Show/Hide toggle button. By default the button text is "Show". When clicked the button creates a number of subsequent buttons from a predefined list and the button text is changed to "Hide".
When the user clicks "Hide" I desire to hide/remove the buttons that were created. I presume I need to use the grid_forget()
function in the else
condition but how so?
Thanks for reading.
# Toggles between Show/Hide and creates buttons
def toggle_text():
if btn['text'] == 'Show':
btn['text'] = 'Hide'
for i, item in enumerate(some_list):
btn = Button(root, text='%s' % item)
btn.grid(row=6+i, column=0, sticky=W)
else:
btn['text'] = 'Hide'
# Show/Hide button
btn = Button(root, text='Show', command=toggle_text)
btn.grid(row=5, column=0, sticky=W)
You have to create list to keep buttons and then you can use
grid()
andgrid_forget()
If you have always the same buttons then you can create them once.
Or you can use
Frame
to groupButtons