0

Hello I think I have a python tkinter design issue. I have looked on this to structure my code better. I do not want to change the configuration of all my widgets individually and would like to use parent_widget.winfo_children() command that I found from this question.

I am wondering is there a better way to not individually configure the widgets and yet update their font and Style.

Here is my code and the current behavior:

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

Current behavior for dependent drop down lists (code below):

enter image description here

The second list does not overlap if I add project_module_dropdown.configure(font='helvetica 12') below every dependent OptionsMenu, Here is more code for the function 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)

I think the problem is with how I have structured my code but i believe i have compartmentalized the code well but I am open to suggestions. this is not exactly a code review problem. My problem is overlapping drowdowns, but I feel that I have some duplication in my code that I would like to avoid. Any help is great. Thanks.

I would like this as my desired behavior without adding project_module_dropdown.configure(font='helvetica 12'):

enter image description here

technazi
  • 888
  • 4
  • 21
  • 42
  • 1
    Please try to reduce the code down to a [mcve]. For example, can you illustrate the same problem without creating a custom style? If so, that would eliminate almost two dozen lines. Is the use of a notebook with three tabs necessary, or can you illustrate the problem with just a normal frame? – Bryan Oakley Oct 12 '18 at 19:59
  • Agreed! I have made the changes and made it minimal. The reason i am keeping the tabs is to give an idea of how many places I would have to add the configuration line. – technazi Oct 12 '18 at 20:07

0 Answers0