Reputation: 159
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)
Upvotes: 1
Views: 2701
Reputation: 143098
You have to create list to keep buttons and then you can use grid()
and grid_forget()
import tkinter as tk
# --- functions ---
def toggle_text():
global buttons # inform function to use external/global variable
if btn['text'] == 'Show':
btn['text'] = 'Hide'
for i, item in enumerate(some_list, 6):
# don't use name `btn` because you overwrite external `btn`
b = tk.Button(root, text=item)
b.grid(row=i, column=0, sticky='we')
buttons.append(b)
else:
btn['text'] = 'Show'
for b in buttons:
#b.grid_forget()
# better `destroy` because you will create new buttons
# so you can free memory
b.destroy()
# remove all buttons from list
#buttons.clear() # only Python 3 (doesn't need `global buttons`)
#buttons[:] = [] # Python 2 and 3 (doesn't need `global buttons`)
buttons = [] # Python 2 and 3 but needs `global buttons`
# --- main ---
some_list = ['A', 'B', 'C']
buttons = [] # create global variable
root = tk.Tk()
btn = tk.Button(root, text='Show', command=toggle_text)
btn.grid(row=5, column=0, sticky='w')
root.mainloop()
If you have always the same buttons then you can create them once.
import tkinter as tk
# --- functions ---
def toggle_text():
if btn['text'] == 'Show':
btn['text'] = 'Hide'
for i, b in enumerate(buttons, 6):
b.grid(row=i, column=0, sticky='we')
else:
btn['text'] = 'Show'
for b in buttons:
b.grid_forget()
# --- main ---
root = tk.Tk()
some_list = ['A', 'B', 'C']
buttons = [] # create global variable
for item in some_list:
b = tk.Button(root, text=item)
buttons.append(b)
btn = tk.Button(root, text='Show', command=toggle_text)
btn.grid(row=5, column=0, sticky='w')
root.mainloop()
Or you can use Frame
to group Buttons
import tkinter as tk
# --- functions ---
def toggle_text():
if btn['text'] == 'Show':
btn['text'] = 'Hide'
frame.grid(row=6, column=0, sticky='we')
else:
btn['text'] = 'Show'
frame.grid_forget()
# --- main ---
root = tk.Tk()
some_list = ['A', 'B', 'C']
frame = tk.Frame(root)
frame.columnconfigure(0, weight=1) # resise column
for i, item in enumerate(some_list):
b = tk.Button(frame, text=item)
b.grid(row=i, column=0, sticky='we')
btn = tk.Button(root, text='Show', command=toggle_text)
btn.grid(row=5, column=0, sticky='w')
root.mainloop()
Upvotes: 1