【问题标题】:Why does Python 3's tkinter importing syntax seem to work in Python 2?为什么 Python 3 的 tkinter 导入语法似乎在 Python 2 中有效?
【发布时间】:2023-03-24 20:05:01
【问题描述】:

我正在通过 Anaconda 运行 Python 2.7,据我所知没有安装 Python 3。我对导入tkinter 感到困惑。 Stack Overflow 上的其他几个问题表明,tkinter 有单独的模块和略有不同的导入语法,具体取决于您运行的是 Python 2 还是 Python 3。但是,Python 3 语法 有点 有效在 Python 2 中对我来说(参见下面代码中的 cmets)。什么给了?

import sys
print sys.version
# prints: 2.7.12 |Continuum Analytics, Inc.| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)]

# I hear these should not work in Python 2.  
# In reality, they work fine if run normally via the Python 2 interpreter.
# However, they do NOT work when I use pyinstaller to make an executable.
from tkinter import *
from tkinter import ttk, messagebox

# These work fine in Python 2, as they should, even if compiled into an exe.
from Tkinter import *
import ttk
import tkMessageBox

编辑:

针对 Bryan Oakley 的评论,print sys.path 的结果是:

['C:\\Users\\...\\tkinter test program', 
'C:\\Miniconda\\python27.zip', 
'C:\\Miniconda\\DLLs', 
'C:\\Miniconda\\lib', 
'C:\\Miniconda\\lib\\plat-win', 
'C:\\Miniconda\\lib\\lib-tk', 
'C:\\Miniconda', 
'C:\\Miniconda\\lib\\site-packages', 
'C:\\Miniconda\\lib\\site-packages\\win32', 
'C:\\Miniconda\\lib\\site-packages\\win32\\lib', 
'C:\\Miniconda\\lib\\site-packages\\Pythonwin', 
'C:\\Miniconda\\lib\\site-packages\\setuptools-23.0.0-py2.7.egg']

针对 Sun Bear 的回答,我的电脑上发生了以下情况:

C:\>python
Python 2.7.12 |Continuum Analytics, Inc.| (default, Jun 29 2016, 11:07:13) [MSC
v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import tkinter
>>>

回应 Łukasz Rogalski 的评论:

>>> import Tkinter
>>> print Tkinter.__file__
C:\Miniconda\lib\lib-tk\Tkinter.pyc
>>> import tkinter
>>> print tkinter.__file__
C:\Miniconda\lib\site-packages\tkinter\__init__.pyc
>>>

针对 Sun Bear 回答的 cmets 中的讨论,这是 C:\Miniconda\lib\site-packages\tkinter\__init__.pyc 的内容,这解释了为什么即使我使用的是 Python 2,import tkinter 也能工作:

from __future__ import absolute_import
import sys

if sys.version_info[0] < 3:
    from Tkinter import *
else:
    raise ImportError('This package should not be accessible on Python 3. '
                      'Either you are trying to run from the python-future src folder '
                      'or your installation of python-future is corrupted.')

【问题讨论】:

  • 在 macOS 上不适合我(“ImportError: No module named tkinter”)。
  • Windows 上不区分大小写的文件系统,也许吧?但是一个区分大小写的 .zip 存档,当捆绑到可执行文件中时......
  • 我不认为这是不区分大小写的问题,因为ttkmessagebox 仅在tkinter 中,而不是Tkinter。这失败了:from Tkinter import ttk
  • 通过任何更改,您在工作目录中有一个名为 tkinter.py 的文件?
  • 打印出sys.path。很可能,您的路径中有 python 3 库的位置。

标签: python tkinter python-2.x pyinstaller


【解决方案1】:

tkinter 是适用于 Python 3 语法的 Tk 的 Python 3 包装层。
Tkinter 是适用于 Python 2 语法的 Tk 的 Python 2 包装层。

导入命令也是一样的。但是您需要为用于编写代码的 python 版本类型导入正确的包装器。下面显示 python 2 只能导入 Tkinter 而不是 tkinter

$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named tkinter
>>> import Tkinter
>>> 

更新:如果 Miniconda 选择偏离主流实践并在幕后做一些事情以允许您所描述的内容,则由 Miniconda 自行决定。你的print tkinter.__file__ 命令显示你有tkinterprint Tkinter.__file__ 命令显示你有Tkinter。简而言之,Miniconda 预装了Tkintertkinter。您是否尝试过比较这两个文件以查看它们是否相同或不同?

【讨论】:

  • 这不是在我的计算机上发生的。查看我的编辑。
  • @MarredCheese 你能找到你的 lib-tk 文件夹,看看 tkinter.py 和 Tkinter.py 是否都在那里吗?同时打开这些文件并比较它们的语法。
  • 我有两个 lib-tk 文件夹,但它们里面都只有 Tkinter.py,而不是 tkinter.py。一个在C:\Miniconda\Lib,另一个在C:\Miniconda\pkgs\python-2.7.12-0\Lib
  • @MarredCheese 这意味着你正在使用 Tkinter。我怀疑 Miniconda 有一个脚本可以将 tkinter 等同于 Tkinter.... 同意吗?
  • from Tkinter import ttk 失败,而 from tkinter import ttk 有效。由于 ttk 是 tkinter 而不是 Tkinter 的一部分,因此这不支持 import tkinter 在幕后偷偷导入 Tkinter 的想法。
猜你喜欢
  • 2013-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-29
  • 2017-05-18
  • 1970-01-01
  • 2020-10-21
  • 2015-11-22
相关资源
最近更新 更多