简体   繁体   English

当小部件跨越多列时,如何制作宽度相等的Tkinter列(Python 2.7)

[英]How to make Tkinter columns of equal width when widgets span multiple columns (Python 2.7)

In the following, the buttons labelled 'ONE', 'TWO', and 'THR' do not get evenly spaced out. 在下文中,标有“ONE”,“TWO”和“THR”的按钮不会均匀分布。 It seems to me that the root of the problem is that Tk is assuming a default minimum width for any column containing part of a widget that spans multiple columns. 在我看来,问题的根源是Tk假定包含跨越多列的小部件的任何列的默认最小宽度。 However, this behaviour appears to be undocumented, so I am unsure how to accommodate for or adjust it in order to get the columns to be of equal width - including the two columns spanned by the text widget and the single column not spanned by the text widget - and thus space out the buttons evenly. 但是,此行为似乎没有记录,因此我不确定如何容纳或调整它以使列具有相等的宽度 - 包括文本小部件跨越的两列和文本未跨越的单列小部件 - 从而均匀地分隔按钮。 I could kludge it by trial and error, ie padding out the latter column until it matches the former two, but that seems a poor solution. 我可以通过反复试验来克服它,即填充后一列,直到它与前两列匹配,但这似乎是一个糟糕的解决方案。

EDIT: Following discussion below with @jwillis0720, I've added an extra column (3) and button ('FIV') to make the problem clearer. 编辑:在下面与@ jwillis0720进行讨论后,我添加了一个额外的列(3)和按钮('FIV')以使问题更加清晰。 This question is about how to get columns the same width when some of those columns are spanned by multi-column widgets and others are not. 这个问题是关于如何在多列小部件跨越其中一些列而其他列不同的情况下使列具有相同的宽度。

import Tkinter

master = Tkinter.Tk()

Tkinter.Button(master, text='ONE').grid(row=0, column=0)
Tkinter.Button(master, text='TWO').grid(row=0, column=1)
Tkinter.Button(master, text='THR').grid(row=0, column=2)
Tkinter.Button(master, text='FOU').grid(row=1, column=2)
Tkinter.Button(master, text='FIV').grid(row=0, column=3) # added as per above edit
Tkinter.Text(master).grid(row=1, column=0, columnspan=2)

master.mainloop()

Please note that using grid_columnconfigure with uniform does not solve this problem. 请注意,使用带有uniform grid_columnconfigure并不能解决此问题。 Inserting the following lines (see answer to similar question here: How to create equal-width columns in Python 2.7 with Tkinter ) simply makes the columns stretchy; 插入以下行(请参阅此处类似问题的答案: 如何在Python 2.7中使用Tkinter创建等宽列 )只会使列具有弹性; they remain unevenly sized: 它们仍然不均匀:

master.grid_columnconfigure(0, weight=1, uniform='a')
master.grid_columnconfigure(1, weight=1, uniform='a')
master.grid_columnconfigure(2, weight=1, uniform='a')
master.grid_columnconfigure(3, weight=1, uniform='a') # added as per above edit

I think you might want to use the sticky option. 我想你可能想要使用粘性选项。

sticky= Defines how to expand the widget if the resulting cell is larger than the widget itself. sticky =如果结果单元格大于小部件本身,则定义如何扩展小部件。 This can be any combination of the constants S, N, E, and W, or NW, NE, SW, and SE. 这可以是常数S,N,E和W,或NW,NE,SW和SE的任何组合。

For example, W (west) means that the widget should be aligned to the left cell border. 例如,W(west)表示窗口小部件应与左边的单元格边框对齐。 W+E means that the widget should be stretched horizontally to fill the whole cell. W + E表示小部件应水平拉伸以填充整个单元格。 W+E+N+S means that the widget should be expanded in both directions. W + E + N + S意味着小部件应该在两个方向上扩展。 Default is to center the widget in the cell. 默认是将窗口小部件置于单元格中心。

import Tkinter

master = Tkinter.Tk()

