Tkinter Grid column ignored

Consider the following python script

#!/usr/bin/env python
from Tkinter import Tk, Label

width = SOME_VALUE_HERE

root = Tk()
label1 = Label(root, text='1 columns wide')
label2 = Label(root, text='%i columns wide' % width)

label1.grid()
label2.grid(row=0,column=1,columnspan=width)

root.mainloop()

      

When I run this, no matter what value is set for "SOME_VALUE_HERE", both labels take up half of the window, regardless of whether Grid.columnconfigure is called or a custom parameter is used in grid ().

Unless I missed something, I would have thought that setting the column would cause the second label to be "SOME_VALUE_HERE" once as wide as the first.

Am I misunderstanding how the grid works? How can I achieve this behavior?

+1


source to share


3 answers


By default, an empty grid column is zero, so you described the following table. The default mesh geometry manager will try to optimize the screen real estate used by your application. It will integrate all constraints and create the most suitable layout.

+---------------+---------------++++
|       0       |       1       |||| <-- 2,3,4 empty, 0 width
+---------------+---------------++++
| 1 column wide | 4 column wide    |
+---------------+---------------++++

      

To provide strict proportional column width, you must use the option uniform

columnconfigure

. uniform

take an arbitrary value to denote the group of columns that these proprotions share, the argument is weight

used to handle the size of the widget correctly.



label1.grid(row=0, column=0)
label2.grid(row=0,column=1, columnspan=width)
for i in range(width+1):
    root.grid_columnconfigure(i, weight=1, uniform="foo")

      

Note that with these two labels, you could achieve the same layout by adjusting the width of column 1. The differences will repeat as you fill column 2,3,4 ...

label2.grid(row=0,column=1) #no columnspan
root.grid_columnconfigure(0, weight=1, uniform="foo")
root.grid_columnconfigure(1, weight=width, uniform="foo")

      

+2


source


When you put something in column 1 with a column of two (or more) that means it will be in column 1 and column 2 (and so on). However, if there is nothing controlling the width of a column, that column will have a width of zero. You need to force column 2 to have something in it by putting something in there, giving it a minsize, or forcing uniform columns.



When I look at your code, I can't guess how wide your column 2 should be, as well as the computer.

+2


source


I had a similar problem only to find that the items are constrained to the widest widget. We can safely say that Tkinter is set up to keep your application consistent as it needs to be a regular repeating square / triangular structure. Solution to override default options.

  • With Tkinter's auto-optimizations taken into account, play with the width and height of the largest widget (grid) and relate the other margins to it proportionally.
  • Using the above method, use columnspan

    to adjust the width.
  • Adjust the width with columnconfigure()

+1


source







All Articles