递归Tkinter笔记本

2024-05-18 23:39:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着用一个笔记本来构建一个应用程序,在这个应用程序中,我真的不知道有多少东西会用到它。我指的是事物、标签和子标签以及子标签的子标签。我决定编写一个自定义小部件,它只允许我在参数中给出一个字典。 我可能看起来不清楚,所以听听我的例子。从图形上看,它是完全有效的,但当我们深入查看notebook.tabs字典时,情况并非如此

from tkinter import ttk
# Custom Tkinter Widget
class NotebookPlus(ttk.Notebook):
    def __init__(self, *args, schema=None, **kwargs):
        ttk.Notebook.__init__(self, *args, **kwargs)
        self.schema = schema
        self.tabs = {}

        def superiterdict(dictionnary, lastK=None, lastN=None):

            for key, value in dictionnary.items():
                if lastN == None: lastN = self

                # With multiple layer of notebook
                if isinstance(value, dict):

                    # Create a new principal notebook
                    self.tabs[key] = {}

                    # Define the frame where the new notebook will be and where tabs are going to be
                    self.tabs[key]['frame'] = ttk.Frame(lastN)
                    self.tabs[key]['frame'].pack(fill='both')

                    # Define the new notebook
                    self.tabs[key]['notebook'] = ttk.Notebook(self.tabs[key]['frame'])
                    self.tabs[key]['notebook'].pack(fill='both')

                    lastN.add(self.tabs[key]['frame'], text=key)

                    # Define tabs
                    self.tabs[key]['tabs'] = {}

                    superiterdict(dictionnary=value, lastK=key, lastN=self.tabs[key]['notebook'])

                else:
                    if self.tabs == {} and lastK == None:
                        self.tabs[lastK] = {'frame':None, 'notebook':lastN, 'tabs':{}}
                    self.tabs[lastK]['tabs'][key] = ttk.Frame(lastN)
                    self.tabs[lastK]['tabs'][key].pack(fill='both')
                    self.tabs[lastK]['notebook'].add(self.tabs[lastK]['tabs'][key], text=key)

        superiterdict(self.schema)


root = tk.Tk()
frame = tk.Frame(root)
tabsSchema = {'TAB1': {'subtab11': None, 'subtab12': None},
              'TAB2': {'subtab21': {'ssubtab211': None, 'ssubtab212': None}, 'subtab22': None}
              }
notebook = NotebookPlus(root,schema=tabsSchema)
notebook.pack(fill='both')


root.mainloop()

我的问题是,每次递归函数superterdict出现在字典前面时,它都被视为一个全新的选项卡,但有时不是。为了了解这一点,这里有一本字典,里面所有的东西都放进去了

{
    'TAB1': {
        'frame': 'blob',
        'notebook': 'blob',
        'tabs': {
                'subtab11': 'blob',
                'subtab12': 'blob'
                }
        },
    'TAB2': {
            'frame': 'blob',
            'notebook': 'blob',
            'tabs': {
                    'subtab22':'blob'
                    }
        },
    'subtab21': {
                'frame': 'blob',
                'notebook': 'blob',
                'tabs': {
                        'ssubtab211': 'blob',
                        'ssubtab212': 'blob'
                        }
                    }
}

我使用“blob”而不是“<;tkinter.ttk.Frame对象。!notebookplus。!框架2。!笔记本框架2>;'为了可读性。我们可以注意到'subtab21'不在TAB2的选项卡字典中


Tags: keyselfnone字典schema标签frameblob
1条回答
网友
1楼 · 发布于 2024-05-18 23:39:54

问题是每次都使用self.tabs添加到字典中。这意味着每当一个键有一个字典作为它的值时,该键就会被添加到self.tabs而不是父项的tabs字典中。您希望将其添加到父级的tabs字典中。
下面是一个工作superiterdict函数:

def superiterdict(dictionary, lastN = self, lastK = self.tabs):
            # lastN: The widget to use as the parent.
            # lastK: The "tabs" dictionary of the parent.
            #        If not given, it defaults to self.tabs.
            for key, value in dictionary.items():
                if isinstance(value, dict):
                    lastK[key] = {}
                    lastK[key]["frame"] = ttk.Frame(lastN)
                    lastK[key]["frame"].pack(fill = "both")
                    lastN.add(lastK[key]["frame"], text = key)
                    lastK[key]["notebook"] = ttk.Notebook(lastK[key]["frame"])
                    lastK[key]["notebook"].pack(fill = "both")
                    lastK[key]["tabs"] = {}
                    superiterdict(value, lastN = lastK[key]["notebook"], lastK = lastK[key]["tabs"])
                else:
                    lastK[key] = ttk.Frame(lastN)
                    lastK[key].pack(fill = "both")
                    lastN.add(lastK[key], text = key)

这提供了所需格式的self.tabs

相关问题 更多 >

    热门问题