0

I begin with Tkinter and I try to understand the different geometry package managers. I found something weird and I would like a explanation.

I try to print this windows:
enter image description here

I test two pieces of code :

import tkinter as tk
fen = tk.Tk() 
fr1 = tk.Frame(fen).grid(row=0,column=0)
fr2 = tk.Frame(fen).grid(row=0,column=1)

txt1 = tk.Label(fr1, text="txt1").pack(side="top")
tk.Frame(fr1,bg="blue",height=5).pack(side="bottom",fill="x")

txt2 = tk.Label(fr2, text="txt2").pack(side="top")
tk.Frame(fr2,bg="red",height=5).pack(side="bottom",fill="x")

and

import tkinter as tk
fen = tk.Tk() 
fr1 = tk.Frame(fen)
fr2 = tk.Frame(fen)

txt1 = tk.Label(fr1, text="txt1").pack(side="top")
tk.Frame(fr1,bg="blue",height=5).pack(side="bottom",fill="x")

txt2 = tk.Label(fr2, text="txt2").pack(side="top")
tk.Frame(fr2,bg="red",height=5).pack(side="bottom",fill="x")

fr1.grid(row=0,column=0)
fr2.grid(row=0,column=1)

Just the position of the call to the grid is different.

The first one sends me a geometry error :

"cannot use geometry manager pack inside . which already has slaves managed by grid"

and the second one works.

Why there is this difference in behaviour?

Tranbi
  • 11,407
  • 6
  • 16
  • 33
  • 1
    Does this answer your question? [Tkinter: AttributeError: NoneType object has no attribute ](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name) You don't get the error but this is the issue you have (at least in the first example) – Matiiss Nov 02 '21 at 11:57
  • also what is the usage of the empty frames that just get packed? they don't really do anything, nothing can be put in those either – Matiiss Nov 02 '21 at 12:02
  • In fact, I work on something complexier than this toy example. I propose this example in order to better hightligh the behavior which I don't understand. – Hugo Chevroton Nov 02 '21 at 13:08
  • yes that is because in the first example `fr1 = None` and `fr2 = None` too, so basically you are setting the parent as `None` for those labels and frames, that means they default to the root `Tk` instance and that causes both pack and grid to be used there which causes the error, I already linked to a question that answers (partially, it shows the solution tho) this – Matiiss Nov 02 '21 at 13:28
  • Ok, I got it! I didn't notice that tk.Frame(fen).grid(row=0,column=0) return None. Thank you very much – Hugo Chevroton Nov 02 '21 at 14:47

0 Answers0