【问题标题】:Why I cannot use tkinter geometry manager grid?为什么我不能使用 tkinter 几何管理器网格?
【发布时间】:2020-07-24 21:30:26
【问题描述】:

我正在尝试构建一个简单的 tkinter GUI,但出现此错误:

_tkinter.TclError: cannot use geometry manager grid inside 。已经有由 pack 管理的 slave

这是我的代码:

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')

有什么想法吗?

【问题讨论】:

  • 就是这样:您需要为几何管理器做出选择,然后为父窗口小部件中的所有窗口小部件坚持使用一个几何管理器。
  • 你可能也想看看这个答案:stackoverflow.com/questions/63079633/…

标签: python tkinter


【解决方案1】:

weather_conditions_label_frameNone,因为你是 windows_conditions_label_frame = LabelFrame(...).grid(...)。稍后,您执行ttk.Entry(weather_conditions_label_frame, ...),它将条目放在根窗口中,因为主窗口是Nonetab_controller 已经使用 pack 添加到根窗口,因此当您尝试调用 grid 时会收到错误消息。

解决方案是将小部件创建与小部件布局分开:

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

【讨论】:

    猜你喜欢
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    相关资源
    最近更新 更多