展开Tkinter框架

时间:2016-02-08 00:50:38

标签: python tkinter

我有一个使用tkinter和python(3.5)的基本应用程序。我希望应用程序以全屏运行,并有多个窗口可以切换。到目前为止,这就是我所拥有的。

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()

当我运行应用程序时,程序以全屏模式加载,但框架及其所有小部件都紧紧地打包在屏幕的左上角。不知道为什么会发生这种情况,我已经搞乱了我的应用程序的属性#34;和我的帧。如果有人可以告诉我什么是错的,或者将我引导到一个我能找到答案的地方,那将非常感激。感谢。

1 个答案:

答案 0 :(得分:0)

不确定为什么会修复它...

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

打包容器后添加此代码可以解决问题。