spuckhafte
spuckhafte

Reputation: 89

Tkinter: why .grid() method giving an error?

Code:

import tkinter as tk
import random as rd

number = rd.randint(10,51)
trial = 0
chance = 0

window = tk.Tk()
window.title("Guess")
label = tk.Label(window, text="Guess a number that can be anything from '10' to '50', you have 5 chances !!!\n", font=("Arial Bold",20)).pack()

bt = tk.Button(window, text="Enter")
bt.grid(column=3, row=3)

window.geometry('2000x1000')
window.mainloop()

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

If I try to remove .pack() in 'label', the text doesn't load and still the button remains at (0,0) while there is no error

Upvotes: 0

Views: 304

Answers (3)

H. McClelland
H. McClelland

Reputation: 101

All you have to do is change label to also use grid instead of pack, I changed the line below:

label = tk.Label(window, text="Guess a number that can be anything from '10' to '50', you have 5 chances !!!\n", font=("Arial Bold",20))
label.grid()

Upvotes: 1

Delrius Euphoria
Delrius Euphoria

Reputation: 15098

The error is quite clear, you cannot mix between pack() and grid() inside a container or window. Here you are using pack() on the label and grid() on the btn. So change label to grid(). Or change bt.grid(..) to bt.pack().


Keep a note, your label is None, it can cause several errors later, if you plan on reusing the label. So the ideal way is to grid()/pack() on next line:

label = tk.Label(window, text="Guess a number that can be anything from '10' to '50', you have 5 chances !!!\n", font=("Arial Bold",20))
label.pack()

And if you don't plan on reusing it, then don't bother giving it a variable name at all.

Upvotes: 2

N o b o d y
N o b o d y

Reputation: 9

You need to use either grid or pack, not both. That's what I think from my experience.

Error: _tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack It says it's managed by pack, so you need to use pack manager "OR" grid manager

Upvotes: 0

Related Questions