在切换按钮中删除/忘记按钮

时间:2016-12-28 20:22:51

标签: python user-interface tkinter togglebutton

在我的GUI应用程序中,我有一个显示/隐藏切换按钮。默认情况下,按钮文本为"显示"。单击该按钮时,按钮会从预定义列表中创建许多后续按钮,按钮文本将更改为"隐藏"。

当用户点击"隐藏"我希望隐藏/删除创建的按钮。我认为我需要在grid_forget()条件中使用else函数,但是如何使用?

感谢阅读。

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

1 个答案:

答案 0 :(得分:0)

您必须创建列表以保留按钮,然后您可以使用grid()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()

如果您总是使用相同的按钮,则可以创建一次。

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

或者您可以使用FrameButtons

进行分组
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()