Tkinter Class Structure (class Per Frame) Issue With Duplicating Widgets
Solution 1:
The point of using classes is to encapsulate a bunch of behavior as a single unit. An object shouldn't modify anything outside of itself. At least, not by simply creating the object -- you can have methods that can have side effects.
In my opinion, the proper way to create "pages" is to inherit from Frame
. All of the widgets that belong to the "page" must have the object itself as its parent. For example:
classPageOne(tk.Frame):def__init__(self, parent):
# use the __init__ of the superclass to create the actual frame
tk.Frame.__init__(self, parent)
# all other widgets use self (or some descendant of self)# as their parentself.label = tk.Label(self, ...)
self.button = tk.Button(self, ...)
...
Once done, you can treat instances of this class as if they were a single widget:
root = tk.Tk()
page1 = PageOne(root)
page1.pack(fill="both", expand=True)
You can also create a base Page
class, and have your actual pages inherit from it, if all of your pages have something in common (for example, a header or footer)
classPage(tk.Frame):def__init__(self, parent):
tk.Frame.__init__(self, parent)
<code common to all pages goes here>
classPageOne(Page):def__init__(self, parent):
# initialize the parent class
Page.__init__(self, parent)
<code unique to page one goes here>
Solution 2:
Your use of OOP is not very logical here. Your main program is in the class GUI_start. If your pages inherit from GUI_start, basically you create a whole new program with every page instance you create. You should instead inherit from Frame as Bryan Oakley has pointed our in the comments. Here is a somewhat repaired version of what you have posted. The original one by Bryan is still much better.
from Tkinter import *
classGUI_Start:
def__init__(self, master):
self.master = master
self.master.geometry('300x300')
self.master.grid_rowconfigure(0, weight=1)
self.master.grid_columnconfigure(0, weight=1)
self.win_colour = '#D2B48C'
self.current_page=0
self.pages = []
for i inrange(5):
page = Page(self.master,i+1)
page.grid(row=0,column=0,sticky='nsew')
self.pages.append(page)
for i inrange(2):
page = Page_diff(self.master,i+1)
page.grid(row=0,column=0,sticky='nsew')
self.pages.append(page)
self.pages[0].tkraise()
defNext_Page():
next_page_index = self.current_page+1if next_page_index >= len(self.pages):
next_page_index = 0print(next_page_index)
self.pages[next_page_index].tkraise()
self.current_page = next_page_index
page1_button = Button(self.master, text='Visit next Page',command = Next_Page)
page1_button.grid(row=1,column=0)
classPage(Frame):
def__init__(self,master,number):
super().__init__(master,bg='#D2B48C')
self.master = master
self.master.tkraise()
page1_label = Label(self, text='PAGE '+str(number))
page1_label.pack(fill=X,expand=True)
classPage_diff(Frame):
def__init__(self,master,number):
super().__init__(master)
self.master = master
self.master.tkraise()
page1_label = Label(self, text='I am different PAGE '+str(number))
page1_label.pack(fill=X)
root = Tk()
gui = GUI_Start(root)
root.mainloop()
Post a Comment for "Tkinter Class Structure (class Per Frame) Issue With Duplicating Widgets"