[英]Expand Tkinter Frame
I have a basic application using tkinter with python (3.5). 我有一个使用tkinter和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. 包装容器后添加此代码可解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.