python - importing turtle module [duplicate] - python

Today I wanted to start working with Tkinter, but I have some problems.
Python 3.2 (r32:88445, Mar 28 2011, 04:14:07)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from tkinter import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.2/tkinter/__init__.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
So how can I configure my Python 3.2 to work with Tkinter?

Under Arch/Manjaro just install the package tk:
sudo pacman -S tk

Solution for Linux, Windows (WSL/Ubuntu) and MacOS
After trying a bunch of things, this is how it finally worked:
$ brew install python-tk

Install tk-devel (or a similarly-named package) before building Python.

To get this to work with pyenv on Ubuntu 16.04 and 18.04, I had to:
$ sudo apt-get install python-tk python3-tk tk-dev
Then install the version of Python I wanted:
$ pyenv install 3.6.2
Then I could import tkinter just fine:
import tkinter

According to http://wiki.python.org/moin/TkInter :
If it fails with "No module named _tkinter", your Python configuration needs to be modified to include this module (which is an extension module implemented in C). Do not edit Modules/Setup (it is out of date). You may have to install Tcl and Tk (when using RPM, install the -devel RPMs as well) and/or edit the setup.py script to point to the right locations where Tcl/Tk is installed. If you install Tcl/Tk in the default locations, simply rerunning "make" should build the _tkinter extension.

So appearantly many seems to have had this issue (me including) and I found the fault to be that Tkinter wasn't installed on my system when python was compiled.
This post describes how to solve the problem by:
Removing the virtual environment/python distribution
install Tkinter with sudo apt-get install tk-dev (for deb) or sudo pacman -S tk (for arch/manjaro)
Then proceed to compile python again.
This worked wonders for me.

I also faced similar problem. I resolved it by installing python-tk in my system.
Command for mac : brew install python-tk.

Had the same issue on Fedora with Python 2.7. Turns out some extra packages are required:
sudo dnf install tk-devel tkinter
After installing the packages, this hello-world example seems to be working fine on Python 2.7:
$ cat hello.py
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
$ python --version
Python 2.7.8
$ python hello.py
And through X11 forwarding, it looks like this:
Note that in Python 3, the module name is lowercase, and other packages are probably required...
from tkinter import *

Oh I just have followed the solution Ignacio Vazquez-Abrams has suggest which is install tk-dev before building the python.
(Building the Python-3.6.1 from source on Ubuntu 16.04.)
There was pre-compiled objects and binaries I have had build yesterday though, I didn't clean up the objects and just build again on the same build path. And it works beautifully.
sudo apt install tk-dev
(On the python build path)
(No need to conduct 'make clean')
./configure
make
sudo make install
That's it!

sudo apt-get install python3-tk

I encountered this issue on python 2.7.9.
To fix it, I installed tk and tcl, and then rebuild python code and reinstall, and during configure, I set the path for tk and tcl explicitly, by:
./configure --with-tcltk-includes="-I/usr/include" --with-tcltk-libs="-L/usr/lib64 -ltcl8.5 -L/usr/lib64 -ltk8.5"
Also, a whole article for python install process: Building Python from Source

Installing Tkinter
python -m pip install tk-tools
or
sudo apt install python3-tk
Code
from tkinter import *
root = Tk()
root.title('My App')
# Code
root.mainloop()

now i figured out what's going on ubuntu,
Follow these step to solve the issue
check your python version python3 --version
Lets Imagine you have python 3.10
Then Install Python-tk for the python version by using bellow command
sudo apt install python3.10-tk
simple if you have python3.8 then sudo apt install python{"use your python version here"}-tk

since I can not comment yet, here's my answer to another post:
since I'm still using python 3.9, this code works for me:
brew install python-tk#3.9
if using brew install python-tk brew will install python-tk#3.10 which is key-only

I think the most complete answer to this is the accepted answer found here:
How to get tkinter working with Ubuntu's default Python 2.7 install?
I figured it out after way too much time spent on this problem, so
hopefully I can save someone else the hassle.
I found this old bug report deemed invalid that mentioned the exact
problem I was having, I had Tkinter.py, but it couldn't find the
module _tkinter: http://bugs.python.org/issue8555
I installed the tk-dev package with apt-get, and rebuilt Python using
./configure, make, and make install in the Python2.7.3 directory. And
now my Python2.7 can import Tkinter, yay!
I'm a little miffed that the tk-dev package isn't mentioned at all in
the Python installation documentation.... below is another helpful
resource on missing modules in Python if, like me, someone should
discover they are missing more than _tkinter.

