簡體   English   中英

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 ...?"

您遇到了在 StackOverflow 中多次被問到的問題:

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 行中的 the.pack(),因為您已經在下面提到了 that.add()...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM