skip to Main Content

I’m a fairly new Linux user running Ubuntu (Mint), and I’ve been trying to learn to build a GUI app using tkinter.

My understanding is this comes built-in with Python, and theoretically shouldn’t need a separate installation. Unfortunately for me this does not appear to be the case. All installation paths are default, so to my knowledge everything related to Python3.12 is under my /usr directory to avoid problems with the Python install that comes with Linux.

I followed just about every guide I could find on stackoverflow to try to fix this situation, but I’m at a loss. I’ve also tried reinstalling Python3.12 with APT, and also followed this guide: https://linuxcapable.com/install-python-3-12-on-ubuntu-linux/ and ran:

sudo apt install python3.12-full

In the hopes that it would pick up anything the default install didn’t get.

My Python version appears correct:

<user>@<user>:~$ python3 --version
Python 3.12.4

<user>@<user>:~$ which python3
/usr/local/bin/python3

I did install tk via APT, and reattempting produces:

<user>@<user>:~$ sudo apt install tk
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
tk is already the newest version (8.6.11+1build2).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Using a Python shell to check for Tk does not work:

<user>@<user>:~$ python3.12
Python 3.12.4 (main, Jul 18 2024, 11:12:47) [GCC 11.4.0] 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.12/tkinter/__init__.py", line 38, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
    ^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named '_tkinter'

Nor does attempting to run the basic tutorial script from Tk inside Pycharm. I have a virtual environment setup via pipenv with the following pipfile:

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
tk = "*"

[dev-packages]

[requires]
python_version = "3.12"
python_full_version = "3.12.4"

Python script:

from tkinter import *
from tkinter import ttk
root = Tk()
frm = ttk.Frame(root, padding=10)
frm.grid()
ttk.Label(frm, text="Hello World!").grid(column=0, row=0)
ttk.Button(frm, text="Quit", command=root.destroy).grid(column=1, row=0)
root.mainloop()

Produces:

Traceback (most recent call last):
  File "/home/<user>/PycharmProjects/gui_app/main.py", line 1, in <module>
    from tkinter import *
  File "/usr/local/lib/python3.12/tkinter/__init__.py", line 38, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
    ^^^^^^^^^^^^^^^
ModuleNotFoundError: No module named '_tkinter'

I’m not even sure where to look next, as every article I’ve come across suggests installing Tk via APT, which as demonstrated has already been done. The error message suggests a configuration issue, but I’m not skilled enough to understand the documentation for this topic.

Any help is appreciated.

2

Answers


  1. You may have to install tkinter on your linux device manually. Here are all the cases for installing tkinter:

    For Debian-based Linux:

    sudo apt-get install python-tk

    For Arch-based Linux:

    sudo pacman -S tk

    For Fedora-based Linux:

    sudo dnf install python3-tkinter

    For RHEL, CentOS, Oracle Linux:

    sudo yum install -y tkinter tk-devel

    Enter all these commands in the terminal to install tkinter

    (ModuleNotFoundError: No module named ‘_tkinter’ means that you have to either manually download the module from the terminal or check the folder python is installed.)

    Login or Signup to reply.
  2. There are few things to mention:-

    1. Tkinter library usually don’t work on a tiling window managers
      like(Qtile), or even some time breaks on wayland too, so it is
      suggested to use x11 while using tk library
    2. As you stated:-

    I have a virtual environment setup

    Hence, after activating environment with source you must try to locally install tk with with pip.

    1. According to your referenced link you are manually adding a PPA to the package manager for installing python 3.12 version. Hence, if you’ve installed Python from a custom source (e.g., a PPA or a manual installation), you might need to add the custom Python path to your system’s PATH environment variable.

    2. And as my friend @acw1668 suggested, you can use sudo apt install python3.12-tk to install the package globally on the system too.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search