在 python 2.7 中从 tkinter 导入 ttk 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23984614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
problems importing ttk from tkinter in python 2.7
提问by Benjamin Boyce
I'm working with an example file in a tutorial that asks me to first do two imports:
我正在使用教程中的示例文件,该文件要求我首先执行两次导入:
from tkinter import *
from tkinter import ttk
I get an error. I researched a bit and found that in python 2.7.x I need to capitalize the 't'in tkinter, so I change to:
我收到一个错误。我研究了一下,发现在 python 2.7.x 中我需要大写 tkinter 中的 't',所以我改为:
from Tkinter import *
from Tkinter import ttk.
the first line no longer gives and error, but I still get error:
第一行不再给出错误,但我仍然收到错误:
ImportError: cannot import name ttk.
I have researched this issue on this site and other places, and cannot seem to understand what this ttk is. I'm further confused by the fact that, when I go to the python interpreter, and I type "help()", then "modules", and then "ttk" it seems to know what it is, and gives me a lot of description, for example: "DESCRIPTION This module provides classes to allow using Tk themed widget set." -however, python won't let me import it.
我在这个站点和其他地方研究过这个问题,似乎无法理解这个 ttk 是什么。我进一步感到困惑的是,当我进入 python 解释器时,我输入“help()”,然后是“modules”,然后是“ttk”,它似乎知道它是什么,并给了我很多说明,例如:“DESCRIPTION 该模块提供允许使用 Tk 主题小部件集的类。” - 但是,python 不会让我导入它。
采纳答案by Bryan Oakley
In python 2.7, ttk is its own package:
在 python 2.7 中, ttk 是它自己的包:
import Tkinter
import ttk
This is documented in the official python documentation: https://docs.python.org/2/library/ttk.html#module-ttk
这在官方 python 文档中有记录:https: //docs.python.org/2/library/ttk.html#module-ttk
回答by Ravi Chandran
In Python 2.7.16, ttk is its own package:
在 Python 2.7.16 中,ttk 是它自己的包:
import Tkinter
导入 Tkinter
import ttk
导入 ttk
from Tkinter import *
从 Tkinter 导入 *
from ttk import *
从 ttk 导入 *