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 --versionPython 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 tkReading package lists... DoneBuilding dependency tree... DoneReading state information... Donetk 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.12Python 3.12.4 (main, Jul 18 2024, 11:12:47) [GCC 11.4.0] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import tkinterTraceback (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 = truename = "pypi"[packages]tk = "*"[dev-packages][requires]python_version = "3.12"python_full_version = "3.12.4"
Python script:
from tkinter import *from tkinter import ttkroot = 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.