简体   繁体   中英

Expand Tkinter Frame

I have a basic application using tkinter with python (3.5). I want the application to be run in full screen, and have multiple windows to switch through. So far this is what I have.

import tkinter as tk

class Window(tk.Tk):

def __init__(self):
    tk.Tk.__init__(self)

    self.title("Movie Kiosk")
    self.attributes("-fullscreen", True)
    self.resizable(width=False, height=False)

    container = tk.Frame(self)
    container.pack(side="top", fill="both", expand=1)

    self.frames = {}

    for f in (StartPage, PageOne):
        frame = f(container, self)
        self.frames[f] = frame
        frame.grid(row=0, column=0, sticky="nsew")

    self.show_frame(StartPage)

def show_frame(self, cont):
    frame = self.frames[cont]
    frame.tkraise()

class StartPage(tk.Frame):

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    label = tk.Label(self, text="Main Page", font=("Verdana",48))
    label.place(relx=0.5, y=0, anchor=tk.N)
    button = tk.Button(self, text="Go to page 1",
                       command=lambda: controller.show_frame(PageOne))
    button.place(relx=1, rely=1, anchor=tk.SE)
    exitButton = tk.Button(self, text="Close Program", command=exit)
    exitButton.place(relx=0, rely=1, anchor=tk.SW)

class PageOne(tk.Frame):

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    label = tk.Label(self, text="Page 1")
    label.pack()
    button = tk.Button(self, text="Back to home",
                       command=lambda: controller.show_frame(StartPage))
    button.pack()


app = Window()
app.mainloop()

When I run the application, the program loads in full screen mode, however the frame and all its widgets are packed tightly in the top left corner of the screen. Not sure why this is happening, I have messed around changing properties of my my "app" and my frames. If someone could tell me whats wrong or direct me to a place where I can find an answer it would be very much so appreciated. Thanks.

Not sure why this fixes it...

container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)

Adding this code after packing the container fixes the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM