Oct-29-2020, 07:06 AM
Hi,
I have a large python GUI program and I wanted to add a button that "resets all settings to default"
I have 3 pages of my program that I need to create new instances of these pages to "reset the settings".
I made a function that does the work, the only issue is, my navigation buttons stop working. That is because they lead to the previous instance of the page, after the new instances are created the navigation buttons simply don't do anything.
Is there a standard way to get around this? One random idea I had was using getters/setters in the format below, would that work for instances?
The function that does the work is called resetPages()
I have a large python GUI program and I wanted to add a button that "resets all settings to default"
I have 3 pages of my program that I need to create new instances of these pages to "reset the settings".
I made a function that does the work, the only issue is, my navigation buttons stop working. That is because they lead to the previous instance of the page, after the new instances are created the navigation buttons simply don't do anything.
Is there a standard way to get around this? One random idea I had was using getters/setters in the format below, would that work for instances?
@property def Temp(self): return self._temp @Temp.setter def Temp(self, value): self._temp = valueI tried to strip down my program, and I have included the code. I doubt it actually runs, but at least y'all have something to go off.
The function that does the work is called resetPages()
import tkinter as tk class Page(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) def show(self): self.lift() class MainPage(Page): def __init__(self, *args, **kwargs): Page.__init__(self, *args, **kwargs) main_desc = tk.Label(text="stuff and things") class FunctionsPage(Page): def __init__(self, *args, **kwargs): Page.__init__(self, *args, **kwargs) self.device_desc_lbl = tk.Label(text="Devices", anchor=tk.CENTER) class TempPage(Page): def __init__(self, *args, **kwargs): Page.__init__(self, *args, **kwargs) temp_page_lbl = tk.Label(text="stuff and things") class CycleTimesPage(Page): def __init__(self, *args, **kwargs): Page.__init__(self, *args, **kwargs) cycle_times_lbl = tk.Label(frame.interior, font=page_title_font, text='Cycle Times') cycle_times_lbl.grid(row=0, column=0, columnspan=4) reset_pages_btn = tk.Button(command=main.resetPages) # here is reset pages button reset_pages_btn.grid(column=0, row=1) class OverridesPage(Page): def __init__(self, *args, **kwargs): Page.__init__(self, *args, **kwargs) override_page_lbl = tk.Label(master=self, text='Sensor Overrides') override_page_lbl.grid(row=0, column=0, columnspan=2) class MainView(tk.Frame): def __init__(self, *args, **kwargs): tk.Frame.__init__(self, *args, **kwargs) main_bg = "#ffe499" self.container = tk.Frame(self) self.container.pack(side="left", fill="both", expand=True) self.p1 = MainPage(self.container) self.p2 = FunctionsPage(self.container) self.p3 = TempPage(self.container) self.p4 = CycleTimesPage(self.container) self.p5 = OverridesPage(self.container) self.p1.configure(bg=main_bg) self.p2.configure(bg=main_bg) self.p3.configure(bg=main_bg) self.p4.configure(bg=main_bg) self.p5.configure(bg=main_bg) buttonframe = tk.Frame(self) buttonframe.pack(side="right", fill="y", expand=False, anchor=tk.E) self.p1.place(in_=self.container, x=0, y=0, relwidth=1, relheight=1) self.p2.place(in_=self.container, x=0, y=0, relwidth=1, relheight=1) self.p3.place(in_=self.container, x=0, y=0, relwidth=1, relheight=1) self.p4.place(in_=self.container, x=0, y=0, relwidth=1, relheight=1) self.p5.place(in_=self.container, x=0, y=0, relwidth=1, relheight=1) self.v = tk.IntVar(value=1) radio_bg_select = "#ffae61" radio_bg = "#ffead6" nav_btn_width = 128 nav_btn_font = font.Font(size=12, weight='bold') self.r1 = tk.Radiobutton(buttonframe, text="Home", font=nav_btn_font, bg=radio_bg, selectcolor=radio_bg_select, bd=3, indicatoron=0, width=nav_btn_width, height=80, variable=self.v, command=self.p1.lift, value=1).pack(anchor=tk.E, expand=True, fill="both") self.r2 = tk.Radiobutton(buttonframe, text="Functions", font=nav_btn_font, bg=radio_bg, selectcolor=radio_bg_select, bd=3, indicatoron=0, width=nav_btn_width, variable=self.v, command=self.p2.lift, value=2).pack(anchor=tk.E, expand=True, fill="both") self.r3 = tk.Radiobutton(buttonframe, text="Temperature", font=nav_btn_font, bg=radio_bg, selectcolor=radio_bg_select, bd=3, indicatoron=0, width=nav_btn_width, variable=self.v, command=self.p3.lift, value=3).pack(anchor=tk.E, expand=True, fill="both") self.r4 = tk.Radiobutton(buttonframe, text="Cycle Times", font=nav_btn_font, bg=radio_bg, selectcolor=radio_bg_select, bd=3, indicatoron=0, width=nav_btn_width, variable=self.v, command=self.p4.lift, value=4).pack(anchor=tk.E, expand=True, fill="both") self.r5 = tk.Radiobutton(buttonframe, text="Overrides", font=nav_btn_font, bg=radio_bg, selectcolor=radio_bg_select, bd=3, indicatoron=0, width=nav_btn_width, variable=self.v, command=self.p5.lift, value=5).pack(anchor=tk.E, expand=True, fill="both") self.p1.show() def resetPages(self): main_bg = "#ffe499" self.p3.destroy() self.p4.destroy() self.p5.destroy() self.p3 = TempPage(self.container) self.p4 = CycleTimesPage(self.container) self.p5 = OverridesPage(self.container) self.p3.place(in_=self.container, x=0, y=0, relwidth=1, relheight=1) self.p4.place(in_=self.container, x=0, y=0, relwidth=1, relheight=1) self.p5.place(in_=self.container, x=0, y=0, relwidth=1, relheight=1) self.p3.configure(bg=main_bg) self.p4.configure(bg=main_bg) self.p5.configure(bg=main_bg) self.p4.show() # to bring user back to page p4 where the reset button was located if __name__ == "__main__": root = tk.Tk() main = MainView(root) main.pack(side="top", fill="both", expand=True) mainP = main.p1 root.wm_geometry("1024x600") root.configure(bg="#ffe499") root.mainloop()Any help is appreciated.