"ImportError: no module named tkinter" when using Pmw

Here is my problem: I am running the code in this example. I have Python 2.7 and 3 installed on my RaspberryPi, but I checked and double checked, and I run the code in 2.7. I installed Pmw 2.0.0 under 2.7, not 3, but when I try to start, I get the error message "ImportError: No module named tkinter". I use Tkinter all the time, so it usually works fine, and I did a search to check that I definitely call "Tkinter" and not "tkinter", so I think this should be a problem with Pmw, which also, by Apparently, indicated by tracing (described in detail at the bottom of my question). For my life, I cannot find a specific place where Pmw is looking for the lower case "tkinter", and I do not understand how to get around this. I do not want to switch to platforms - this is for work, so if it is not impossible, I need to stick to Tkinter. Oh, and I'm pretty new to Python, so I would like to know that this is a simple problem that someone here can easily notice.

import sys; print sys.path gives me:

['/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'] 

Full trace:

 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 
+6
source share
5 answers

Perhaps I can help you with how to remove the error.

here are two thoughts:

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

2) before importing, follow these steps and hope that this helps:

 #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! 
+26
source

Another workaround would be the following:

 try: import tkinter except: import Tkinter as tkinter 

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

+2
source

I ran into the same problem with matplotlib.pyplot (python 2.7+) in my CentO. I solved the problem by simply installing tkinter. sudo yum install tkinter . Hope this helps you.

+1
source

you imported the wrong module usage: import Tkinter

0
source

rewritten script that runs on python 3.4.0 ahead

 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") 
-1
source

All Articles