python - tkinter columconfigure 和 rowconfigure

标签 python tkinter

我想对 tkinter 中的网格布局有所了解。假设我希望第 1 行中的第 1 列在有额外空间时扩展,但第 2 行中的第 1 列不扩展我该怎么办?

widget.columnconfigure

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

最佳答案

您无法完全按照自己的意愿行事。但是,您可以通过让第 1 行中的小部件跨越两列而第 2 行中的小部件仅跨越一列来获得相同的视觉效果。然后,您可以为第二列赋予权重,这将影响第 1 行的小部件,但不会影响第 2 行的小部件。

这是一个简单的例子:

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()

当这段代码第一次启动时,看起来像这样,其中两列的大小相同。

enter image description here

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

enter image description here

关于python - tkinter columconfigure 和 rowconfigure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21893288/

相关文章:

python - 使用 mypy 处理条件逻辑 + 标记值

python - 当出现故障或挂起时,如何确保关闭负载均衡器中的所有连接?

python - 将 lambda 函数绑定(bind)到使用 For 循环创建的多个 Tkinter Entry 小部件

python - 在哪里可以通过实际示例学习 Pyramid?

python - 计算 Pandas DataFrame 中父级总数的份额

Python - 具有 "don' t care"值的数字类

python - Tkinter 顶级小部件不显示 - python

python - 是什么魔法阻止了 Tkinter 程序在交互式 shell 中阻塞?

python - Python Tkinter 中的部分边框

python - 基于循环更新 tkinter 标签