I'm a newbie to python. Would you be able to help me find a way to pass a value onto the CommonPage class for the 'mypage' variable, rather than it being hardcoded onto there. I plan for both pages A and B to have the same format and structure, but different values for variables (such as the title as in this example). This is why I would like to use only one class for both pages A and B, rather than two separate classes with similar code but different values for the variables. Is this possible?
thank you! any help would be much appreciated
from tkinter import *
import tkinter.ttk as ttk
class MyApp(Tk):
def __init__(self):
Tk.__init__(self)
container = ttk.Frame(self)
container.pack(side="top", fill="both", expand=True)
self.frames = {}
for F in (HomePage, CommonPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky='NSEW')
self.show_frame(HomePage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class HomePage(ttk.Frame):
def __init__(self, parent, controller):
self.controller = controller
ttk.Frame.__init__(self, parent)
# create all of the main containers
top_frame = Frame(self, bg='blue', width=355, height=50)
mid_frame = Frame(self, bg='MediumPurple4', width=350, height=50)
bottom_frame = Frame(self, bg='#a0ccda', width=350, height=100)
# to go inside top_frame
titleLabel = Label(top_frame, font=('Arial', 23, 'bold'), text="My App",
bd=5, fg='snow',bg='blue', width=19, height=1)
titleLabel.grid(row=0, column=0, sticky=N)
top_frame.pack()
# to go inside mid_frame
titleLabel = Label(mid_frame, font=('Helvetica', 15, 'italic'), text="",
bd=5, fg='snow',bg='black', width=30, height=1)
titleLabel.grid(row=1, column=0, sticky=N)
mid_frame.pack()
# to go inside bottom_frame
titleLabel = Label(bottom_frame, font=('Helvetica', 15, 'italic'), text="Select Button Page Below", bd=5, fg='snow',bg='gray', width=30)
titleLabel.grid(row=1, column=0, sticky=N)
# button to CommonPage
# need to pass the Page Button selected to the CommonPage class
btnPageA = Button(bottom_frame, text="Page A", font="Arial 20",
command=lambda: [self.controller.show_frame(CommonPage)],
bg="#a0ccda", width=22)
btnPageA.grid(row=2, column=0, sticky=W)
btnPageB = Button(bottom_frame, text="Page B", font="Arial 20",
command=lambda: [self.controller.show_frame(CommonPage)],
bg="#a0ccda", width=22)
btnPageB.grid(row=3, column=0, sticky=W)
bottom_frame.pack()
class CommonPage(ttk.Frame):
def __init__(self, parent, controller):
self.controller = controller
ttk.Frame.__init__(self, parent)
top_frame = Frame(self, bg='blue', width=355, height=50)
bottom_frame = Frame(self, bg='#a0ccda', width=350, height=0)
btnHomePage = ttk.Button(top_frame, text='Home Page',
command=lambda: self.controller.show_frame(HomePage))
btnHomePage.grid(row=0, sticky=W)
## hardcoded as shown, this needs to be passed when a page button is selected
SelectedPage = "Page B"
if SelectedPage == "Page A":
mypage = "Page A"
else:
mypage = "Page B"
##
titleLabel = Label(top_frame, font=('Arial', 23, 'bold'), text=mypage, bd=5, fg='snow',bg='blue', width=19, height=1)
titleLabel.grid(row=1, column=0, sticky=S)
top_frame.pack()
bottom_frame.pack()
if __name__ == '__main__':
app = MyApp()
app.title('Mobile App')
app.geometry('{}x{}'.format(355, 230))