我正在创建一个动态复选框,用于从任何csv文件中提取列名,但是我在选择选项后无法清除窗口。
这是一个示例代码,其中包含名称很少的列表...
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 2 16:20:54 2016
------------------------Tkinter-------------------------
@author: Suresh
"""
import tkinter as tk
from tkinter import ttk #themed tk
win = tk.Tk()
win.title("py")
win.geometry("970x500")
win.configure()
#selection_frame = Frame(tk)
alabel = ttk.Label(win, text="Demo CheckBox",anchor='center')
alabel.grid(column=70, row=0)
alabel.configure(foreground='darkblue')
global check_box
def clearwindow():
check_box.grid_forget()
feat_names = ['Tv','Radio','Newspaper','Internet','Transport','Sports']
for i in range(len(feat_names)):
feat = tk.StringVar()
check_box = tk.Checkbutton(win, text=feat_names[i], variable=feat, state ='normal')
check_box.grid(column=30, row=i+14, sticky=tk.W)
check_box.deselect()
action = ttk.Button(win, text="Submit", command=clearwindow)
action.grid(column=4, row=30)
win.mainloop()
我想在点击提交按钮后立即获得一个清晰的窗口。
Plz帮助!!
答案 0 :(得分:1)
您必须保留对所有复选框的引用,而不仅仅是一个。
checkboxes = []
for i in range(len(feat_name)):
...
check_box = tk.Checkbutton(...)
checkboxes.append(check_box)
...
然后,循环遍历列表:
def clearwindow():
for check_box in checkboxes:
check_box.grid_forget()
请注意,只是调用grid_forget
不会破坏窗口,它只会将其隐藏在视图之外。如果您创建新的检查按钮以“替换”隐藏的那些,您将创建内存泄漏。