【问题标题】:tkinter Checkbox from list loop. How rebuild (update) checkboxes when new list arrives?列表循环中的 tkinter 复选框。新列表到达时如何重建(更新)复选框?
【发布时间】:2021-11-30 17:04:02
【问题描述】:

在我正在构建的 tkinter 应用程序(win10 python 3.8)中,每次打开一个新文件时,我都会得到一个新列表,我将其分发到文本框(Ok)、组合框(ok)等。当我打开第一个列表时复选框循环构建正常,下一个名为复选框的文件不会更改。我无法更新复选框,我的意思是,删除以前的列表并插入另一个。在我使用按钮的示例中(在应用程序 askopenfilename 中),列表构建一个波纹管另一个。我需要一个替换另一个。我相信我需要使用 grid.clear() 或销毁,但是如何?提前致谢。

from tkinter import *
        
root = Tk()    
root.geometry('400x400')

my_friends = ['Donald', 'Daisy', 'Uncle Scrooge', 'Ze Carioca']
        
my_heroes = ['Captain America', 'Hulk', 'Spider Man', 'Black Widow',
                     'Wanda Maximoff', 'Vision', 'Winter Soldier']
        
what_list = ' '
        
def list_my_friends():
    global what_list
    what_list = my_friends
    create_cbox()
            
def list_my_heroes():
    global what_list
    what_list = my_heroes
    create_cbox()
        
def create_cbox():
    for index, friend in enumerate(what_list):
        current_var = tk.StringVar()
        current_box = tk.Checkbutton(root, text= friend,
                                     variable = current_var,
                                     onvalue = friend, 
                                     offvalue = '')
        current_box.pack()
        
       
button1= tk.Button(root, text = "my_friends",command=lambda:list_my_friends()).pack()
        
button2= tk.Button(root, text = "my_heroes",command=lambda:list_my_heroes()).pack()
        
    
root.mainloop() 

【问题讨论】:

    标签: list loops tkinter checkbox replace


    【解决方案1】:

    您可以将这些复选按钮放在一个框架中,然后在填充新的复选按钮之前删除旧的复选按钮会更容易:

    def create_cbox():
        # remove existing checkbuttons
        for w in container.winfo_children():
            w.destroy()
        # populate new checkbuttons
        for friend in what_list:
            current_var = tk.StringVar()
            # use container as the parent
            current_box = tk.Checkbutton(container, text=friend,
                                         variable=current_var,
                                         onvalue=friend,
                                         offvalue='')
            current_box.pack(anchor='w')
    
    
    tk.Button(root, text="my_friends", command=list_my_friends).pack()
    tk.Button(root, text="my_heroes", command=list_my_heroes).pack()
    
    # frame to hold the checkbuttons
    container = tk.Frame(root)
    container.pack(padx=10, pady=10)
    

    【讨论】:

    • 你的回答解决了我的问题,谢谢!
    【解决方案2】:

    在课堂上稍作调整:

    class PageEight(tk.Frame):
    
        def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)  # parent is Frame
            self.controller = controller
            
            # to adequate to widgets outside the self.container_pg8: 
            self.rowspan_list = len(what_list) 
    
            #container:
            self.container_pg8 = tk.Frame(self)
            self.container_pg8.grid(row=2,
                                    rowspan = self.rowspan_list,
                                    column=1,)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 2019-02-02
      相关资源
      最近更新 更多