Reputation: 5
There is one line of code within a Class that is causing a Problem. When I comment it out there is no error. The line of code I wrote tries to grid() a tk.Label. This is nothing special within Tkinter and in-fact it is nothing special within the class. I have already grid()ed many other tk.Button's (A variable amount) before trying to grid this tk.Label The error says:
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
This would make sense if I was using pack within that class but I am not.
I have tried taking removing and adding this line and taking out pack from other parts of the code however this error persists.
The Board class has been imported from board.py in another file in the same folder. It is working code please don't worry too much about that.
Also please don't rip into me about my coding practices too much:
'''class MainPage(tk.Frame):
def __init__(self, parent, controller, rows):
tk.Frame.__init__(self,parent)
self.rows = rows.get()
self.board = Board(self.rows)
home_page_button = tk.Button(self, text = "Home", command = lambda: controller.show_frame(Home))
home_page_button.grid(row=11, column = 0, columnspan = 3)
self.board_of_buttons = []
self.ice_img = tk.PhotoImage(file="ice.gif")
self.boulder_img = tk.PhotoImage(file="boulder.gif")
for i in range(self.rows):
n = []
for j in range(self.rows):
n.append(tk.Button(self, image = self.ice_img, command = lambda x=i, y=j: self.change_mode(x,y,self.ice_img)))
self.board_of_buttons.append(n)
for i in range(self.rows):
for j in range(self.rows):
self.board_of_buttons[j][i].grid(row = i, column = j)
self.is_possible = tk.StringVar()
self.is_possible.set(self.get_is_possible())
self.is_possible_label = tk.Label(textvariable = self.is_possible)
self.is_possible_label.grid(row = self.rows + 1, column = 0, columnspan=self.rows) ###### <--- This is the line of code that causes the error. When I add it: error. When I take it away: Works
def change_mode(self,row,column,image):
self.change_image(row,column,image)
self.board.board[column][row].change_type()
def change_image(self, row, column, image):
if image == self.ice_img:
self.board_of_buttons[row][column] = tk.Button(self, image = self.boulder_img, command = lambda: self.change_mode(row,column,self.boulder_img))
self.board_of_buttons[row][column].grid(row = column, column = row)
else:
self.board_of_buttons[row][column] = tk.Button(self, image = self.ice_img, command = lambda: self.change_mode(row,column,self.ice_img))
self.board_of_buttons[row][column].grid(row = column, column = row)
def get_is_possible(self):
self.board.nodeify()
p = self.board.check_linkage()
self.board.switch_the_switches(p)
if self.board.is_possible():
return 'It is possible'
else:
return "It is not possible"
'''
Thanks for any help
Upvotes: 0
Views: 288
Reputation: 1272
You don't pass the parent self
to the label
Try:
self.is_possible_label = tk.Label(self, textvariable = self.is_possible)
Upvotes: 2