所以我遇到了一些我绝对无法弄清楚并且需要帮助的事情。我有两个按钮,它们都会将您发送到下一帧,它们工作得很好,但是当我调整窗口大小时,它们保持相同的大小并停留在左上角,我希望它们将自己放置在屏幕的中心。如果有一个关于它们成长的例子,那也将受到赞赏,但不是必需的。如果可能的话,我也想坚持使用网格。 我尝试了 columnconfigure 并将框架放置在另一个框架中,但我所做的一切似乎都不起作用。奇怪的是,如果我用 self 替换容器并将 self 放在按钮上,它可以工作,但命令不起作用,有人可以向我解释这一点,或者给我一个关于我做错了什么的好例子吗?
代码示例:
import tkinter as tk
class MYapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.grid(row = 0,column = 0, sticky = "nsew")
container.rowconfigure(0, weight = 1)
container.columnconfigure(0, weight = 1)
container.columnconfigure(1, weight = 1)
self.frames = {}
for FRAME in (N1, N2):
frame = FRAME(container, self)
self.frames[FRAME] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
self.show_frame(N1)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class N1(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
a = tk.Button(self, text = "N1", command = lambda: controller.show_frame(N2))
a.grid(row = 0, column = 0, sticky = "nsew")
c = tk.Button(self, text = "N1", command = lambda: controller.show_frame(N2))
c.grid(row = 0, column = 1, sticky = "nsew")
class N2(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
b = tk.Button(self, text = "N2")
b.grid(row = 0, column = 0, sticky = "nsew")
if __name__ == "__main__":
app = MYapp()
app.mainloop()
已解决: 感谢布莱恩。 这是更新后的代码,其中包含答案。
import tkinter as tk
class MYapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(fill="both", expand=True)
container.rowconfigure(0, weight = 1)
container.columnconfigure(0, weight = 1)
self.frames = {}
for FRAME in (N1, N2):
frame = FRAME(container, self)
self.frames[FRAME] = frame
frame.grid(row = 0, column = 0, sticky = "nsew")
frame.rowconfigure(0, weight = 1)
frame.columnconfigure(0, weight = 1)
frame.columnconfigure(1, weight = 1)
self.show_frame(N1)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class N1(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
a = tk.Button(self, text = "N1", command = lambda: controller.show_frame(N2))
a.grid(row = 0, column = 0, sticky = "nsew")
c = tk.Button(self, text = "N1", command = lambda: controller.show_frame(N2))
c.grid(row = 0, column = 1, sticky = "nsew")
class N2(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
b = tk.Button(self, text = "N2")
b.grid(row = 0, column = 0, sticky = "nsew")
if __name__ == "__main__":
app = MYapp()
app.mainloop()
最佳答案
您没有了解首选行为是什么,只是知道它所做的事情是错误的。我不知道这个建议是否能达到你的要求。
根据经验,当您使用grid
时,您应该始终为包含正在管理的小部件的父级赋予至少一行和一列的权重。当您对容器中的小部件执行此操作时,您不会对根窗口执行此操作。因此,容器内的小部件可能会正确调整大小,但您不会看到它,因为容器本身没有调整大小。
如果您只想将一个小部件放入另一个小部件中,我建议使用 pack
因为您可以在一行代码中完成所有操作。因此,我建议对容器本身使用 pack
而不是 grid
:
container.pack(fill="both", expand=True)
此外,由于您的容器似乎只有一行和一列,您可能不想为第 1 列指定权重。
最后,您还使用 grid
来管理每个页面内的小部件(N1
、N2
),但您又忘记了为这些框架内的任何行或列赋予权重。
关于python-3.x - tkinter 网格不根据窗口大小调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46898749/