tkinter columconfigure 和 rowconfigure [英] tkinter columconfigure and rowconfigure

查看:28
本文介绍了tkinter columconfigure 和 rowconfigure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解 tkinter 中的网格布局.假设如果有额外空间,我希望第 1 行中的第 1 列展开,但第 2 行中的第 1 列不展开,我该怎么做?

I want to understand something with the grid layout in 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?

widget.columnconfigure

让您控制所有列,无法指定行.

give you control on all columns, it is not possible to specify the row.

推荐答案

你不能精确地做你想做的事.但是,您可以通过让第 1 行中的小部件跨越两列而第 2 行中的小部件仅跨越一列来获得相同的视觉效果.然后您可以给第二列赋予权重,这将影响第 1 行的小部件,但不会影响第 2 行.

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. You can then give weight to the second column, which will affect the widget on row 1 but not on row two.

这是一个简单的例子:

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.

调整窗口大小后,您可以看到第 1 行的小部件展开,但第 2 行的小部件没有展开.

When the window is resized, you can see that the widget on row 1 expands but the widget in row 2 does not.

这篇关于tkinter columconfigure 和 rowconfigure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