Home > Net >  python3: python refuses to recognise tk install/ GUI modules don't work in Ubuntu 22.04
python3: python refuses to recognise tk install/ GUI modules don't work in Ubuntu 22.04

Time:10-11

I'm learning python via this course

I'm using Ubuntu 22.04 and Python 3.10.7

I have to use pyautogui like this

import pyautogui
from time import sleep
sleep(1)
pyautogui.write( ' This is written by a computer')

On the video, the tutor switches to the text editor and just starts writing. Nothing happens on mine when I run this code.

So I go to the pyautogui docs and try the alertbox code sample just to see if I can get anything working.

import pyautogui
pyautogui.alert(text='', title='', button='OK')

This gives me the error AssertionError: Tkinter is required for pymsgbox

Similarly, on the video, using matplotlib allows drawing a graph with this code

import matplotlib, matplotlib.pyplot as plt
from time import sleep
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()

It tells me Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure. Then when I configure a GUI backend with matplotlib.use('TkAgg') I fall into a loop where python3 reports module not found: _tkinter and simultaneously Ubuntu reports that python3-tk is the latest version so there's nothing to do.

So I've tried

  • reinstalling python3
  • installing python3-tk then reinstalling python
  • pip install tk
  • setting up venv and doing the above
  • Google - but I only get the above answers. Which don't work.

So basically everything wants Tkinter but tk is there and python seems to just ignore it. I don't get it

CodePudding user response:

As I understood your question, you are asking that tkinter module is not being found. Well you are installing the module as tk and not tkinter. tkinter is a separate module. Also in the code you are using pyautogui whereas for matplotlib you are providing tkinter. This would probably create an error. So try creating the GUI in tkinter and make sure you installed the right module (tkinter not tk)

CodePudding user response:

Thank you to everyone that replied. It turns out that the solution was to recompile Python from source, telling it at configure where to look for tkinter as described in these instructions

  • Related