To anyone using Windows and Windows Subsystem for Linux, make sure that when you run the python command from the command line, it's not accidentally running the python installation from WSL! This gave me quite a headache just now. A quick check you can do for this is just
which <python command you're using>
If that prints something like /usr/bin/python2 even though you're in powershell, that's probably what's going on.

If you're running on an AWS instance that is running Amazon Linux OS, the magic command to fix this for me was
sudo yum install tkinter
If you want to determine your Linux build, try cat /etc/*release

This symptom can also occur when a later version of python (2.7.13, for example) has been installed in /usr/local/bin "alongside of" the release python version, and then a subsequent operating system upgrade (say, Ubuntu 12.04 --> Ubuntu 14.04) fails to remove the updated python there.
To fix that imcompatibility, one must
a) remove the updated version of python in /usr/local/bin;
b) uninstall python-idle2.7; and
c) reinstall python-idle2.7.

Even after installing python-tk, python3-tk I was getting error your python is not configured for Tk.
So I additionally installed tk8.6-dev
Then I build my Python again, run following again:
make,
make install.
When I did this I saw messages on screen that it is building _tkinter and related modules. Once that is done, I tried 'import tkinter" and it worked.

If you are using Manjaro(Arch Linux) run below command in your terminal
sudo pacman -S tk

Related

Tkinter in Python 3.10?

I am trying to use Tkinter in a Python 3.10 project. I have created a virtual environment with Python 3.10 and have installed Tkinter. When I run import tkinter I get the following error.
Traceback (most recent call last):
File "/home/xxx/yyy/main.py", line 11, in <module>
import tkinter
File "/usr/local/lib/python3.10/tkinter/__init__.py", line 37, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'
However, if I do the same outside of a virtual environment with Python 3.9, or create a new virtual environment with Python 3.9, it runs fine without error.
Is Tkinter not supported yet in 3.10, or could I be doing something else wrong?
I had the same problem.
I solved this installing python3-tk.
Depending your OS, do the appropriated command in terminal or cdm.
For example:
# UBUNTU / DEBIAN
sudo apt-get install python3-tk
# MacOS
brew install python-tk#3.10
# Fedora
sudo dnf install python3-tkinter
# CentOS
sudo yum install python3-tkinter
# Make sure to specify correct Python version:
sudo apt-get install python3.10-tk
brew install python-tk#3.9
If you are on Windows, you have to make sure to tick the checkbox tcl/tk and IDLE when installing Python.
I'm using Pop_os, a Ubuntu based linux, so sudo apt-get install python3-tk worked for me.
I found this solution in https://bobbyhadz.com/blog/python-no-module-named-tkinter
tkinter is part of python standard library but it depend on underlying components namely tcl/tk, on linux distributions. If you had installed python manually via the configure/make commands, the tkinter module build will fail unless you have the tcl/tk packages and its devel packages (tk-devel/tcl-devel usually). If that is the case just install these packages and rebuild and install python via the configure/make commands.

No module named '_tkinter' [duplicate]

This question already has answers here:
ImportError: No module named 'Tkinter'
(27 answers)
Closed 6 months ago.
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?
It is not very easy to install Tkinter locally to use with system-provided Python. You may build it from sources, but this is usually not the best idea with a binary package-based distro you're apparently running.
It's safer to apt-get install python-tk on your machine(s).
(Works on Debian-derived distributions like for Ubuntu; refer to your package manager and package list on other distributions.)
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, 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.
If you are using Python 3 it might be because you are typing Tkinter not tkinter
For Arch Linux users, it goes a bit like
sudo pacman -S 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
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.
If you're using RHEL, CentOS, Oracle Linux, etc. You can use yum to install tkinter module
yum install tkinter
For python 3.7 on ubuntu I had to use sudo apt-get install python3.7-tk to make it work
for python3 user, install python3-tk package by following command
sudo apt-get install python3-tk
tk-devel also needs to be installed in my case
yum install -y tkinter tk-devel
install these and rebuild python
Fedora release 25 (Twenty Five)
dnf install python3-tkinter
This worked for me.
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.
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).
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()
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.
For Fedora >= 25 and python >= 3, we might need to include the dependencies for Tkinter
sudo dnf install python3-tkinter
To install the Tkinter on popular Linux distros:
Debian/Ubuntu:
sudo apt install python3-tk -y
Fedora:
sudo dnf install -y python3-tkinter
Arch:
sudo pacman -Syu tk --noconfirm
REHL/CentOS6/CentOS7:
sudo yum install -y python3-tkinter
OpenSUSE:
sudo zypper in -y 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.
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.
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()
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")
Even after you've successfully imported python3 and python3-tk, it still might not work. I changed the first line of my script from:
#! /usr/bin/python
to:
#! /usr/bin/python3
It finally worked!
For Ubuntu users and Linux users in general, it's not a bad idea to read the tips I've found here:
0: Do NOT purge Python from your system otherwise it's very likely you go through hell to re-install the things you will miss, including the ubuntu-desktop.
1: Update your local software repository
sudo apt update
2: Install the stuff everybody needs:
sudo apt install software-properties-common
3: Add the Dead Snake to your repository list:
sudo add-apt-repository ppa:deadsnakes/ppa
4: Update the entire thing again because we installed stuff and added a new repo:
sudo apt update
5: Install the Python version you want:
sudo apt install python3.10
6: Check that the version you want was installed:
python3 --version
Finally, you might want to restart your PC.
You can simply install it by your package manager(pip). If you haven't installed pip yet you can download the latest version of it.
And to download tkinter
pip install tkinter
And if you are maybe using a few versions of python, you can install it in the specific version you are currently using. if it's 3.10, it is,
pip3.10 install tkinter

python 3.6.3: backspace, arrow keys not working properly [duplicate]

In shells like the interactive python shell, you can usually use the arrow keys to move around in the current line or get previous commands (with arrow-up) etc.
But after I ssh into another machine and start python there, I get sessions like:
>>> import os
>>> ^[[A
where the last character comes from arrow-up. Or, using arrow-left:
>>> impor^[[D
How can I fix this?
In the regular bash, arrow keys work fine. The weird behavior is just in the interactive python (or perl etc.) shell.
I've solved this issue by installing readline package:
pip install readline
On OS X, I have different problem.
When I using system python shell, the keys is no problem, but problem in virtualenv. I'd try to reinstall/upgrade virtualenv/readline and nothing fixed.
While I try to import readline in problem python shell, get this error message:
ImportError: dlopen(/Users/raptor/.virtualenvs/bottle/lib/python2.7/lib-dynload/readline.so, 2): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
Referenced from: /Users/raptor/.virtualenvs/bottle/lib/python2.7/lib-dynload/readline.so
Reason: image not found
Cause there is /usr/local/opt/readline/lib/libreadline.7.dylib but not libreadline.6.dylib, so I make a symbol link:
ln -s libreadline.7.dylib libreadline.6.dylib
Problem has been solved!
On OS X, Xcode updates sometimes break readline. Solution:
brew uninstall readline
brew upgrade python3
brew install readline
pip3 install readline
If the problem still persists, try to remove readline using pip and install it using easy_install:
pip3 uninstall readline
easy_install readline
Looks like readline is not enabled. Check if PYTHONSTARTUP variable is defined, for me it points to /etc/pythonstart and that file is executed by the python process before going interactive, which setups readline/history handling.
Thanks to #chown here is the docs on this: http://docs.python.org/2/tutorial/interactive.html
On OS X, using python 3.5 and virtualenv
$ pip install gnureadline
In the interpreter do:
import gnureadline
Now arrow keys should work properly.
Additional information...
Note that as of Oct 1, 2015 - readline has been DEPRECATED (source https://github.com/ludwigschwardt/python-readline)
Use gnureadline instead (see: https://github.com/ludwigschwardt/python-gnureadline)
If I install readline instead of gnureadline using python 3.5, I receive errors after attempt to import in the interpreter:
>>> import readline
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dlopen(/Users/pi/tmp/python-readline-test/.venv/lib/python3.5/readline.so, 2): Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib
Referenced from: /Users/pi/tmp/python-readline-test/.venv/lib/python3.5/readline.so
Reason: image not found
I have run into this issue recently and after reading a lot about pip install readline (does not work for mac osx) and pip install gnureadline and not being satisfied, this is now my setup which enables using arrow keys in any python console:
install gnureadline using pip install gnureadline
now you can either do import gnureadline and arrow keys should work as expected. To make them work automatically follow the following steps:
create (or append to) file ~/.startup.py: import gnureadline
append to file ~/.bash_profile: export PYTHONSTARTUP=~/.startup.py
One thing that does not work, but did in my previous setup is: automatic import of gnureadline on pdb.set_trace(). If anyone has a good solution to this problem I would be grateful for a comment.
I had problems with shell history(tab/arrows commands) of Python 3.6.x on Ubuntu 16.04 LTS.
Python 3.6.x was installed from source.
What solved for me was install the module "gnureadline" as said by user12345, using this command line:
sudo pip3.6 install gnureadline
:)
install readline-devel package.
recompile python with readline module
Bingo!
Here are the steps which worked for me in ubuntu 12.04 for python 3.3.
1) open teminal and write sudo apt-get install libreadline-dev
2) download the source file of python 3.3.2 from http://www.python.org/ftp/python/3.3.2/Python-3.3.2.tar.xz
3) extract it and navigate to the Python-3.3.2/ directory in a shell
4) execute the following command:
./configure
make
make test
sudo make install
Was impacted after upgrading Mac to High Sierra, this successfully resolved it for me:
brew unlink python
xcode-select --install
brew install python
On CentOS, I fix this by
yum install readline-devel
and then recompile python 3.4.
On OpenSUSE, I fix this by
pip3 install readline
following Valerio Crini's answer.
Perhaps "pip3 install readline" is a general solution. Haven't tried on my CentOS.
If you use Anaconda Python, you can fix this by running:
conda install readline
Worked for me!
readline module has been deprecated which will cause invalid pointer error in latest python versions when executing quit() or exit() in python shell.
pip install gnureadline instead
None of these answers worked for me on two different version of Ubuntu. What worked for me, but isn't a true fix, is wrapping my python code in a call to rlwrap (available in the ubuntu repositories):
rlwrap python mycode.py
I fixed this by doing the following:
yum install readline-devel
pip install readline
I encountered another error here:
gcc: readline/libreadline.a: No such file or directory
gcc: readline/libhistory.a: No such file or directory
I fixed this by installing patch:
yum install patch
After that I managed to run pip install readline successfully which solved the escape characters in my python shell.
FYI, I'm using RedHat
For those using conda, installing the readline package from conda-forge channel will fix the problem:
conda install -c conda-forge readline=6.2
Did you call ssh with the -t parameter to tell ssh to allocate a virtual terminal for you?
From the man page:
-t
Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
Additionally you may also have to set the TERM environment variable on the server correctly as suggested in another post.
On Mac OS X Mojave 10.14.6 with various historical installs via brew I solved this with:
brew reinstall python2
There is likely no magic bullet given everyone has a different install scenario. I tried the above as well so it may have been a combination of a few of the answers. Brew defaults to python3 so if you installed the python2 package it also needs to be reinstalled.
Have you tried using a different SSH client? Some SSH clients have special, built-in keymappings for different remote processes. I ran into this one a lot with emacs.
What client are you using? I'd recommend trying Putty and SecureCRT to compare their behavior.
How's your env variable $TERM set [a] when things work fine and [b] when they don't? Env settings are often the key to such problems.
Try getting a key code library running on the server. If that does not work try to download a library with read-key ability.
I was trying build Python 2.7 on Ubuntu 14.0. You will need libreadline-dev. However, if you get it from apt-get, the current version is 6.3, which is incompatible with Python 2.7 (not sure about Python 3). For example, the data type "Function" and "CPPFunction", which were defined in previous versions of readline has been removed in 6.3, as reported here:
https://github.com/yyuu/pyenv/issues/126
That is to say you need to get the source code of an earlier version of readline. I installed libreadline 5.2 from apt-get for the library, and get the source code of 5.2 for the header files. Put them in /usr/include.
Finally the issue has been resolved.
On MacOsx, I fixed this by reinstalling readline
brew reinstall readline
In Unbuntu or Mint,
if you are using pyenv,
sudo apt install libreadline-dev
pyenv uninstall 3.8.8
pyenv install 3.8.8
Once installing libreadline-dev, you don't need to install pip install gnureadline on every python version.
you can switch from 'sh" to "bash" by
$ /sh/bash

Tkinter: "Python may not be configured for Tk"

Today I wanted to start working with Tkinter, but I have some problems.
Python 3.2 (r32:88445, Mar 28 2011, 04:14:07)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from tkinter import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.2/tkinter/__init__.py", line 39, in <module>
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
So how can I configure my Python 3.2 to work with Tkinter?
Under Arch/Manjaro just install the package tk:
sudo pacman -S tk
Solution for Linux, Windows (WSL/Ubuntu) and MacOS
After trying a bunch of things, this is how it finally worked:
$ brew install python-tk
Install tk-devel (or a similarly-named package) before building Python.
To get this to work with pyenv on Ubuntu 16.04 and 18.04, I had to:
$ sudo apt-get install python-tk python3-tk tk-dev
Then install the version of Python I wanted:
$ pyenv install 3.6.2
Then I could import tkinter just fine:
import tkinter
According to http://wiki.python.org/moin/TkInter :
If it fails with "No module named _tkinter", your Python configuration needs to be modified to include this module (which is an extension module implemented in C). Do not edit Modules/Setup (it is out of date). You may have to install Tcl and Tk (when using RPM, install the -devel RPMs as well) and/or edit the setup.py script to point to the right locations where Tcl/Tk is installed. If you install Tcl/Tk in the default locations, simply rerunning "make" should build the _tkinter extension.
So appearantly many seems to have had this issue (me including) and I found the fault to be that Tkinter wasn't installed on my system when python was compiled.
This post describes how to solve the problem by:
Removing the virtual environment/python distribution
install Tkinter with sudo apt-get install tk-dev (for deb) or sudo pacman -S tk (for arch/manjaro)
Then proceed to compile python again.
This worked wonders for me.
I also faced similar problem. I resolved it by installing python-tk in my system.
Command for mac : brew install python-tk.
Had the same issue on Fedora with Python 2.7. Turns out some extra packages are required:
sudo dnf install tk-devel tkinter
After installing the packages, this hello-world example seems to be working fine on Python 2.7:
$ cat hello.py
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
$ python --version
Python 2.7.8
$ python hello.py
And through X11 forwarding, it looks like this:
Note that in Python 3, the module name is lowercase, and other packages are probably required...
from tkinter import *
Oh I just have followed the solution Ignacio Vazquez-Abrams has suggest which is install tk-dev before building the python.
(Building the Python-3.6.1 from source on Ubuntu 16.04.)
There was pre-compiled objects and binaries I have had build yesterday though, I didn't clean up the objects and just build again on the same build path. And it works beautifully.
sudo apt install tk-dev
(On the python build path)
(No need to conduct 'make clean')
./configure
make
sudo make install
That's it!
sudo apt-get install python3-tk
I encountered this issue on python 2.7.9.
To fix it, I installed tk and tcl, and then rebuild python code and reinstall, and during configure, I set the path for tk and tcl explicitly, by:
./configure --with-tcltk-includes="-I/usr/include" --with-tcltk-libs="-L/usr/lib64 -ltcl8.5 -L/usr/lib64 -ltk8.5"
Also, a whole article for python install process: Building Python from Source
Installing Tkinter
python -m pip install tk-tools
or
sudo apt install python3-tk
Code
from tkinter import *
root = Tk()
root.title('My App')
# Code
root.mainloop()
now i figured out what's going on ubuntu,
Follow these step to solve the issue
check your python version python3 --version
Lets Imagine you have python 3.10
Then Install Python-tk for the python version by using bellow command
sudo apt install python3.10-tk
simple if you have python3.8 then sudo apt install python{"use your python version here"}-tk
since I can not comment yet, here's my answer to another post:
since I'm still using python 3.9, this code works for me:
brew install python-tk#3.9
if using brew install python-tk brew will install python-tk#3.10 which is key-only
I think the most complete answer to this is the accepted answer found here:
How to get tkinter working with Ubuntu's default Python 2.7 install?
I figured it out after way too much time spent on this problem, so
hopefully I can save someone else the hassle.
I found this old bug report deemed invalid that mentioned the exact
problem I was having, I had Tkinter.py, but it couldn't find the
module _tkinter: http://bugs.python.org/issue8555
I installed the tk-dev package with apt-get, and rebuilt Python using
./configure, make, and make install in the Python2.7.3 directory. And
now my Python2.7 can import Tkinter, yay!
I'm a little miffed that the tk-dev package isn't mentioned at all in
the Python installation documentation.... below is another helpful
resource on missing modules in Python if, like me, someone should
discover they are missing more than _tkinter.
To anyone using Windows and Windows Subsystem for Linux, make sure that when you run the python command from the command line, it's not accidentally running the python installation from WSL! This gave me quite a headache just now. A quick check you can do for this is just
which <python command you're using>
If that prints something like /usr/bin/python2 even though you're in powershell, that's probably what's going on.
If you're running on an AWS instance that is running Amazon Linux OS, the magic command to fix this for me was
sudo yum install tkinter
If you want to determine your Linux build, try cat /etc/*release
This symptom can also occur when a later version of python (2.7.13, for example) has been installed in /usr/local/bin "alongside of" the release python version, and then a subsequent operating system upgrade (say, Ubuntu 12.04 --> Ubuntu 14.04) fails to remove the updated python there.
To fix that imcompatibility, one must
a) remove the updated version of python in /usr/local/bin;
b) uninstall python-idle2.7; and
c) reinstall python-idle2.7.
Even after installing python-tk, python3-tk I was getting error your python is not configured for Tk.
So I additionally installed tk8.6-dev
Then I build my Python again, run following again:
make,
make install.
When I did this I saw messages on screen that it is building _tkinter and related modules. Once that is done, I tried 'import tkinter" and it worked.
If you are using Manjaro(Arch Linux) run below command in your terminal
sudo pacman -S tk

Install tkinter for Python [duplicate]

This question already has answers here:
ImportError: No module named 'Tkinter'
(27 answers)
Closed 6 months ago.
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?
It is not very easy to install Tkinter locally to use with system-provided Python. You may build it from sources, but this is usually not the best idea with a binary package-based distro you're apparently running.
It's safer to apt-get install python-tk on your machine(s).
(Works on Debian-derived distributions like for Ubuntu; refer to your package manager and package list on other distributions.)
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, 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.
If you are using Python 3 it might be because you are typing Tkinter not tkinter
For Arch Linux users, it goes a bit like
sudo pacman -S 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
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.
If you're using RHEL, CentOS, Oracle Linux, etc. You can use yum to install tkinter module
yum install tkinter
For python 3.7 on ubuntu I had to use sudo apt-get install python3.7-tk to make it work
for python3 user, install python3-tk package by following command
sudo apt-get install python3-tk
tk-devel also needs to be installed in my case
yum install -y tkinter tk-devel
install these and rebuild python
Fedora release 25 (Twenty Five)
dnf install python3-tkinter
This worked for me.
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.
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).
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()
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.
For Fedora >= 25 and python >= 3, we might need to include the dependencies for Tkinter
sudo dnf install python3-tkinter
To install the Tkinter on popular Linux distros:
Debian/Ubuntu:
sudo apt install python3-tk -y
Fedora:
sudo dnf install -y python3-tkinter
Arch:
sudo pacman -Syu tk --noconfirm
REHL/CentOS6/CentOS7:
sudo yum install -y python3-tkinter
OpenSUSE:
sudo zypper in -y 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.
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.
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()
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")
Even after you've successfully imported python3 and python3-tk, it still might not work. I changed the first line of my script from:
#! /usr/bin/python
to:
#! /usr/bin/python3
It finally worked!
For Ubuntu users and Linux users in general, it's not a bad idea to read the tips I've found here:
0: Do NOT purge Python from your system otherwise it's very likely you go through hell to re-install the things you will miss, including the ubuntu-desktop.
1: Update your local software repository
sudo apt update
2: Install the stuff everybody needs:
sudo apt install software-properties-common
3: Add the Dead Snake to your repository list:
sudo add-apt-repository ppa:deadsnakes/ppa
4: Update the entire thing again because we installed stuff and added a new repo:
sudo apt update
5: Install the Python version you want:
sudo apt install python3.10
6: Check that the version you want was installed:
python3 --version
Finally, you might want to restart your PC.
You can simply install it by your package manager(pip). If you haven't installed pip yet you can download the latest version of it.
And to download tkinter
pip install tkinter
And if you are maybe using a few versions of python, you can install it in the specific version you are currently using. if it's 3.10, it is,
pip3.10 install tkinter

Categories