1

I'm starting to learn coding GUI now (tkinter) after learning about basic Python. So now I'm playing around, trying to get a feel of the geometry management.

There is something I can't figure out as to why it happens. I try to make a grid to get my lay out set up the way I want like this(example):

Label1 = ttk.Label(mainFrame, text='label1').grid(column=1, row=1)
Label2 = ttk.Label(mainFrame, text='label2').grid(column=3, row=2)
Label3 = ttk.Label(mainFrame, text='label3').grid(column=5, row=3)
Label4 = ttk.Label(mainFrame, text='label4').grid(column=45, row=4)
Label5 = ttk.Label(mainFrame, text='label5').grid(column=109, row=6)
Label6 = ttk.Label(mainFrame, text='label6').grid(column=115, row=7)

This does not result in big amounts of space between the labels, but still has everything right next to each other. Why does it not keep open space there when I skip a row or column number?

I'm using http://www.tkdocs.com/tutorial/ for most info, but also couldnt fint the answer to this anywhere else.

this is the full code that I use in case it's needed:

from tkinter import *
from tkinter import ttk

root = Tk()
root.title('Title of the root frame!')

mainFrame = ttk.Frame(root) #root is the parent of the frame
mainFrame.grid(column=0, row=0)
mainFrame['padding'] = (15, 20, 1, 50)
mainFrame['height'] = (10500)
mainFrame['width'] = (100)
mainFrame['borderwidth'] = 5
mainFrame['relief'] = 'sunken'

Frame2 =ttk.Frame(mainFrame)
Frame2.grid(column=0, row=0)
Frame2['borderwidth'] = 100

Frame2['relief'] = 'sunken'
Labelfr2 = ttk.Label(Frame2, text = 'labelfr2 in Frame2').grid(column=1, row =1)


Label1 = ttk.Label(mainFrame, text='The label1').grid(column=1, row=1)
Label2 = ttk.Label(mainFrame, text='The label2').grid(column=3, row=2)
Label3 = ttk.Label(mainFrame, text='The label3').grid(column=5, row=3)
Label4 = ttk.Label(mainFrame, text='The label4').grid(column=45, row=4)
Label5 = ttk.Label(mainFrame, text='The label5').grid(column=109, row=6)
Label6 = ttk.Label(mainFrame, text='The label6').grid(column=115, row=7)

root.mainloop()
LuukV
  • 203
  • 1
  • 11
  • I doubt this has anything to do with your problem, but did you know? In your code, `Label1` through `Label6` are all `None` because you're not assigning a Label to them, you're assigning the result of `grid`. – Kevin May 20 '14 at 15:21
  • @Kevin Hey man, thanks. Thats really good to know. It's not in the tutorial (so far) – LuukV May 20 '14 at 15:52

1 Answers1

2

Why is there not lots of extra space? Because columns by default have a width of zero, and rows have a default height of zero. If there's nothing in a column, that column will be invisible.

By the way -- you are making a very common mistake. The method grid always returns None, and if you do foo = x().y(), foo will always have the value of y(). Thus, when you do something like Label1 = ttk.Label(...).grid(...), Label1 will be set to None.

The best practice -- regardless of what some tutorials show you -- is to separate widget creation from widget layout. For example, I would change your code to be:

Label1 = ttk.Label(...)
Label2 = ttk.Label(...)
...

Label1.grid(...)
Label2.grid(...)
...

By keeping all of the layout together for a given container widget, it makes your code easier to visualize and easier to maintain over time.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Nice explanation on the `grid` issue. I don't suppose you know of a nice standalone question that explains that problem? I'm searching for a "canonical" post I can point people to. [ttk button returns None](http://stackoverflow.com/questions/2034576/ttk-button-returns-none) is the best one I've found so far. – Kevin May 20 '14 at 15:32