[英]Expand Tkinter Frame
我有一個使用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()
當我運行該應用程序時,該程序以全屏模式加載,但是該框架及其所有小部件都緊密包裝在屏幕的左上角。 不知道為什么會這樣,我已經改變了我的“應用”和框架的屬性。 如果有人可以告訴我出了什么問題或將我引導到可以找到答案的地方,那將非常感謝。 謝謝。
不確定為什么可以解決這個問題...
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
包裝容器后添加此代碼可解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.