Python Tkinter.grid 间距选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28019402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:37:36  来源:igfitidea点击:

Tkinter.grid spacing options?

pythonpython-3.xtkintergrid-layout

提问by PlatypusVenom

I'm new to Tkinter, and I tried creating an app with the grid layout manager. However, I can't seem to find a way to utilize it the way I want to. What I need to do is simulate a grid full of 'cells' so that I can place, for example, a label in cell (3,8) or a button in cell (5,1). Here is an example of what I've tried:

我是 Tkinter 的新手,我尝试使用网格布局管理器创建一个应用程序。但是,我似乎无法找到一种以我想要的方式使用它的方法。我需要做的是模拟一个充满“单元格”的网格,以便我可以在单元格 (3,8) 中放置一个标签或在单元格 (5,1) 中放置一个按钮。这是我尝试过的示例:

import tkinter as tk
root = tk.Tk()

label = tk.Label(root, text = 'Label')
label.grid(column = 3, row = 8)

button = tk.Button(root, text = 'Button')
button.grid(column = 5, row = 1)

Tkinter keeps the relative position of each widget (i.e. buttonis above and to the right of label), but it ignores any space in between. I've searched a bit for this problem and found out about weight, but that doesn't seem to be what I'm looking for; I'd like something independent of the labeland button's layouts. Thanks for the help!

Tkinter 保持每个小部件的相对位置(即button在 的上方和右侧label),但它会忽略中间的任何空间。我对这个问题进行了一些搜索,发现了 about weight,但这似乎不是我要找的;我想要一些独立于labelandbutton的布局。谢谢您的帮助!

采纳答案by zehnpaard

You can specify the spacing on columns/rows in the grid (including those that contain no widget) by running the grid_columnconfigureand grid_rowconfiguremethods on the parent of the widgets (in this case, the parent would be root).

您可以通过在小部件的父级上运行grid_columnconfiguregrid_rowconfigure方法(在这种情况下,父级为root)来指定网格中列/行的间距(包括那些不包含小部件的)。

For example, to set a minimum width for the 4th column, add the last line to your code:

例如,要设置第 4 列的最小宽度,请将最后一行添加到您的代码中:

import Tkinter as tk
root = tk.Tk()

label = tk.Label(root, text = 'Label')
label.grid(column = 3, row = 8)

button = tk.Button(root, text = 'Button')
button.grid(column = 5, row = 1)

root.grid_columnconfigure(4, minsize=100)  # Here

If you want to set a minimum size for all of your columns and rows, you can iterate through them all using:

如果要为所有列和行设置最小大小,可以使用以下方法遍历它们:

col_count, row_count = root.grid_size()

for col in xrange(col_count):
    root.grid_columnconfigure(col, minsize=20)

for row in xrange(row_count):
    root.grid_rowconfigure(row, minsize=20)

EditInterestingly, effbot states:

编辑有趣的是,effbot状态:

minsize=Defines the minimum size for the row. Note that if a row is completely empty, it will not be displayed, even if this option is set.

minsize=定义行的最小大小。请注意,如果一行完全为空,即使设置了此选项,它也不会显示。

(italics mine) but it seems to work even for rows and columns that are empty, as far as I've tried.

(斜体我的)但它似乎甚至适用于空的行和列,据我尝试。

回答by Bryan Oakley

By default, a row is of height zero and a column is of width zero. So, rows and columns that don't have anything in them will be invisible and take up no space.

默认情况下,行的高度为零,列的宽度为零。因此,其中没有任何内容的行和列将不可见且不占用空间。