调整主要 window 中的帧大小以适应 window
Resizing frames in main window to fit the window
我的菜单框架在左侧,主容器在右侧,但是当我启动它时,我只在主容器内的一个小盒子里看到它们 window。
如何使框架适合 window,无论它是什么尺寸?
代码:
class GUI(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title("GUI Project")
# self.resizable(0, 0)
menu = tk.Frame(self, relief="solid")
container = tk.Frame(self, relief="ridge")
menu.grid(row=0, column=0, rowspan=4, sticky="nsew")
container.grid(row=0, column=1, sticky="nsew")
menu.grid_rowconfigure(0, weight=1)
container.rowconfigure(0, weight=0)
container.columnconfigure(1, weight=0)
self.frames = ["Menu", "FutureFeature2", "TestPing", "FutureFeature3", "FutureFeature"]
self.frames[0] = Menu(parent=menu, controller=self)
self.frames[1] = FutureFeature2(parent=container, controller=self)
self.frames[2] = TestPing(parent=container, controller=self)
self.frames[3] = FutureFeature3(parent=container, controller=self)
self.frames[4] = FutureFeature(parent=container, controller=self)
self.frames[0].grid(row=0, column=0, sticky="nsew")
self.frames[1].grid(row=0, column=0, sticky="nsew")
self.frames[2].grid(row=0, column=0, sticky="nsew")
self.frames[3].grid(row=0, column=0, sticky="nsew")
self.frames[4].grid(row=0, column=0, sticky="nsew")
self.show_frame(1)
def show_frame(self, page_name):
frame = self.frames[page_name]
print(frame)
frame.tkraise()
frame.grid(row=0, column=0, columnspan=5, rowspan=5, sticky="nsew")
class Menu(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
button1 = tk.Button(self, text="Ping Test", bg="royalblue2",
command=lambda: controller.show_frame(2))
button2 = tk.Button(self, text="FutureFeature", bg="dark violet",
command=lambda: controller.show_frame(4))
buttun3 = tk.Button(self, text="FutureFeature", bg="pale goldenrod",
command=lambda: controller.show_frame(1))
button4 = tk.Button(self, text="Quit", bg="gray40",
command=lambda: self.terminate())
button1.pack(fill="both", expand=True)
button2.pack(fill="both", expand=True)
buttun3.pack(fill="both", expand=True)
button4.pack(fill="both", expand=True)
def terminate(self):
path = fr'c:/users/{os.getlogin()}/desktop/Gui-Skeleton'
try:
os.rmdir(path)
except OSError as err:
print(f"Error Deleting tmp folder! {err}")
exit()
if __name__ == "__main__":
path = fr'c:/users/{os.getlogin()}/desktop/Gui-Skeleton'
try:
if os.path.isdir(path):
pass
else:
os.mkdir(path)
except OSError as err:
print(f"[!] Operation failed! {err}")
app = GUI()
app.geometry("800x600")
app.mainloop()
................................................ ..................................................... ..................................................... ..................................................... ..................................................... .....................
您询问的具体问题是由于您使用 grid
将 menu
和 container
放入根 window 但您没有' 给根 window 中的任何行或列一个权重。因此,menu
和 grid
都将根据需要使用尽可能少的 space。
一般的经验法则是,当你使用grid
时,你应该至少给一行和一列正权重,这样就没有未使用的space。您在根 window 中没有做到这一点。有关网格权重的更多信息,请参阅
解决方案很简单:确保您在根中至少给一行和一列正权重 window:
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(1, weight=1)
也就是说,我建议对 menu
和 container
使用 pack
,因为它们是直接位于根 window 中的唯一小部件。在行或列中布置小部件时,pack
通常优于 grid
,如果没有其他原因,您不必采取额外的分配权重的步骤。
menu.pack(side="left", fill="y")
container.pack(side="right", fill="both", expand=True)
我的菜单框架在左侧,主容器在右侧,但是当我启动它时,我只在主容器内的一个小盒子里看到它们 window。
如何使框架适合 window,无论它是什么尺寸?
代码:
class GUI(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title("GUI Project")
# self.resizable(0, 0)
menu = tk.Frame(self, relief="solid")
container = tk.Frame(self, relief="ridge")
menu.grid(row=0, column=0, rowspan=4, sticky="nsew")
container.grid(row=0, column=1, sticky="nsew")
menu.grid_rowconfigure(0, weight=1)
container.rowconfigure(0, weight=0)
container.columnconfigure(1, weight=0)
self.frames = ["Menu", "FutureFeature2", "TestPing", "FutureFeature3", "FutureFeature"]
self.frames[0] = Menu(parent=menu, controller=self)
self.frames[1] = FutureFeature2(parent=container, controller=self)
self.frames[2] = TestPing(parent=container, controller=self)
self.frames[3] = FutureFeature3(parent=container, controller=self)
self.frames[4] = FutureFeature(parent=container, controller=self)
self.frames[0].grid(row=0, column=0, sticky="nsew")
self.frames[1].grid(row=0, column=0, sticky="nsew")
self.frames[2].grid(row=0, column=0, sticky="nsew")
self.frames[3].grid(row=0, column=0, sticky="nsew")
self.frames[4].grid(row=0, column=0, sticky="nsew")
self.show_frame(1)
def show_frame(self, page_name):
frame = self.frames[page_name]
print(frame)
frame.tkraise()
frame.grid(row=0, column=0, columnspan=5, rowspan=5, sticky="nsew")
class Menu(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
button1 = tk.Button(self, text="Ping Test", bg="royalblue2",
command=lambda: controller.show_frame(2))
button2 = tk.Button(self, text="FutureFeature", bg="dark violet",
command=lambda: controller.show_frame(4))
buttun3 = tk.Button(self, text="FutureFeature", bg="pale goldenrod",
command=lambda: controller.show_frame(1))
button4 = tk.Button(self, text="Quit", bg="gray40",
command=lambda: self.terminate())
button1.pack(fill="both", expand=True)
button2.pack(fill="both", expand=True)
buttun3.pack(fill="both", expand=True)
button4.pack(fill="both", expand=True)
def terminate(self):
path = fr'c:/users/{os.getlogin()}/desktop/Gui-Skeleton'
try:
os.rmdir(path)
except OSError as err:
print(f"Error Deleting tmp folder! {err}")
exit()
if __name__ == "__main__":
path = fr'c:/users/{os.getlogin()}/desktop/Gui-Skeleton'
try:
if os.path.isdir(path):
pass
else:
os.mkdir(path)
except OSError as err:
print(f"[!] Operation failed! {err}")
app = GUI()
app.geometry("800x600")
app.mainloop()
................................................ ..................................................... ..................................................... ..................................................... ..................................................... .....................
您询问的具体问题是由于您使用 grid
将 menu
和 container
放入根 window 但您没有' 给根 window 中的任何行或列一个权重。因此,menu
和 grid
都将根据需要使用尽可能少的 space。
一般的经验法则是,当你使用grid
时,你应该至少给一行和一列正权重,这样就没有未使用的space。您在根 window 中没有做到这一点。有关网格权重的更多信息,请参阅
解决方案很简单:确保您在根中至少给一行和一列正权重 window:
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(1, weight=1)
也就是说,我建议对 menu
和 container
使用 pack
,因为它们是直接位于根 window 中的唯一小部件。在行或列中布置小部件时,pack
通常优于 grid
,如果没有其他原因,您不必采取额外的分配权重的步骤。
menu.pack(side="left", fill="y")
container.pack(side="right", fill="both", expand=True)