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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With