Before I post my code, this is not all of it but what I feel is relevant to my problem. The first class is run when the user clicks a button, so the class contents (the frame) is displayed. The handler for my frames is:
class Begin(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# Creating the initial frame
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 = {}
for F in (LoginScreen, RegisterWindow, RevisionTopics, dataRep, reviseTen, FrameTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
page_name = LoginScreen.__name__
self.frames[page_name] = frame
self.show_frame(LoginScreen) # Shows the page currently being interacted
Now, this is the frame which has the important function, 'start', which I need to run in my second frame.
First frame:
class reviseTen(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.startButton = tk.Button(self, text="Click here to start revision session", command = self.start)
self.optionOne = tk.Button(self, text="Option One")
self.optionTwo = tk.Button(self, text="Option Two")
self.optionThree = tk.Button(self, text="Option Three")
self.optionFour = tk.Button(self, text="Option Four")
self.proceedButton = tk.Button(self, text="Proceed to next question", command=lambda: controller.show_frame(FrameTwo))
self.question = tk.Label(self, text="What is the definition of: ")
self.startButton.grid(row=0, column=0)
def start(self): #This is what I wanna use in my second frame
firstTime = True
while firstTime:
self.startButton.destroy()
firstTime = False
words = self.makeDict()
initialListOne = ['Integer', 'Natural number', 'Rational numbers', 'Irrational numbers', 'Real numbers', 'Ordinal numbers', 'Binary', 'Hexadecimal']
listOne = []
for i in initialListOne:
listOne.append(words[i])
initialListTwo = ['Denary to Hex', 'Binary to Hex', 'ASCII', 'Unicode', 'Overflow error', 'Twos complement', 'Bitmapped graphics', 'Resolution']
listTwo = []
for i in initialListTwo:
listTwo.append(words[i])
initialListThree = [ 'Bit Colour Depth', 'Metadata', 'Sample resolution', 'Sample Rate', 'Audio file size', 'Nyquist Theorem', 'MIDI', 'Lossy Compression']
listThree = []
for i in initialListThree:
listThree.append(words[i])
initialListFour = ['Lossless Compression', 'Run Length Encoding', 'Dictionary compression', 'Encryption', 'Encryption steps', 'Caesar cipher',
'Brute force attack', 'Frequency analysis', 'Vernam cipher', 'One-Time Pad']
listFour = []
for i in initialListFour:
listFour.append(words[i])
listOfKeys = [] # Holds the keywords
listOfValues = [] # Holds the definitions
for key in words:
listOfKeys.append(key)
listOfValues.append(words[key])
keywordPosition = random.randint(1, len(listOfKeys)-1)
QKeyword = listOfKeys[keywordPosition]
QDef = listOfValues[keywordPosition]
self.question.grid(row=0, column=0)
self.optionOne.grid(row=1, column=0)
self.optionTwo.grid(row=2, column=0)
self.optionThree.grid(row=3, column=0)
self.optionFour.grid(row=4, column=0)
self.proceedButton.grid(row=5, column=0)
self.question.config(text=("What is the definition of: "+ QKeyword))
randomOne = random.randint(0, len(listOne))
randomTwo = random.randint(0, len(listTwo))
randomThree = random.randint(0, len(listThree))
randomFour = random.randint(0, len(listFour))
selectButton = random.randint(1,4)
if selectButton == 1:
self.optionOne.config(text=QDef)
self.optionTwo.config(text=listOfValues[randomTwo])
self.optionThree.config(text=listOfValues[randomThree])
self.optionFour.config(text=listOfValues[randomFour])
elif selectButton == 2:
self.optionOne.config(text=listOfValues[randomOne])
self.optionTwo.config(text=QDef)
self.optionThree.config(text=listOfValues[randomThree])
self.optionFour.config(text=listOfValues[randomFour])
elif selectButton == 3:
self.optionOne.config(text=listOfValues[randomOne])
self.optionTwo.config(text=listOfValues[randomTwo])
self.optionThree.config(text=QDef)
self.optionFour.config(text=listOfValues[randomFour])
elif selectButton == 4:
self.optionOne.config(text=listOfValues[randomOne])
self.optionTwo.config(text=listOfValues[randomTwo])
self.optionThree.config(text=listOfValues[randomThree])
self.optionFour.config(text=QDef)
def makeDict(self):
dict = {}
con = sql.connect("dataRep.db")
cur = con.cursor()
for column in cur.execute("SELECT keyword, definition FROM words"):
variable = column[0]
variable2 = column[1]
dict[variable] = variable2
return dict
Second Frame:
class FrameTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.optionOne = tk.Button(self, text="Option One")
self.optionTwo = tk.Button(self, text="Option Two")
self.optionThree = tk.Button(self, text="Option Three")
self.optionFour = tk.Button(self, text="Option Four")
self.question = tk.Label(self, text="What is the definition of: ")
# TRIED THIS - screen stays blank (but start method has code that makes the widgets appear
self.start(controller)
def start(self, controller):
self.reviseTen = reviseTen(self, controller)
I need start to complete the exact same functions as it did on the frame 'reviseTen', the function is running but just not doing anything to my second frame. It is just blank. The code for positioning the elements (so they show up) is meant to run after running start...
Is it to do with the way I called it?
Many thanks for any help.