在Python中导入Tkinter时出现ImportError

发布于 2024-12-06 03:33:51 字数 393 浏览 0 评论 0原文

我正在尝试使用 Python 3.2 和标准库 Tkinter 测试 GUI 代码,但无法导入该库。

这是我的测试代码:

from Tkinter import *

root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()

shell 报告此错误:

Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
from Tkinter import *
ImportError: No module named Tkinter

I'm trying to test GUI code using Python 3.2 with standard library Tkinter but I can't import the library.

This is my test code:

from Tkinter import *

root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()

The shell reports this error:

Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
from Tkinter import *
ImportError: No module named Tkinter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心碎无痕… 2024-12-13 03:33:51

问题的根源在于,Tkinter 模块在 python 2.x 中名为 Tkinter(大写“T”),在 python 3 中名为 tkinter(小写“t”) .x。

要使您的代码在 Python 2 和 3 中工作,您可以执行以下操作:

try:
    # for Python2
    from Tkinter import *
except ImportError:
    # for Python3
    from tkinter import *

但是,PEP8 关于通配符导入有这样的说法:

应避免通配符导入(fromimport *)

尽管有无数教程忽略 PEP8,但符合 PEP8 的导入方式将如下所示:

import tkinter as tk

以这种方式导入时,您需要在所有 tkinter 命令前添加前缀tk. (例如:root = tk.Tk() 等)。这将使您的代码更容易理解,但需要稍微多输入一些内容。鉴于 tkinter 和 ttk 经常一起使用并导入同名的类,这是一件好事。正如Python之禅所说:“显式优于隐式”。

注意:as tk 部分是可选的,但可以让您少输入一些内容:tk.Button(...)tkinter.Button(.. .)

The root of the problem is that the Tkinter module is named Tkinter (capital "T") in python 2.x, and tkinter (lowercase "t") in python 3.x.

To make your code work in both Python 2 and 3 you can do something like this:

try:
    # for Python2
    from Tkinter import *
except ImportError:
    # for Python3
    from tkinter import *

However, PEP8 has this to say about wildcard imports:

Wildcard imports ( from <module> import * ) should be avoided

In spite of countless tutorials that ignore PEP8, the PEP8-compliant way to import would be something like this:

import tkinter as tk

When importing in this way, you need to prefix all tkinter commands with tk. (eg: root = tk.Tk(), etc). This will make your code easier to understand at the expense of a tiny bit more typing. Given that both tkinter and ttk are often used together and import classes with the same name, this is a Good Thing. As the Zen of python states: "explicit is better than implicit".

Note: The as tk part is optional, but lets you do a little less typing: tk.Button(...) vs tkinter.Button(...)

折戟 2024-12-13 03:33:51

在 3.x 中,该模块称为 tkinter,而不是 Tkinter

The module is called tkinter, not Tkinter, in 3.x.

忆沫 2024-12-13 03:33:51

对于 3.x,使用 Tkinter 将代码重写为 tkinter(小写):

from tkinter import *

root = Tk()

w = Label(root, text="Hello, world!")
w.pack()

root.mainloop()

Rewrite the code as follows with Tkinter as tkinter (lowercase) for 3.x:

from tkinter import *

root = Tk()

w = Label(root, text="Hello, world!")
w.pack()

root.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文