Xubuntu [SOLVED]: tkinter import error on ubuntu 16.4

Xubuntu [SOLVED]: tkinter import error on ubuntu 16.4

Home Forums Xubuntu Xubuntu [SOLVED]: tkinter import error on ubuntu 16.4

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36865

    Anonymous

    QuestionQuestion

    I tried to run a code which use turtle to draw a tree. There is no problem with code because I ran it on Windows 10 successfully, but on Ubuntu, the following error was encountered:

    mosharraf@mmh:~$ cd Desktop
    mosharraf@mmh:~/Desktop$ python3 Tree.py
    Traceback (most recent call last):
      File "Tree.py", line 6, in <module>
        import turtle 
      File "/usr/local/lib/python3.6/turtle.py", line 107, in <module>
        import tkinter as TK
      File "/usr/local/lib/python3.6/tkinter/__init__.py", line 36, in <module>
        import _tkinter # If this fails your Python may not be configured for Tk
    ModuleNotFoundError: No module named '_tkinter'
    mosharraf@mmh:~/Desktop$
    

    Then I tried to install tkinter but I failed.

    mosharraf@mmh:~$ sudo apt-get install python3-tk
    [sudo] password for mosharraf: 
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    python3-tk is already the newest version (3.5.1-1).
    0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
    mosharraf@mmh:~$
    

    Afterwards, I checked for the tkinter in the directories and found it.

    mosharraf@mmh:/usr/local/lib/python3.6$ cd tkinter
    mosharraf@mmh:/usr/local/lib/python3.6/tkinter$ ls
    colorchooser.py  constants.py  dnd.py         font.py      __main__.py    __pycache__      simpledialog.py  tix.py
    commondialog.py  dialog.py     filedialog.py  __init__.py  messagebox.py  scrolledtext.py  test             ttk.py
    mosharraf@mmh:/usr/local/lib/python3.6/tkinter$ python3
    Python 3.6.4 (default, Dec 22 2017, 18:44:45) 
    [GCC 5.4.0 20160609] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import tkinter
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python3.6/tkinter/__init__.py", line 36, in <module>
        import _tkinter # If this fails your Python may not be configured for Tk
    ModuleNotFoundError: No module named '_tkinter'
    >>> 
    

    tkinter is in the directories as you can see above, but I can’t import it. I tried several ways like

    import tkinter
    
    import tkinter as tk
    
    from tkinter import *
    

    etc.

    Note that I ran the code in python 2.7 and it ran successfully. I also ran the code on Windows 10, Python 3.6. But in Ubuntu, it’s causing the problem.

    #36866

    Anonymous

    Accepted AnswerAnswer

    At the bottom of your post you say:

    Note that I ran the code in python 2.7 and it ran successfully.

    When you import Tkinter from Python 2 you must say:

    import Tkinter

    When you import Tkinter from Python 3 you must say:

    import tkinter

    A snippet I use for Tkinter when I know that my script is going to be ran from multiple versions of Python is below:

    import sys
    
    py_version = int(sys.version[0])
    
    # If the python version is lower than 3, use Python 2 import
    if py_version < 3:
        import Tkinter
    
    # Otherwise use Python 3 import 
    else:
        import tkinter
    

    IF THE ABOVE DID NOT FIX YOUR PROBLEM:

    I see that in one of your error messages it says

    python3-tk is already the newest version (3.5.1-1).

    But the error message also says that your Python version is 3.6.

    Try using: sudo apt-get install python3.6-tk

    Source: https://stackoverflow.com/questions/48012086/tkinter-import-error-on-ubuntu-16-4
    Author: Jebby
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.