0

I have started working with tkinter and I have such problems. For example,

from tkinter import *

root = Tk()

frame = Frame(root)
frame.pack()

theLabel = Label(root, text="bla bla")
theLabel.grid(row=0)

root.mainloop()

This code does not work and gives an error like that.

File "C:\Users\Kenan\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2074, in grid_configure
    + self._options(cnf, kw))
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
Lafexlos
  • 7,618
  • 5
  • 38
  • 53

2 Answers2

3

As mentioned by @Luke.py, you can't mix grid() and pack() within the same widget.

  • On line 6, you are packing a Frame inside root
  • On line 9, you are griding a Label inside root

Perhaps you meant to grid() the Label inside the frame, in which case change line 8 to

theLabel = Label(frame, text="bla bla")

Or maybe you really do want the Frame and Label at the same level, in which case, change line 6 to

frame.grid()
Grezzo
  • 2,220
  • 2
  • 22
  • 39
  • thanks guys, my mistake was frame. I should not use it. When I remove it worked. Thanks to everyone :) –  Oct 28 '16 at 09:07
  • If you start using ttk for more native looking controls and windows on all platforms, you will want to put a `ttk.Frame` inside every `Toplevel` to get the right (system default) colour background – Grezzo Oct 28 '16 at 09:21
2

This problem arises when you use a combination of pack and grid within the same frame / root.

Change the grid on line 9 to pack(), or vise versa.

from tkinter import *

root = Tk()

frame = Frame(root)
frame.pack()

theLabel = Label(frame, text="bla bla") *this should use frame - not root
theLabel.grid()

root.mainloop()

Hope this helps! Luke

Luke.py
  • 965
  • 8
  • 17
  • Firstly thanks for answer. But, .pack works anyway. I need to use .grid. Actually I have written some entries and I want to locate them near labels. Can you help me about this? –  Oct 27 '16 at 08:14
  • 1
    You changed both the `Label`'s root (from `root` to `frame`) which would've fixed it. But additionally you also changed `grid` to `pack` which isn't necessary anymore. – xZise Oct 27 '16 at 10:01
  • @xZise good spot! Thank you – Luke.py Oct 27 '16 at 10:50