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?
This question is related to
python
linux
tkinter
installation
You only need to import
it:
import tkinter as tk
then you will be use the phrase tk
, which is shorter and easier.
Also, I prefer using messagebox too:
from tkinter import messagebox as msgbx
Here's some ways you will be able to use it.
# make a new window
window = tk.Tk()
# show popup
msgbx.showinfo("title", "This is a text")
Actually, you just need to use the following to install the tkinter for python3:
sudo apt-get install python3-tk
In addition, for Fedora users, use the following command:
sudo dnf install python3-tkinter
If you have Python 3.7.2 you don't need to install anything , just type exactly:
from tkinter import *
for python3 user, install python3-tk package by following command
sudo apt-get install python3-tk
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()
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 think you have to install the tkinter onto your code repository directory.
For liunx (Ubuntu or debian), open the terminal. Install the tkinter package by running this command on the terminal.
sudo apt-get install python-tk
After installing the python-tk package on the terminal, create a new python file. Call it filename.py.
On the first line of the filename.py, import tkinter into the python file.
import tkinter
To create a tkinter with different fields, you can use this tutorial. https://www.delftstack.com/tutorial/tkinter-tutorial/
The documentation of tkinter can be found here: https://docs.python.org/3.7/library/tkinter.html
Hope that helps. Do ask us if you are still stuck. Remember to identify what you need in the GUI on tkinter before implementing it. Drawing wireframes of how the GUI will look like will help in coding the Tkinter GUI.
For Arch Linux users, it goes a bit like
sudo pacman -S tk
If, like me, you don't have root privileges on your network because of your wonderful friends in I.S., and you are working in a local install you may have some problems with the above approaches.
I spent ages on Google - but in the end, it's easy.
Download the tcl and tk from http://www.tcl.tk/software/tcltk/download.html and install them locally too.
To install locally on Linux (I did it to my home directory), extract the .tar.gz files for tcl and tk. Then open up the readme files inside the ./unix directory. I ran
cd ~/tcl8.5.11/unix
./configure --prefix=/home/cnel711 --exec-prefix=/home/cnel711
make
make install
cd ~/tk8.5.11/unix
./configure --prefix=/home/cnel711 --exec-prefix=/home/cnel711 --with-tcl=/home/cnel711/tcl8.5.11/unix
make
make install
It may seem a pain, but the files are tiny and installation is very fast.
Then re-run python setup.py build
and python setup.py install
in your python installation directory - and it should work. It worked for me - and I can now import Tkinter etc to my heart's content - yipidy-yay. An entire afternoon spent on this - hope this note saves others from the pain.
Install python version 3.6+
and open you text editor
or ide
write sample code like this:
from tkinter import *
root = Tk()
root.title("Answer")
root.mainloop()
For python 3.7 on ubuntu I had to use sudo apt-get install python3.7-tk
to make it work
The situation on macOS is still a bit complicated, but do-able:
Python.org strongly suggest downloading tkinter from ActiveState, but you should read their license first (hint: don't redistribute or want Support).
When the download is opened OS X 10.11 rejected it because it couldn't find my receipt: "ActiveTcl-8.6.pkg can’t be opened because it is from an unidentified developer".
I followed an OSXDaily fix from 2012 which suggested allowing from anywhere. But OS X has now added an "Open Anyway" option to allow (e.g.) Active-Tcl as a once off, and the "Anywhere" option has gained a timeout.
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
).
Use ntk for your desktop application, which work on top of tkinter to give you more functional and good looking ui in less codding.
install ntk by pip install ntk
proper Documentation in here: ntk.readthedocs.io
Happy codding.
There is _tkinter and Tkinter - both work on Py 3.x But to be safe- Download Loopy and change your python root directory(if you're using an IDE like PyCharms) to Loopy's installation directory. You'll get this library and many more.
For Fedora >= 25 and python >= 3, we might need to include the dependencies for Tkinter
sudo dnf install python3-tkinter
If you are using Python 3 it might be because you are typing Tkinter
not tkinter
If you're using RHEL, CentOS, Oracle Linux, etc. You can use yum to install tkinter module
yum install tkinter
Fedora release 25 (Twenty Five)
dnf install python3-tkinter
This worked for me.
For Python 2.7:
As it says here,
You don't need to download
Tkinter
- it's an integral part of all Python distributions (except binary distributions for platforms that don't support Tcl/Tk).
In my case, on Windows, what helped was reinstalling the Python distribution. A long time ago, I had unchecked the "Tcl/Tk" installation feature. After reinstalling, all works fine and I can import _tkinter
and import Tkinter
.
tk-devel also needs to be installed in my case
yum install -y tkinter tk-devel
install these and rebuild python
Source: Stackoverflow.com