我正在通过这门课程学习python
我使用的是Ubuntu 22.04和Python 3.10.7
我必须像这个一样使用pyautogui
import pyautogui
from time import sleep
sleep(1)
pyautogui.write( ' This is written by a computer')
在视频中,导师切换到文本编辑器,开始写作。当我运行此代码时,我的代码不会发生任何变化。
所以我去了pyautogui文档,尝试了alertbox代码示例,看看是否能让任何东西正常工作。
import pyautogui
pyautogui.alert(text='', title='', button='OK')
这给了我错误AssertionError: Tkinter is required for pymsgbox
类似地,在视频中,使用matplotlib可以使用以下代码绘制图形
import matplotlib, matplotlib.pyplot as plt
from time import sleep
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()
它告诉我Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure
。然后,当我用matplotlib.use('TkAgg')
配置GUI后端时,我陷入了一个循环,python3报告module not found: _tkinter
,同时Ubuntu报告python3-tk
是最新版本,所以没有什么可做的。
所以我试过
- 重新安装python3
- 安装python3-tk,然后重新安装python
- pip安装tk
- 设置venv并执行以上操作
- 谷歌——但我只得到上面的答案。这不起作用
所以基本上所有的东西都想要Tkinter,但tk在那里,python似乎忽略了它
感谢所有回复的人。事实证明,解决方案是从源代码重新编译Python,在configure
告诉它在哪里查找tkinter,如中所述