Aiden Chow
Aiden Chow

Reputation: 435

Tkintter error: cannot use geometry manager grid inside . which already has slaves managed by pack

from tkinter import *
import random
import time
tk=Tk()
canvas=Canvas(tk,width=1000,height=1000)
canvas.pack()
i=0
x1=-50
y1=0
choice=None
canvas.create_text(500, 300, text="Choose one.", font='TNR 20 bold')
def car():
    choice='car'
    return choice


button1=Button(tk,text='car',width=10,command=car)
button1.grid(row=1,column=5)#I do not see anything wrong here
tk.mainloop()

This is just part of my code if you are wondering why I have so many unused variables.

This is what is says when I run the code:

  
Traceback (most recent call last):
  File "(You dont need to know the file name)", line 180, in 
    button1.grid(row=1,column=5)

  File "C:\Users\()\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2223, in grid_configure
    + self._options(cnf, kw))

_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack

Upvotes: 3

Views: 1932

Answers (1)

Ishan Srivastava
Ishan Srivastava

Reputation: 1189

You cannot mix pack() and grid()
from the docs:

Warning: Never mix grid and pack in the same master window. Tkinter will happily spend the rest of your lifetime trying to negotiate a solution that both managers are happy with. Instead of waiting, kill the application, and take another look at your code. A common mistake is to use the wrong parent for some of the widgets.

you are calling canvas.pack() but to the same tk object you add a button on which you call the grid() function.

Upvotes: 2

Related Questions