简体   繁体   English

tkinter columconfigure 和 rowconfigure

[英]tkinter columconfigure and rowconfigure

I want to understand something with the grid layout in tkinter.我想了解 tkinter 中的网格布局。 Let's say that I want the column 1 in the row 1 to expand if there is extra space, but the column 1 in the row 2 to not expand how can I can do it?假设如果有额外空间,我希望第 1 行中的第 1 列扩展,但第 2 行中的第 1 列不扩展我该怎么做?

widget.columnconfigure

give you control on all columns, it is not possible to specify the row.让您控制所有列,无法指定行。

You can't do precisely what you want. 您不能完全按照自己的意愿做。 However, you can get the same visual effect by having the widget in row 1 span two columns where the widget in row 2 spans only one. 但是,通过使第1行中的小部件跨越两列,而第2行中的小部件仅跨越一列,您可以获得相同的视觉效果。 You can then give weight to the second column, which will affect the widget on row 1 but not on row two. 然后,您可以将权重赋予第二列,这将影响第1行的小部件,但不会影响第二行的小部件。

Here's a simple example: 这是一个简单的例子:

import tkinter as tk

root = tk.Tk()

l1 = tk.Frame(root, background="red", width=100, height=50)
l2 = tk.Frame(root, background="orange", width=100, height=50)
l3 = tk.Frame(root, background="green", width=100, height=50)
l4 = tk.Frame(root, background="blue", width=100, height=50)

root.columnconfigure(2, weight=1)
l1.grid(row=1, column=1, columnspan=2, sticky="ew")
l2.grid(row=1, column=3, sticky="ew")
l3.grid(row=2, column=1, sticky="ew")
l4.grid(row=2, column=3, sticky="ew")

root.mainloop()

When this code first starts up, it looks like this where the two columns are identical in size. 这段代码首次启动时,看起来两列大小相同。

在此处输入图片说明

When the window is resized, you can see that the widget on row 1 expands but the widget in row 2 does not. 调整窗口大小后,您可以看到第1行的小部件展开了,但第2行的小部件没有展开。

在此处输入图片说明

You may be looking for the columnspan option. 您可能正在寻找columnpan选项。

see http://effbot.org/tkinterbook/grid.htm 参见http://effbot.org/tkinterbook/grid.htm

Use two canvases, one for each row.使用两张画布,每行一张。 Then change the width and the columnspan of the widget you want to expand.然后更改要扩展的小部件的宽度和列跨度。 Finally, user columnconfigure.最后,用户列配置。 In the following example, I've created 2 Canvas,2 Frames, 2 buttons.在以下示例中,我创建了 2 个画布、2 个框架、2 个按钮。 One for each row.每行一个。

import tkinter as tk

root = tk.Tk()

root.geometry("600x300")
root.resizable(False,False)


canvas_1 = tk.Canvas(master=root,width=600, height=15)
canvas_2 = tk.Canvas(master=root)

canvas_1.pack(expand=True, fill="both")
canvas_2.pack(expand=True, fill="both")


#
frame_1 = tk.Frame(master=root)
frame_1.pack(expand=True, fill="both")

#  ------------------------------------------------------ All in row=0
button_in_column_0 = tk.Button(master=frame_1, text="I'm in Column_1").grid(row=0, column=1) 
button_in_column_1 = tk.Button(master=frame_1, text="I'm in Column_2",width=80).grid(row=0, column=2,columnspan=2)
button_in_column_2 = tk.Button(master=frame_1, text="I'm in Column_3").grid(row=0, column=3)

#  -----Let's say that you want the column 1 in the row 0 to expand if there is extra space
for x in range(2,3):
    frame_1.columnconfigure(x, weight=1)
    frame_1.rowconfigure(x, weight=1)


#  LET CREATE THE SECOND LINE WITH LIST OF BUTTONS

frame_2 = tk.Frame(master=root)
frame_2.pack(expand=True, fill="both")

#  ------------------------------------------------------------ All in row=0
button_1 = tk.Button(master=frame_2, text="I'm in Column_1").grid(row=0, column=1, sticky=tk.NSEW)
button_2 = tk.Button(master=frame_2, text="I'm in Column_2").grid(row=0, column=2, sticky=tk.NSEW)
button_3 = tk.Button(master=frame_2, text="I'm in Column_3").grid(row=0, column=3, sticky=tk.NSEW)


window_1 = canvas_1.create_window(0,1, anchor="nw", window=frame_1)
window_2 = canvas_2.create_window(0,1, anchor="nw", window=frame_2)


root.mainloop()

enter image description here在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM