Install tkinter for Python

2019-01-02 22:32发布

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?

16条回答
趁早两清
2楼-- · 2019-01-02 22:50

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

sudo apt-get install python-tk

for python 3

sudo apt-get install python3-tk

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

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.
>>>

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.

>>>import Tkinter

Now, just to check you can create an empty window using Tkinter.

>>>Tkinter.Tk()
查看更多
干净又极端
3楼-- · 2019-01-02 22:52

If you're using Python 3 then you must install as follows:

sudo apt-get update
sudo apt-get install python3-tk

Tkinter for Python 2 (python-tk) is different from Python 3's (python3-tk).

查看更多
三岁会撩人
4楼-- · 2019-01-02 22:55

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:

sudo apt-get install python-tk
查看更多
你好瞎i
5楼-- · 2019-01-02 22:56

for python3 user, install python3-tk package by following command sudo apt-get install python3-tk

查看更多
The star\"
6楼-- · 2019-01-02 22:58

Fedora release 25 (Twenty Five)

dnf install python3-tkinter

This worked for me.

查看更多
Deceive 欺骗
7楼-- · 2019-01-02 22:59

If you're using RHEL, CentOS, Oracle Linux, etc. You can use yum to install tkinter module

yum install tkinter
查看更多
登录 后发表回答