Salhi Fedi
Salhi Fedi

Reputation: 71

Why I cannot use tkinter geometry manager grid?

I'am trying to build a simple tkinter GUI but I got this error:

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

Here is my code:

file_menu = Menu(menu_bar, tearoff=0)
file_menu.add_command(label='New')
file_menu.add_separator()
file_menu.add_command(label='Exit', command=_quit)
menu_bar.add_cascade(label='File', menu=file_menu)

help_menu = Menu(menu_bar, tearoff=0)
help_menu.add_command(label='About')
menu_bar.add_cascade(label='Help', menu=help_menu)

tab_controller = ttk.Notebook(win)

tab_1 = ttk.Frame(tab_controller)
tab_controller.add(tab_1, text='NOAA')

tab_2 = ttk.Frame(tab_controller)
tab_controller.add(tab_2, text='Station IDs')

tab_3 = ttk.Frame(tab_controller)
tab_controller.add(tab_3, text='Images')

tab_4 = ttk.Frame(tab_controller)
tab_controller.add(tab_4, text='Open Weather Map')

tab_controller.pack(expand=1, fill='both')

weather_conditions_label_frame = ttk.LabelFrame(tab_1, text='Current Weather Conditions').grid(column=0, row=1,                                                                                            padx=WEATHER_CONDITIONS_LABEL_FRAME_PAD_X,                                                                                            pady=WEATHER_CONDITIONS_LABEL_FRAME_PAD_Y)

ttk.Label(weather_conditions_label_frame, text='Last Update').grid(column=0, row=1, sticky='E')
last_update_entry_var = tk.StringVar()
last_update_entry = ttk.Entry(weather_conditions_label_frame, width=ENTRY_WIDTH, textvariable=last_update_entry_var, state='readonly')
last_update_entry.grid(column=1, row=1, sticky='W')

Any idea?

Upvotes: 0

Views: 123

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385980

weather_conditions_label_frame is None because you do windows_conditions_label_frame = LabelFrame(...).grid(...). Later, you do ttk.Entry(weather_conditions_label_frame, ...) which places the entry in the root window since the master is None. tab_controller was already added to the root window with pack, so when you try to call grid you get the error.

The solution is to separate widget creation from widget layout:

weather_conditions_label_frame = ttk.LabelFrame(...)
weather_conditions_label_frame.grid(...)

Upvotes: 2

Related Questions