Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
>>>
on the other hand...
Python 3.2 (r32:88445, Mar 25 2011, 19:28:28)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Tkinter
I checked synaptic, Tkinter is installed. Then I found this--
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.
I am guessing that tkinter is still associated with the old python in my pc. How do I change that so python3 can use tkinter?
What worked for me in Ubuntu was actually just:
sudo apt-get install python3-tk
For python 3.6:
sudo apt-get install python3.6-tk
I didn't read anywhere, I simply tried it, as onteria_'s method didn't seem to work for me.
The answer to your question is that Tkinter
is renamed to tkinter
in python3.
That is with lowercase t.
In python 3 T
kinter renamed t
kinter
Use the following command:
sudo apt-get install python3-tk
The following commands do not work:
sudo apt-get install python3-tkinter
sudo apt-get install python3-Tkinter
pip3 install Tkinter
pip3 install tkinter
sudo apt-get install python3-tk
Since you mention synaptic I think you're on Ubuntu. You probably need to run update-python-modules to update your Tkinter module for Python 3.
EDIT: Running update-python-modules
First, make sure you have python-support
installed:
sudo apt-get install python-support
Then, run update-python-modules
with the -a
option to rebuild all the modules:
sudo update-python-modules -a
I cannot guarantee all your modules will build though, since there are some API changes between Python 2 and Python 3.
If you are using Ubuntu 18.04 along with Python 3.6, then pip or pip3 won't help. You need to install tkinter
using following command:
sudo apt-get install python3-tk
I had the same problem. I tried to use:
sudo apt-get install python3-tk
It gave an error stating blt(>=2.4z-7) is not present and is not installable.
I went here and manually installed it. (For Ubuntu 14.04)
Then I used apt again and it worked.
I concluded that python3.4 in Ubuntu didn't come with the .so file required to carry on installation. And blt was required to download it.
this works for me:
from tkinter import *
root = Tk()
l = Label(root, text="Does it work")
l.pack()
I found this looking for a fix for python 3.5.
In my case I was building python from source, here is what I did to help fix:
Add the tkinter headers with and rebuild python
sudo apt-get install tk8.6-dev
sudo make
Adding solution for CentOs 7 (python 3.6.x)
yum install python36-tkinter
I had tried about every version possible, hopefully this helps out others.
Adding the solution that I faced with python 3.4
on Fedora 21
. Hope this will help those facing a similar issue.
Any of these commands will install tkinter
:
sudo yum install python3-tkinter
OR
sudo dnf install python3-tkinter
requirement for tkinter:
python 3.6+
and go to shell write the test code like :
from tkinter import *
root = Tk()
root.mainloop()
For Ubuntu 20.04 this works for me.
sudo apt-get install python3.6-tk
精彩评论