tkinter python GUI 中存在错误。如何解决?

There is an Error in tkinter python GUI. How to fix it?

import tkinter
import tkinter as tk
from tkinter import ttk, messagebox, Menu, Button


window = tk.Tk()
window.title('Notebook')
window.geometry('320x320')

notebook = ttk.Notebook(window)
notebook.pack(pady=10, expand=True)

frame1 = ttk.Frame(notebook, width=400, height=280).pack(fill='both', expand=True)
frame2 = ttk.Frame(notebook, width=400, height=280).pack(fill='both', expand=True)

notebook.add(frame1, text='General Profile Sett')
notebook.add(frame2, text='My profile')

window.mainloop()

错误:

Traceback (most recent call last):
  File "C:/Users/lenovo/PycharmProjects/Python_Project_1/GUI.py", line 55, in <module>
    notebook.add(frame1, text='General Profile Sett')
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\tkinter\ttk.py", line 844, in add
    self.tk.call(self._w, "add", child, *(_format_optdict(kw)))
_tkinter.TclError: wrong # args: should be ".!notebook add window ?-option value ...?"

您遇到的问题已在 Whosebug 中被多次询问:

frame1 = ttk.Frame(notebook, width=400, height=280).pack(fill='both', expand=True)
frame2 = ttk.Frame(notebook, width=400, height=280).pack(fill='both', expand=True)

frame1frame2None 因为它们是 pack(...) 而不是 ttk.Frame(...).

的结果

实际上,您根本不需要调用 pack(...),因为它们是由 notebook.add(...) 添加到笔记本中的。

frame1 = ttk.Frame(notebook, width=400, height=280)
frame2 = ttk.Frame(notebook, width=400, height=280)

notebook.add(frame1, text='General Profile Sett')
notebook.add(frame2, text='My profile')

删除第 13 行和第 14 行中的 .pack(),因为您已经在下面提到了 .add()...