【问题标题】:Python error trying to use Tab Control: Tkinter has no attribute Notebook尝试使用选项卡控件的 Python 错误:Tkinter 没有属性 Notebook
【发布时间】:2019-08-12 09:38:40
【问题描述】:

我正在尝试将选项卡控件添加到我的 GUI。但是我收到此错误:AttributeError:模块 tkinter 没有属性“笔记本”。其他 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'

【问题讨论】:

  • 错误告诉你问题出在哪里:tkinter 没有笔记本。但是,ttk 模块可以。
  • 布莱恩,谢谢。我看过的所有例子都没有说明 Notebook 是 ttk 的一部分,它是 tkinter 的一个子模块。他们只是使用 ttk,所以我以为这是他们在导入 tkinter 时使用的名称!

标签: python-3.x tkinter tabs


【解决方案1】:

ttk 是 tkinter 的子模块;它需要进口。比我以前使用的更好的示例: Notebook widget in Tkinter

【讨论】:

    【解决方案2】:

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

    from Tkinter import ttk

    ttk.notebook(root)... 等等

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 2011-10-24
      • 2013-12-11
      • 2021-07-24
      • 2015-11-20
      • 2015-07-14
      相关资源
      最近更新 更多