尝试使用选项卡控件的 Python 错误:Tkinter 没有属性 Notebook

Python error trying to use Tab Control: Tkinter has no attribute Notebook

提问人:KyferEz 提问时间:3/21/2019 最后编辑:KyferEz 更新时间:5/30/2021 访问量:2264

问:

我正在尝试将选项卡控件添加到我的 GUI。但是,我收到此错误:AttributeError:module tkinter 没有属性“Notebook”。其他 Tkinter 对象工作得很好,例如按钮、标签、画布。

任何想法是什么导致这种情况?

另见 REPL 中的相同内容:

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> tkinter.notebook()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'tkinter' has no attribute 'notebook'
>>> tkinter.Notebook()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'tkinter' has no attribute 'Notebook'

这是我的代码:

import tkinter as tk

class Tab1():
    def __init__(self, master):
        self.frame = tk.Frame(master)
        self.frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
        self.tabControl = tk.Notebook(self.frame)

我安装了较新版本的 Python,并得到相同的结果:

Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> tkinter.Notebook()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'tkinter' has no attribute 'Notebook'

python-3.x tkinter 选项卡

评论

1赞 Bryan Oakley 3/21/2019
错误告诉你问题到底是什么:tkinter 没有笔记本。但是,ttk 模块可以。
1赞 KyferEz 3/21/2019
布莱恩,谢谢你。我回顾过的示例中没有一个解释 Notebook 是 ttk 的一部分,ttk 是 tkinter 的一个子模块。他们只是使用了 ttk,所以我认为这是他们在导入 tkinter 时使用的名称!

答:

1赞 KyferEz 3/21/2019 #1

ttk 是 tkinter 的一个子模块;它需要导入。比我以前使用的例子更好的例子:Tkinter 中的笔记本小部件

0赞 PCM 5/30/2021 #2

这是因为 Notebook Widgets 不属于 Tkinter 模块。您需要从 Tkinter 导入它。TTK的。例如-

from Tkinter import ttk

ttk.notebook(root)... 等等