Tkinter 配置列宽 [英] Tkinter configure columnwidth

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

问题描述

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

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

  • 最小化
  • 重量

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

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

我有 3 个复选按钮,我想将它们排成一排.

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

我发现的工作是:

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?

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

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

最好的问候,

卡斯帕

推荐答案

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

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.

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

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.

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

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.

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

Here is a working example base on 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.

这篇关于Tkinter 配置列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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