Python:更改小部件子配置会复制tkin中的小部件

2024-05-12 14:43:02 发布

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

你好,我想我有一个python tkinter设计问题。我已经研究了this来更好地构造代码。我不想单独更改所有小部件的配置,我想使用从this问题中找到的parent_widget.winfo_children()命令。你知道吗

我想知道有没有更好的方法来不单独配置小部件,而更新它们的字体和样式。你知道吗

以下是我的代码和当前行为:

class TabOne(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.tab1_note = ttk.Notebook(self,width=parent.winfo_screenwidth(), height=parent.winfo_screenheight())

        tab1_open_observations = ttk.Frame(self.tab1_note)
        tab1_closed_observations = ttk.Frame(self.tab1_note)

        self.tab1_note.add(tab1_open_observations, text= "Open Projects")
        self.tab1_note.add(tab1_closed_observations, text= "Closed/Deferred Projects")
        self.tab1_note.pack()

        self.tab_two_load(tab1_open_observations)
        self.tab_three_load(tab1_closed_observations)
        widget_list = []
        widget_list.extend(tab1_open_observations.winfo_children())
        widget_list.extend(tab1_closed_observations.winfo_children())

        for wid in widget_list:
            try:
                wid.configure(font = 'helvetica 12')
            except:
                pass

     def tab_one_load(self,tab1_refresh_db):
     def tab_two_load(self,tab1_open_observations)

class TabTwo(Frame):
class TabThree(Frame):

class MainWindow(Frame):
    def __init__(self, window, **kwargs):
        Frame.__init__(self, window, **kwargs)
        self.load_ui()

    def load_ui(self):
        self.note = ttk.Notebook(self,width=self.window.winfo_screenwidth()-(2*self.pad), height=self.window.winfo_screenheight()-(2*self.pad))

        self.tab1 = TabOne(self.note)
        self.tab2 = TabTwo(self.note)
        self.tab3 = TabThree(self.note)

        self.note.pack()

def main():
    window = Tk()
    window.title('Productivity Tool')
    app = MainWindow(window)
    app.pack(side="top", fill="both", expand=True)
    window.mainloop()

if __name__ == '__main__':
    main()

相关下拉列表的当前行为(下面的代码):

enter image description here

如果在每个依赖选项菜单下添加project_module_dropdown.configure(font='helvetica 12'),则第二个列表不会重叠,下面是函数tab_two_load()的更多代码

def tab_two_load(self,tab1_open_observations):

    def update_modules(a,b,c):

        proj_mo_names = [module[0] for module in project_modules]
        proj_mod_select.set(proj_mo_names[0])
        project_module_dropdown = OptionMenu(tab1_open_observations,proj_mod_select,*proj_mo_names)
        project_module_dropdown.configure(font='helvetica 12')
        project_module_dropdown.grid(row=2, column=1,padx=10, pady=10)

    proj_select = StringVar(tab1_open_observations,value='Default Text')
    proj_select.trace('w',update_modules)

    proj_mod_select = StringVar(tab1_open_observations,value='Default Text')
    proj_mod_select.trace('w',update_teams)
    proj_mod_select.trace('w',update_artifacts)

    proj_names = [project[1] for project in projects]
    proj_select.set(proj_names[0])
    project_dropdown = OptionMenu(tab1_open_observations,proj_select,*proj_names)
    project_dropdown.grid(row=1,column=1,padx=10,pady=10)

我认为问题在于我如何构建代码,但是我相信我已经很好地划分了代码,但是我愿意接受建议。这不完全是一个代码复查问题。我的问题是重叠的drowdown,但我觉得我的代码中有一些重复,我想避免。任何帮助都是好的。谢谢。你知道吗

我希望这是我想要的行为,而不添加project_module_dropdown.configure(font='helvetica 12')

enter image description here


Tags: 代码selfprojectdefloadopenwindowselect