在 tkinter 中安排框架的大小
Arranging size of a frame in tkinter
我正在为我的应用制作一个简单的工具箱。我使用了class方法,它继承了Frame
作为它的超class。在我的主文件中,我导入了这个 class.
它将是一个主要的 window,所有小部件都将包含在其中。但是有一个问题,这里是源代码:
from tkinter import *
class ToolBox(Frame):
def __init__(self, master=None,
width=100, height=300):
Frame.__init__(self, master,
width=100, height=300)
self.pack()
Button(self, text="B").grid(row=0, sticky=(N,E,W,S))
Button(self, text="B").grid(row=0, column=1, sticky=(N,E,W,S))
Button(self, text="B").grid(row=1, column=0,sticky=(N,E,W,S))
Button(self, text="B").grid(row=1, column=1, sticky=(N,E,W,S))
Button(self, text="B").grid(row=2, column=0, sticky=(N,E,W,S))
Button(self, text="B").grid(row=2, column=1, sticky=(N,E,W,S))
我在这里导入这个:
from tkinter import *
import toolbox as tl
root = Tk()
frame = Frame(root, width=400, height=400)
frame.pack()
tl.ToolBox(frame).pack()
root.mainloop()
Main window,也就是拥有frame
的root
,其宽度和高度必须为400。但它出现在我工具箱的尺寸中。我希望工具箱位于主 window 中。我该如何解决这个问题?
您可以使用 geometry
方法强制根 window 具有特定的维度。
root = Tk()
root.geometry("400x400")
如果您还希望按钮均匀伸展以填充整个根部window,您需要做两件事:
- 调用
rowconfigure
和 columnconfigure
设置根按钮的权重以及作为按钮父级的每个帧。
- 为您的根的每个按钮和框架指定粘性参数。
举个例子。我删除了您的 frame
框架,因为它似乎没有任何作用。工具箱毕竟已经是一个框架了,把一个框架放在一个框架里面没有多大意义。
from tkinter import *
class ToolBox(Frame):
def __init__(self, master=None,
width=100, height=300):
Frame.__init__(self, master,
width=width, height=height)
for i in range(2):
self.grid_columnconfigure(i, weight=1)
for j in range(3):
self.grid_rowconfigure(j, weight=1)
Button(self, text="B").grid(row=0, sticky=(N,E,W,S))
Button(self, text="B").grid(row=0, column=1, sticky=(N,E,W,S))
Button(self, text="B").grid(row=1, column=0,sticky=(N,E,W,S))
Button(self, text="B").grid(row=1, column=1, sticky=(N,E,W,S))
Button(self, text="B").grid(row=2, column=0, sticky=(N,E,W,S))
Button(self, text="B").grid(row=2, column=1, sticky=(N,E,W,S))
root = Tk()
root.geometry("400x400")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
ToolBox(root).grid(sticky="news")
root.mainloop()
现在你的根大小合适了,你的按钮伸展来填充它。
我正在为我的应用制作一个简单的工具箱。我使用了class方法,它继承了Frame
作为它的超class。在我的主文件中,我导入了这个 class.
它将是一个主要的 window,所有小部件都将包含在其中。但是有一个问题,这里是源代码:
from tkinter import *
class ToolBox(Frame):
def __init__(self, master=None,
width=100, height=300):
Frame.__init__(self, master,
width=100, height=300)
self.pack()
Button(self, text="B").grid(row=0, sticky=(N,E,W,S))
Button(self, text="B").grid(row=0, column=1, sticky=(N,E,W,S))
Button(self, text="B").grid(row=1, column=0,sticky=(N,E,W,S))
Button(self, text="B").grid(row=1, column=1, sticky=(N,E,W,S))
Button(self, text="B").grid(row=2, column=0, sticky=(N,E,W,S))
Button(self, text="B").grid(row=2, column=1, sticky=(N,E,W,S))
我在这里导入这个:
from tkinter import *
import toolbox as tl
root = Tk()
frame = Frame(root, width=400, height=400)
frame.pack()
tl.ToolBox(frame).pack()
root.mainloop()
Main window,也就是拥有frame
的root
,其宽度和高度必须为400。但它出现在我工具箱的尺寸中。我希望工具箱位于主 window 中。我该如何解决这个问题?
您可以使用 geometry
方法强制根 window 具有特定的维度。
root = Tk()
root.geometry("400x400")
如果您还希望按钮均匀伸展以填充整个根部window,您需要做两件事:
- 调用
rowconfigure
和columnconfigure
设置根按钮的权重以及作为按钮父级的每个帧。 - 为您的根的每个按钮和框架指定粘性参数。
举个例子。我删除了您的 frame
框架,因为它似乎没有任何作用。工具箱毕竟已经是一个框架了,把一个框架放在一个框架里面没有多大意义。
from tkinter import *
class ToolBox(Frame):
def __init__(self, master=None,
width=100, height=300):
Frame.__init__(self, master,
width=width, height=height)
for i in range(2):
self.grid_columnconfigure(i, weight=1)
for j in range(3):
self.grid_rowconfigure(j, weight=1)
Button(self, text="B").grid(row=0, sticky=(N,E,W,S))
Button(self, text="B").grid(row=0, column=1, sticky=(N,E,W,S))
Button(self, text="B").grid(row=1, column=0,sticky=(N,E,W,S))
Button(self, text="B").grid(row=1, column=1, sticky=(N,E,W,S))
Button(self, text="B").grid(row=2, column=0, sticky=(N,E,W,S))
Button(self, text="B").grid(row=2, column=1, sticky=(N,E,W,S))
root = Tk()
root.geometry("400x400")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
ToolBox(root).grid(sticky="news")
root.mainloop()
现在你的根大小合适了,你的按钮伸展来填充它。