Similar to another question I've asked, but feel this is probably the best way to go about it after much consideration.
What I want is for the user to select one of the two radio buttons, hit the "Next Page" button and be brought to just one frame. Within that frame class there will be two labels, but depending on what radio button was selected on the previous frame, I want one of the two labels to appear
here is my code -
import Tkinter as tk
class MainApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# the main container that holds all the frames
container = tk.Frame(self)
container.pack(side = "top", fill = "both", expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0,weight = 1)
self.frames = {}
# adding frames to the dictionary
for F in (Page1,Page2):
frame = F(container,self)
self.frames[F] = frame
frame.grid(row = 0, column = 0, sticky = "w")
self.show_frame(Page1)
def show_frame(self,page_name):
#SHOWS A FRAME WITH THE GIVEN NAME
for frame in self.frames.values():
frame.grid_remove()
frame = self.frames[page_name]
frame.grid()
#STACKING THE FRAMES
#frame = self.frames[cont]
#frame.tkraise()
class Page1(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
lbl1 = tk.Label(self,text = "Yes",font =("Helvetica",12,"bold"))
lbl1.grid(row=1,sticky="W")
lbl2 = tk.Label(self,text = "No",font =("Helvetica",12,"bold"))
lbl2.grid(row=1,column=1,sticky="W")
btn1 = tk.Button(self, text="next page", font=('MS', 24, 'bold'))
btn1.grid(row=3,column = 0,columnspan=1)
self.var1 = tk.BooleanVar()
rButton1 = tk.Radiobutton(self,variable = self.var1,value=True)
rButton1.grid(row=2,sticky = "W")
rButton2 = tk.Radiobutton(self,variable = self.var1,value=False)
rButton2.grid(row=2,column=1,sticky = "W")
btn1['command']= lambda: controller.show_frame(Page2)
class Page2(tk.Frame,Page1):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
self.var1 = Page1.var1
lbl = tk.Label(self,text="This is reccomendation 2",font=("Helvetica",12,"bold"))
lbl.pack_forget()
lbl1 = tk.Label(self,text="This is reccomendation 3",font=("Helvetica",12,"bold"))
lbl1.pack_forget()
# if self.var1.get() == 0:
# lbl.pack()
# else:
# lbl1.pack()
So 2 things, firstly, I'm assuming Page2 has to inherit Page1's self.var1, which I attempted doing with this -
class Page2(tk.Frame,Page1):
but only receive this error message -
self.var1 = Page1.var1
AttributeError: class Page1 has no attribute 'var1'
which, i find odd, because page 1 DOES have var1?! And, secondly, I'm not even sure if the pack_forget() method is the correct way of achieving this?
update 4/4/16
After a bit of digging around i discovered the StringVar variable.
so after implementing -
def get_page(self,className):
for page in self.frames.values():
if str(page.__class__.__name__) == className:
return page
return None
in the controller, I can now access page 1's self.var1
I have updated my radio buttons -
self.var1 = tk.StringVar()
rButton1 = tk.Radiobutton(self,variable = self.var1,value="reco 1"
,command = "this is reco 1")
rButton1.grid(row=2,sticky = "W")
rButton2 = tk.Radiobutton(self,variable = self.var1,value="reco 2"
,command = "this is reco 2")
rButton2.grid(row=2,column=1,sticky = "W")
from what I gathered about the StringVar, based on which radio button is selected, the string associated with the radio button gets stored in the String Var? could be entirely wrong... anyway, I have now updated page 2 -
class Page2(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
self.controller = controller
page_one = self.controller.get_page("Page1")
# Access to Page1's self.var1
reco = page_one.var1.get()
lbl = tk.Label(self,text= reco,font=("Helvetica",12,"bold"))
lbl.pack()
The program runs, but frustratingly it doesn't run as I want it, as soon as the next page button gets pressed nothing appears on the second frame. Am I heading in the right direction with this thought process or is there another way to do this? either way the text has to appear on the next frame.