Python 使用 Pmw 时出现“ImportError: No module named tkinter”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14587980/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:54:22  来源:igfitidea点击:

"ImportError: No module named tkinter" when using Pmw

pythonpython-2.7tkinterimporterror

提问by emily belzer

Here's my problem: I'm running the code in thisexample. I have Python 2.7 and 3 installed on my RaspberryPi but I have checked and double-checked, and I am running the code in 2.7. I've installed Pmw 2.0.0 under 2.7, not 3, but when I try to run, I get the "ImportError: No module named tkinter" error. I use Tkinter all the time, so it usually works fine, and I've done a search to verify that I'm definitely calling "Tkinter", not "tkinter", so I think it has to be a problem with Pmw, which also seems to be indicated by the traceback (posted in full at the bottom of my question). I can't for the life of me find a specific place where Pmw is looking for lower-case "tkinter", and I'm at a loss for how to work around this. I'm not eager to switch platforms--this is for work, so unless this is unfixable, I need to stick with Tkinter. Oh, and I am pretty new to Python, so I would love to find out that it's a simple problem that someone on here can spot easily.

这是我的问题:我在运行代码例子。我在 RaspberryPi 上安装了 Python 2.7 和 3,但我已经检查并仔细检查过,并且我正在 2.7 中运行代码。我已经在 2.7 下安装了 Pmw 2.0.0,而不是 3,但是当我尝试运行时,出现“ImportError: No module named tkinter”错误。我一直在使用 Tkinter,所以它通常可以正常工作,并且我已经进行了搜索以验证我确实在调用“Tkinter”,而不是“tkinter”,所以我认为它必须是 Pmw 的问题,其中回溯似乎也表明(在我的问题底部完整发布)。我一生都找不到 Pmw 正在寻找小写“tkinter”的特定地方,而且我不知道如何解决这个问题。我并不急于切换平台——这是为了工作,所以除非这是无法修复的,我需要坚持使用 Tkinter。哦,我对 Python 还很陌生,所以我很想知道这是一个简单的问题,这里的人可以很容易地发现。

import sys; print sys.path gives me:

导入系统;打印 sys.path 给了我:

['/home/pi/Desktop', '/home/pi', '/usr/bin', '/usr/local/lib/python2.7/dist-packages/distribute-0.6.28-py2.7.egg', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/pymodules/python2.7']

The full traceback is:

完整的回溯是:

Traceback (most recent call last):
  File "/home/pi/Desktop/LinkedMenusSample.py", line 151, in <module>
    Pmw.initialise(root)
  File "/usr/local/lib/python2.7/dist-packages/Pmw/Pmw_2_0_0/lib/PmwLoader.py", line 131, in __getattr__
    self._initialise()
  File "/usr/local/lib/python2.7/dist-packages/Pmw/Pmw_2_0_0/lib/PmwLoader.py", line 89, in _initialise
    raise ImportError(msg)
ImportError: No module named tkinter

采纳答案by User

Maybe I can help you on how to remove the error.

也许我可以帮助您解决错误。

here are two thoughts:

这里有两个想法:

1) you use python 2.xx and have installed the python 3 pwm module (Tkinter was renamed to tkinter from Python 2 to 3)

1)您使用python 2.xx并安装了python 3 pwm模块(Tkinter从Python 2到3重命名为tkinter)

2) you do the following before the import and hope it helps:

2)您在导入之前执行以下操作并希望它有所帮助:

#import tkinter
#Traceback (most recent call last):
#  File "<pyshell#11>", line 1, in <module>
#    import tkinter
#ImportError: No module named tkinter

import sys, Tkinter
sys.modules['tkinter'] = Tkinter # put the module where python looks first for modules
#import tkinter # now works!

回答by Aman Jain

you have imported wrong module use: import Tkinter

您导入了错误的模块使用:import Tkinter

回答by MarkWatney

Another workaround would be the following:

另一种解决方法如下:

try:
    import tkinter
except:
    import Tkinter as tkinter

This way you would always have the module tkinter available and depending on the Python version your program loads tkinter or Tkinter.

通过这种方式,您将始终拥有可用的模块 tkinter,并且您的程序将根据 Python 版本加载 tkinter 或 Tkinter。

回答by Shaun Wainwright

rewritten script that runs on python 3.4.0 onwards

在 python 3.4.0 以上运行的重写脚本

def add():
        print ("Enter the two numbers to Add")
        A=int(input("Enter A: "))
        B=int(input("Enter B: "))
        return A + B 

def sub():
        print ("Enter the two numbers to Subtract")
        A=int(input("Enter A: "))
        B=int(input("Enter B: "))
        return A - B

def mul():
        print ("Enter the two numbers to Multiply")
        A=int(input("Enter A: "))
        B=int(input("Enter B: "))
        return A * B

def div():
        print ("Enter the two number to Divide")
        A=float(input("Enter A: "))
        B=float(input("Enter B: "))
        return A / B

print ("1: ADDITION")
print ("2: SUBTRACTION")
print ("3: MULTIPLICATION")
print ("4: DIVISION")
print ("0: QUIT")

while True:

    CHOICE = int(input("ENTER THE CORRESPONDING NUMBER FOR CALCULATION ")) 

    if CHOICE == 1: 
        print ('ADDING TWO NUMBERS:')
        print (add())

    elif CHOICE == 2:
        print ('SUBTRACTING TWO NUMBERS')
        print (sub())

    elif CHOICE == 3:
        print ('MULTIPLYING TWO NUMBERS')
        print (mul())

    elif CHOICE == 4:
        print ("DIVIDEING TWO NUMBERS")
        print (div())

    elif CHOICE == 0:
        exit()
    else:
        print ("The value Enter value from 1-4")

回答by Amrendra

I was facing the same problem with matplotlib.pyplot(python 2.7+) in my CentOs. I solved the problem by just installing the tkinter. sudo yum install tkinter. Hope this can help you.

我在matplotlib.pyplotCentO 中遇到了(python 2.7+)同样的问题。我通过安装 tkinter 解决了这个问题。sudo yum install tkinter. 希望这可以帮到你。