Tkinter:我不明白网格是如何工作的(试图创建同样大小的列)
Tkinter : I don't understand how the grid works (trying to create equally sized columns)
我的目标是在 tkinter 中创建 3 个大小相同的列,然后能够用我想要的任何东西填充它们。
我正在尝试做的类似于 bootstrap 中的网格系统,但在 tkinter 中,在列中添加一些内容会使列变大。
那么我的问题是,如何创建固定大小的列?
谢谢!
创建不相等列的代码:
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, background = "black")
self.columnconfigure(0, weight = 1)
self.columnconfigure(1, weight = 1)
self.columnconfigure(2, weight = 1)
self.rowconfigure(0, weight = 1)
self.rowconfigure(1, weight = 1)
label = tk.Label(self, text = "Weather", fg = "white", bg="green", font = LARGE_FONT)
label.grid(row = 0, column = 0, padx = 0, pady = 0, sticky = 'new')
label = tk.Label(self, text = "Time", fg = "white", bg="blue", font = LARGE_FONT)
label.grid(row = 0, column = 1, pady = 0, sticky = 'nwe')
label = tk.Label(self, text = "Test", fg = "white", bg="red", font = LARGE_FONT)
label.grid(row = 0, column = 2, pady = 0, sticky = 'nwe')
label2 = tk.Label(self, text = "News", fg = "white", bg="black", font = LARGE_FONT)
label2.grid(row = 1, column = 0, padx = 0, pady = 10, sticky = 'sw')
grid
有一个专门用于大小相等的列和行的选项:uniform
。它接受任何字符串作为值,所有具有相同值的列都将具有相同的大小。
在你的情况下它看起来像这样:
self.columnconfigure(0, weight = 1, uniform="x")
self.columnconfigure(1, weight = 1, uniform="x")
self.columnconfigure(2, weight = 1, uniform="x")
我的目标是在 tkinter 中创建 3 个大小相同的列,然后能够用我想要的任何东西填充它们。 我正在尝试做的类似于 bootstrap 中的网格系统,但在 tkinter 中,在列中添加一些内容会使列变大。
那么我的问题是,如何创建固定大小的列?
谢谢!
创建不相等列的代码:
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, background = "black")
self.columnconfigure(0, weight = 1)
self.columnconfigure(1, weight = 1)
self.columnconfigure(2, weight = 1)
self.rowconfigure(0, weight = 1)
self.rowconfigure(1, weight = 1)
label = tk.Label(self, text = "Weather", fg = "white", bg="green", font = LARGE_FONT)
label.grid(row = 0, column = 0, padx = 0, pady = 0, sticky = 'new')
label = tk.Label(self, text = "Time", fg = "white", bg="blue", font = LARGE_FONT)
label.grid(row = 0, column = 1, pady = 0, sticky = 'nwe')
label = tk.Label(self, text = "Test", fg = "white", bg="red", font = LARGE_FONT)
label.grid(row = 0, column = 2, pady = 0, sticky = 'nwe')
label2 = tk.Label(self, text = "News", fg = "white", bg="black", font = LARGE_FONT)
label2.grid(row = 1, column = 0, padx = 0, pady = 10, sticky = 'sw')
grid
有一个专门用于大小相等的列和行的选项:uniform
。它接受任何字符串作为值,所有具有相同值的列都将具有相同的大小。
在你的情况下它看起来像这样:
self.columnconfigure(0, weight = 1, uniform="x")
self.columnconfigure(1, weight = 1, uniform="x")
self.columnconfigure(2, weight = 1, uniform="x")