Python Tkinter 配置列宽

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14045271/
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-18 10:16:09  来源:igfitidea点击:

Tkinter configure columnwidth

pythontkinter

提问by Evilunclebill

I have been around the docs and found 3 viable options inside the columnconfigure:

我浏览了文档并在 columnconfigure 中找到了 3 个可行的选项:

  • minsize
  • pad
  • weight
  • 最小尺寸
  • 软垫
  • 重量

But what if you wanted to have the column as small as possible?

但是,如果您想让列尽可能小怎么办?

I have 3 checkbuttons i want to line up next to eachother.

我有 3 个复选按钮,我想排成一排。

what i found working was to:

我发现工作是:

weekly.grid(row = 2, column = 1, sticky = W)
monthly.grid(row = 2, column = 1, padx = 75, sticky = W)
yearly.grid(row = 2, column = 1, padx = 150, sticky = W)

Is there not a more "beautifull" way to do this?

没有更“美丽”的方式来做到这一点吗?

(might be worth saying that when using column 2 and 3 they are seperated waaaay too much :-()

(可能值得一提的是,在使用第 2 列和第 3 列时,它们分开太多了 :-()

Best regards,

此致,

Casper

卡斯帕

采纳答案by Bryan Oakley

The default behavior of using the grid geometry manager is that columns will be as small as possible, so you don't need to do anything (assuming you're using grid properly).

使用网格几何管理器的默认行为是列尽可能小,因此您不需要做任何事情(假设您正确使用了网格)。

The behavior you describe where there is too much space between the elements is probably due to the fact you have other widgets in that same column which are wider. The column will default to the smallest width that will accomodate the widest item. That, or elsewhere in your code you give a non-zero weight to some of those columns that is causing them to expand.

您描述的元素之间空间太大的行为可能是因为您在同一列中有其他更宽的小部件。该列将默认为可容纳最宽项目的最小宽度。那,或在您的代码中的其他地方,您为导致它们扩展的某些列赋予非零权重。

Before I talk about a solution, let me make sure it's clear that the way you're using grid to put more than one widget in the same cell is definitely the wrong way to do it. There's absolutely no reason to resort to such a solution. A general rule of thumb is that you should never put more than one widget in a cell.

在我谈论解决方案之前,让我确保您使用网格将多个小部件放在同一个单元格中的方式绝对是错误的方式。绝对没有理由求助于这样的解决方案。一般的经验法则是,您永远不应在一个单元格中放置多个小部件。

The simplest solution for you is to combine grid and pack. Put all of your checkbuttons in a frame and pack them on the left side of that frame. Then, put the frame in your grid with sticky="w". Then, no matter how big the window gets, the checkbuttons will always be stuck to the left side of their containing frame.

对您来说最简单的解决方案是结合 grid 和 pack。将所有复选按钮放在一个框架中,并将它们打包在该框架的左侧。然后,使用 将框架放入网格中sticky="w"。然后,无论窗口有多大,复选按钮将始终卡在其包含框架的左侧。

Note that this solution doesn't break the rule of thumb I mentioned earlier. You're only putting one widget in a cell: the frame. You can put whatever you want in that inner frame, but from the perspective of the grid there is only a single widget in each cell of the grid.

请注意,此解决方案不会违反我之前提到的经验法则。您只在一个单元格中放置了一个小部件:框架。您可以在该内部框架中放置任何您想要的内容,但从网格的角度来看,网格的每个单元格中只有一个小部件。

Here is a working example base on python 2.7:

这是一个基于 python 2.7 的工作示例:

import Tkinter as tk

class ExampleView(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        cbframe = tk.Frame(self)
        cb1 = tk.Checkbutton(cbframe, text="Choice 1")
        cb2 = tk.Checkbutton(cbframe, text="Choice 2")
        cb3 = tk.Checkbutton(cbframe, text="Choice 3")

        cb1.pack(side="left", fill=None, expand=False)
        cb2.pack(side="left", fill=None, expand=False)
        cb3.pack(side="left", fill=None, expand=False)

        # this entry is for illustrative purposes: it
        # will force column 2 to be widget than a checkbutton
        e1 = tk.Entry(self, width=20)
        e1.grid(row=1, column=1, sticky="ew")

        # place our frame of checkbuttons in the same column
        # as the entry widget. Because the checkbuttons are
        # packed in a frame, they will always be "stuck"
        # to the left side of the cell.
        cbframe.grid(row=2, column=1, sticky="w")

        # let column 1 expand and contract with the 
        # window, so you can see that the column grows
        # with the window, but that the checkbuttons
        # stay stuck to the left
        self.grid_columnconfigure(1, weight=1)

if __name__ == "__main__":
    root = tk.Tk()
    view = ExampleView(root)
    view.pack(side="top", fill="both", expand=True)
    root.wm_geometry("400x200")
    root.mainloop()

Of course, you can also place the checkbuttons in separate columns -- which is often easiest -- but you might need to have other items in other rows span multiple columns and deal with column weights. Since it's not clear exactly what your problem is based on your description, the above solution is probably the simplest for you.

当然,您也可以将复选按钮放在单独的列中——这通常是最简单的——但是您可能需要在其他行中放置其他项目跨越多列并处理列权重。由于根据您的描述尚不清楚您的问题究竟是什么,因此上述解决方案对您来说可能是最简单的。