Tkinter.Button(master, text='ONE').grid(row=0, column=0, sticky='NW')
Tkinter.Button(master, text='TWO').grid(row=0, column=1, sticky='NW')
Tkinter.Button(master, text='THR').grid(row=0, column=2, sticky='NW')
Tkinter.Button(master, text='FOU').grid(row=1, column=2)
Tkinter.Text(master).grid(row=1, column=0, columnspan=2)


master.mainloop()

Edit 编辑

What does it look like. 它是什么样子的。 Mine looks like this evenly spaced except the text widget takes up two columns as specified. 除了文本小部件占用指定的两列之外,我看起来像这样均匀分布。

它看起来像这样吗?

old post I know, but I also struggled to get the columns and rows to maintain a common width/height so I thought I would share my solution 老帖子我知道,但我也努力让列和行保持共同的宽度/高度所以我想我会分享我的解决方案

newish to python and tkinter, so if there are any mistakes please let me know 对python和tkinter来说很新,所以如果有任何错误请告诉我

I created a grid manager, this allowed the main window and any frame to be setup with evenly spaced columns and rows, it's not 100% but for what I was using it for it worked well, it was especially useful during the building phase 我创建了一个网格管理器,这允许主窗口和任何框架设置均匀间隔的列和行,它不是100%但是因为它使用它因为它工作得很好,它在构建阶段特别有用

one downside when creating a frame is the maximum number of rows/columns of the frame must be equal to or less than the number or rows/columns it is spanning, otherwise it goes a bit weird (nut sure why) 创建框架时的一个缺点是框架的最大行数/列数必须等于或小于它所跨越的数量或行数/列数,否则它会有点奇怪(坚信为什么)

hope this helps 希望这可以帮助

import tkinter

class grid_manager:
    def __init__(self, Frame, colour = "gray94"):
        self.Frame = Frame
        self.Colour = colour

    def set_grid(self, numofRows, numofColumns, borderwidth = 1):
        self.numofRows = numofRows
        self.numofColumns = numofColumns
        self.borderwidth = borderwidth
        for i in range(numofRows):
            for j in range(numofColumns):
                canvas = tkinter.Canvas(self.Frame)
                canvas.config(relief="raised", borderwidth=self.borderwidth)   #comment out to hide grid layout
                canvas.grid(row=i, column=j)
                canvas.config(background=self.Colour)
                self.Frame.columnconfigure(j, weight=1)
            self.Frame.rowconfigure(i, weight=1)

mainwindow = tkinter.Tk()

mainwindow.title("Test")
mainwindow.geometry("640x480-8-200")
mainGrid = grid_manager(mainwindow)
mainGrid.set_grid(10, 10)

header_Frame = tkinter.Frame(mainwindow)
header_Frame.grid(row=0, column=0, columnspan=10, sticky="nsew")
headerGrid = grid_manager(header_Frame)
headerGrid.set_grid(numofRows=1, numofColumns=10, borderwidth=5)

footerFrame = tkinter.Frame(mainwindow)
footerFrame.grid(row=9, column=0, columnspan=10, sticky="nsew")
footerGrid = grid_manager(footerFrame, "red")
footerGrid.set_grid(numofRows=1, numofColumns=10, borderwidth=5)

rightFrame = tkinter.Frame(mainwindow)
rightFrame.grid(row=1, column=5, rowspan=5, columnspan=5, sticky="nsew")
rightGrid = grid_manager(rightFrame, "blue")
rightGrid.set_grid(numofRows=5, numofColumns=5, borderwidth=2)

leftFrame = tkinter.Frame(mainwindow)
leftFrame.grid(row=3, column=0, rowspan=5, columnspan=4, sticky="nsew")
leftGrid = grid_manager(leftFrame, "yellow")
leftGrid.set_grid(numofRows=5, numofColumns=4, borderwidth=2)

mainwindow.mainloop()

enter image description here 在此输入图像描述

import tkinter

master = tkinter.Tk()

tkinter.Button(master, text='ONE                ').grid(row=0, column=3, sticky='NW')
tkinter.Button(master, text='TWO               ').grid(row=1, column=3, sticky='NW')
tkinter.Button(master, text='THR                ').grid(row=2, column=3, sticky='NW')
tkinter.Button(master, text='FOU                ').grid(row=3, column=3, sticky='NW')
tkinter.Text(master).grid(column=30, columnspan=10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM