简体   繁体   English

为什么 tkinter ttk 在 python 3.5.1 中显示“名称 ttk 未定义”

[英]why does tkinter ttk showing “name ttk is not defined” in python 3.5.1

Consider this simple code:考虑这个简单的代码:

from tkinter import *
from tkinter.ttk import *
root= Tk()
ttk.Label(root, text='Heading Here').grid(row=1, column=1)
ttk.Separator(root,orient=HORIZONTAL).grid(row=2, columnspan=5)
root.mainloop()

when i run this code it's showing error当我运行此代码时,它显示错误

ttk.Label(root, text='Heading Here').grid(row=1, column=1)
NameError: name 'ttk' is not defined

When you do import X , you are importing a module named X .当您import X ,您正在导入一个名为X的模块。 From this point on, X will be defined.从这一点开始, X将被定义。

When you do from X import * , you are not importing X , you are only importing the things that are inside of X .当你from X import * ,你是不是进口X ,您只导入一个是内部的事情X X itself will be undefined. X本身将是未定义的。

Thus, when you do from tkinter.ttk import * , you are not importing ttk , you are only importing the things in ttk.因此,当你from tkinter.ttk import * ,你是不是进口ttk ,你只导入东西TTK。 This will import things such as Label , Button , etc, but not ttk itself.这将导入诸如LabelButton等内容,但不会ttk本身。

The proper way to import ttk in python3 is with the following statement:在python3中导入ttk的正确方法是使用以下语句:

from tkinter import ttk  

With that, you can reference the ttk label with ttk.Label , the ttk button as ttk.Button , etc.这样,您可以使用ttk.Label引用 ttk 标签,将 ttk 按钮引用为ttk.Button等。

Note : doing from tkinter.ttk import * is dangerous.注意:执行from tkinter.ttk import *是危险的。 Unfortunately, ttk and tkinter both export classes with the same name.不幸的是, ttktkinter两个出口类具有相同的名称。 If you do both from tkinter import * and from tkinter.ttk import * , you will be overriding one class with another.如果您同时执行from tkinter.ttk import * from tkinter import *from tkinter.ttk import * ,您将用另一个类覆盖一个类。 The order of the imports will change how your code behaves.导入的顺序将改变您的代码的行为方式。

For this reason -- particularly in the case of tkinter and ttk which each have several classes that overlap -- wildcard imports should be avoided.出于这个原因——特别是在 tkinter 和 ttk 的情况下,每个类都有几个重叠的类——应该避免通配符导入。 PEP8 , the official python style guide, officially discourages wildcard imports: PEP8 ,官方 python 风格指南,正式不鼓励通配符导入:

Wildcard imports ( from import * ) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools.应避免通配符导入( from import * ),因为它们使命名空间中存在哪些名称变得不清楚,使读者和许多自动化工具都感到困惑。


Note: your question implies you're using python 3, but in case you're using python 2 you can just do import ttk rather than from tkinter import ttk .注意:您的问题暗示您使用的是 python 3,但如果您使用的是 python 2,您可以只执行import ttk而不是from tkinter import ttk ttk moved in python 3. ttk在 python 3 中移动。

To import ttk , replace the following line:要导入ttk ,请替换以下行:

from tkinter.ttk import *

with:与:

from tkinter import ttk

Otherwise, attributes of tkinter.ttk module will be loaded into the current module namespace instead of ttk itself.否则, tkinter.ttk模块的属性将被加载到当前模块命名空间而不是ttk本身。

When you are importing the ttk module, you can do it in 2 ways -当您导入 ttk 模块时,您可以通过两种方式进行 -

  1. from tkinter import ttk When you do this, ttk is imported almost like a variable, so you can use that ttk.Label from tkinter import ttk当你这样做时,ttk 几乎像一个变量一样被导入,所以你可以使用那个ttk.Label

  2. from tkinter import * This is called wildcard import. from tkinter import *这称为通配符导入。 You can't use ttk.Label you have to directly write Label(options)你不能使用ttk.Label你必须直接写Label(options)

ttk.Label(root, text='HeadingHere').grid(row=1, column=1) 
NameError: name 'ttk' is not defined
In this remove ttk as follows.    
Label(root, text='HeadingHere').grid(row=1, column=1

Now it works fine现在它工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM