Reputation: 312
I am trying to make a fairly simple GUI with multiple windows. I have my windows built as classes just with a label in each one for now. I can't seem to figure out why when I run my program it packs all of the labels on the "StartPage" and none of the other windows have anything in them. It could be that I have my classes configured incorrectly?
import tkinter as tk
class application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
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 (StartPage, WeeklyBudget, LongtermSavings, Investments):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.ShowFrame(StartPage)
def ShowFrame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
start_label = tk.Label(self, text = 'Welcome to Finance Track!')
start_label.pack()
week_btn = tk.Button(self, text = 'Weekly Budgeting', command =lambda: controller.ShowFrame(WeeklyBudget))
savings_btn = tk.Button(self, text = 'Longterm Savings', command = lambda: controller.ShowFrame(LongtermSavings))
invest_btn = tk.Button(self, text = 'Investments', command = lambda: controller.ShowFrame(Investments))
week_btn.pack(pady = 10, padx = 10)
savings_btn.pack(pady = 10, padx = 10)
invest_btn.pack(pady = 10, padx = 10)
class WeeklyBudget(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(text = 'Welcome to your Weekly Budget')
label.pack()
add_btn = tk.Button(text = 'add new week')
add_btn.pack()
class LongtermSavings(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(text = 'Welcome to your Longterm Savings')
label.pack()
class Investments(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(text = 'Welcome to your Investments')
label.pack()
app = application()
app.mainloop()
The current outcome as I described earlier is just one window with all the labels and all the buttons in it.
Upvotes: 1
Views: 202
Reputation: 815
As jasonharper mention you aren't defining the parents (aka master) of many widgets.
class Investments(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(text = 'Welcome to your Investments')
label.pack()
Using this Investments
class for example, your label will be given the window as it's parents by default, to set it's parent to the newly created frame, just do the following:
class Investments(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text = 'Welcome to your Investments')
label.pack()
Upvotes: 2