0

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?

Salhi Fedi
  • 73
  • 8
  • That's exactly what it is: you need to make a choice for the geometry manager and then stick to one geometry manager for all widgets in the parent widget. – Ronald Jul 24 '20 at 21:40
  • You may want to take look on this answer here too: https://stackoverflow.com/questions/63079633/tkinter-grid-forget-is-clearing-the-frame/63079747#63079747 – Thingamabobs Jul 25 '20 at 09:10

1 Answers1

2

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(...)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685