I am trying to import Tkinter. However, I get an error stating that Tkinter has not been installed:
ImportError: No module named _tkinter, please install the python-tk package
I could probably install it using synaptic manager (can I?), however, I would have to install it on every machine I program on. Would it be possible to add the Tkinter library into my workspace and reference it from there?
Tkinter is a GUI module for python. you can use it to make GUI based applications in python. Tkinter provides several GUI widgets like buttons,menu, canvas,text,frame,label etc. to develop desktop applications.Though Tkinter is very popular and is included with windows, macosx install of python, There are also alternative choices like pyQt, wxPython...
In this tutorial we will see how to install it on linux and use it with an example.
First, check if you have python installed and also check its version
Open up your terminal and type python. if its installed then it will show information like version, help... check your version (mine is python 2.7.9)
aman@vostro:~$ python Python 2.7.9 (default, Apr 2 2015, 15:33:21) [GCC 4.9.2] on linux2 Type "help", "copyright", "credits" or "license" for more information.
If you don't have python then install it sudo apt-get install python
If you want to install python 3 then enter the following. If you are a newbie, I would recommend python 2 instead of python 3. Python 2 is still very popular and many apps are made on it. On ubuntu python2 is still the default sudo apt-get install python3
Finally, Install Tkinter
for python 3
How to Use it
Now, lets check if Tkinter is working well with this little example
open your terminal and enter into your python shell. python
for python3 python3
if python was installed correctly you will get a >>> prompt. aman@vostro:~$ python
Now import Tkinter module. it wont show any error if it got imported correctly. NOTE: Make sure you type Tkinter (not tkinter) in python2 and tkinter (not Tkinter) in python3.
Now, just to check you can create an empty window using Tkinter.
If you're using
Python 3
then you must install as follows:Tkinter for
Python 2
(python-tk
) is different fromPython 3
's (python3-tk
).you will need the package and its dependencies.
since you mentioned synaptic, you must be using a Debian based system. one way to get what you need:
for python3 user, install python3-tk package by following command
sudo apt-get install python3-tk
Fedora release 25 (Twenty Five)
This worked for me.
If you're using RHEL, CentOS, Oracle Linux, etc. You can use yum to install tkinter module