sudo apt-get install python3-tk Installs tkinter for Python3.x.x
Sources:
https://stackoverflow.com/questions/6084416/tkinter-module-not-found-on-ubuntu https://pythonprogramming.net/python-3-tkinter-basics-tutorial/
Answer from Elder Geek on askubuntu.comsudo apt-get install python3-tk Installs tkinter for Python3.x.x
Sources:
https://stackoverflow.com/questions/6084416/tkinter-module-not-found-on-ubuntu https://pythonprogramming.net/python-3-tkinter-basics-tutorial/
For a python 3.6 virtual environment with Python 3.5 as the "system" python (Ubuntu) , I had to install tk 3.6 to match;
sudo apt-get install python3.6-tk
My default python in ubuntu was 3.5, so when using a venv for 3.6, I would get an import error. Drove me crazy for a while.
[Edit: As this question ages]
In general, it seems if one is using a virtual environment with a python of a different version than that of ones base machine, one must install tk for the version of python used in the virtual environment.
sudo apt install python3.x-tk
Where 3.x would match the version of the virtual environment.
[Edit]
It may be necessary to add the repository for tk. In my experience, it was the same repository from which I pulled python3.6, but that may change with time;
sudo add-apt-repository ppa:deadsnakes/ppa
How do i install Tkinter
python - ImportError: No module named '_tkinter', please install the python3-tk package - Stack Overflow
python - Can't import tkinter (or Tkinter) - Stack Overflow
linux - Install tkinter for Python - Stack Overflow
Videos
How do i install Tkinter i watched tutorials but it doesnt match mine python 3.10
When you import matplotlib, it will probably be trying to use the tk backend as the default. If you didn't install tk, or you do not want to use it anywhere else in your project, then a possible solution would be to simply use a different backend:
import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
The message indicates that when you run sudo apt-get install python3-tk it tells you that tkinter is sintalled for Python3.6.5, but on the other hand, the ImportError is related to Python3.5. So I believe this should resolve your problem:
sudo apt-get install python3.5-tk
First try using this code for your imports.
try:
import Tkinter as tk # this is for python2
except:
import tkinter as tk # this is for python3
If this doesn't work, try reinstalling tkinter. If you don't know how to reinstall tkinter look at the tkinter installation page, here.
If you are using Ubuntu or Debian OS try this:-
sudo apt-get install python-tk
Or if you are using Python 3:-
sudo apt-get install python3-tk
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 python3-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
from Tkinter import * imports every exposed object in Tkinter into your current namespace.
import Tkinter imports the "namespace" Tkinter in your namespace and
import Tkinter as tk does the same, but "renames" it locally to 'tk' to save you typing
let's say we have a module foo, containing the classes A, B, and C.
Then import foo gives you access to foo.A, foo.B, and foo.C.
When you do import foo as x you have access to those too, but under the names x.A, x.B, and x.C.
from foo import * will import A, B, and C directly in your current namespace, so you can access them with A, B, and C.
There is also from foo import A, C wich will import A and C, but not B into your current namespace.
You can also do from foo import B as Bar, which will make B available under the name Bar (in your current namespace).
So generally: when you want only one object of a module, you do from module import object or from module import object as whatiwantittocall.
When you want some modules functionality, you do import module, or import module as shortname to save you typing.
from module import * is discouraged, as you may accidentally shadow ("override") names, and may lose track which objects belong to wich module.
You can certainly use
import Tkinter
However, if you do that, you'd have to prefix every single Tk class name you use with Tkinter..
This is rather inconvenient.
On the other hand, the following:
import Tkinter as tk
sidesteps the problem by only requiring you to type tk. instead of Tkinter..
As to:
from Tkinter import *
it is generally a bad idea, for reasons discussed in Should wildcard import be avoided?