Harry Tong Harry Tong - 9 months ago 23
Python Question

Edit Window with TKinter

How do I change the geometry of the window and label when I have multiple frames?

Without frames, my code would be:

nGui = Tk()
nGui.geometry("500x500")


But I'm unsure of what 'nGui' is in below code (my whole code as requested). Therefore when this is run it comes up as a very small window. I think it might be 'tk.Tk' but when i tried to edit it, it just made a new window.

class DietBuddy(tk.Tk):

def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)


# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
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, PageOne, Diet_Finder):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame

# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")

self.show_frame("StartPage")

def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()


class StartPage(tk.Frame):

def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="HazaTea Productions", font=TITLE_FONT)
label.place(relx=.5, rely=.5, anchor="center")

time.sleep(2)
button = tk.Button(self, text="Continue",
command=lambda: controller.show_frame("PageOne"))
button.place(relx=.5, rely=.6, anchor="center")


class PageOne(tk.Frame):

def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is page 1", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Find Diet",
command=lambda: controller.show_frame("Diet_Finder"))
button.pack()


class Diet_Finder(tk.Frame):

def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="Diet Finder", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)


button = tk.Button(self, text="Find my Diet!",
command=lambda: controller.show_frame("StartPage"))
button.pack()


if __name__ == "__main__":
app = DietBuddy()
app.mainloop()


Thank you in advance - if this is in the wrong place, please have mercy and tell me I'm new to this site.

Answer Source

DietBuddy class subclassed Tk. So call geometry method against the DietBuddy instance:

if __name__ == "__main__":
    app = DietBuddy()
    app.geometry('500x500')  # <---
    app.mainloop()