I am using python 2.6 and am new to using Tkinter. I am trying to create a basic template GUI for a Toplevel Frame with multiple frames inside of it. I ran into an issue where I am not able to clear my frames properly. I think I have an inheritance wrong inside of my frame classes but I am not able to fix it without some help. I have not added anything to the commands beside updating the GUI in order to figure out my problem. Thanks.
from Tkinter import *
class F1(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.test(master)
def test(self, master):
self.bttn = Button(master.top)
self.bttn["text"] = "Change Frame"
self.bttn["command"] = master.getF2
self.bttn.grid()
class F2(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.test(master)
def test(self, master):
self.lbl = Label(master.top, text = "You did It!")
self.lbl.grid()
self.bttn = Button(master.top)
self.bttn["text"] = "Change Frame Again?"
self.bttn["command"] = master.getF3
self.bttn.grid()
class F3(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.test(master)
def test(self, master):
self.lbl = Label(master.top, text = "You did It Again!")
self.lbl.grid()
class App(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
self.vartext = StringVar()
self.current_frame = ""
self.create_window()
def create_window(self):
"""Main GUI"""
self.bttn1 = Button(self)
self.bttn1["text"] = "Add Food"
self.bttn1["command"] = self.top_window
self.bttn1.grid()
def top_window(self):
self.top = Toplevel()
self.top.title("Add Food to Log")
self.head_container = Frame(self.top)
self.head_container.grid(row = 1)
self.backbttn = Button(self.head_container, text = "Back Frame", command = self.changeFrame).grid(row = 1, column = 0)
self.head_lbl = Label(self.head_container, textvariable = self.vartext).grid(row = 1, column = 1)
self.exitbttn = Button(self.head_container, text = "Exit", command = self.top.destroy).grid(row = 1, column = 2)
self.container = Frame(self.top)
self.lbl = Label(self.container, text = "Test").grid()
self.container.grid(row = 2)
self.getF1()
def changeFrame(self):
if self.current_frame == "F2":
self.getF1()
if self.current_frame == "F3":
self.getF2()
def getF1(self):
self.container.grid_forget()
self.current_frame = "F1"
print self.current_frame
self.container = F1(self)
self.vartext.set("Frame 1")
self.container.grid()
def getF2(self):
self.current_frame = "F2"
print self.current_frame
self.container.grid_forget()
self.container = F2(self)
self.vartext.set("Frame 2")
self.container.grid()
def getF3(self):
self.current_frame = "F3"
print self.current_frame
self.container.grid_forget()
self.container = F3(self)
self.vartext.set("Frame 3")
self.container.grid()
# Main Code
root = Tk()
root.title("GUI Test")
app = App(root)
root.mainloop()