在看到图形界面编程的时候,需要导入Tkinter模块,从而在解释器中进行import Tkinter,然后。。。报错如下:

[python] view plain copy import tkinter的时候报错import tkinter的时候报错
  1. >>> from tkinter import *  
  2. Traceback (most recent call last):  
  3. File "<stdin>", line 1, in <module>  
  4. File "/usr/local/lib/python3.4/tkinter/__init__.py", line 39, in <module>  
  5. import _tkinter # If this fails your Python may not be configured for Tk  
  6. ImportError: No module named _tkinter  


从报错信息来看,就是导入的时候出错,没有找到tkinter模块,主要是由于在进行编译python的时候,没有对Tk进行做相应的配置。


解决方案如下:

查询操作系统中安装的tk包:

[plain] view plain copy import tkinter的时候报错import tkinter的时候报错
  1. rpm -qa|grep ^tk  #表示查询以tk开头的安装包  

在结果中,只有一个tk包安装了,从而缺少了一个tk-devel的安装包没进行安装。


从而,需要安装tk-devel包,如下所示:(也可以使用其他的方法来进行安装,例如rpm安装,但是yum是最方便的)

[plain] view plain copy import tkinter的时候报错import tkinter的时候报错
  1. yum -y install tk-devel #安装tk-devel安装包  


安装好之后,必须进行重新编译python

[plain] view plain copy import tkinter的时候报错import tkinter的时候报错
  1. 1 ./configure --prefix=/usr/local/python     #后面的地址可以有更改
  2. 2 make && make install  


测试:直接导入模块,不出现任何错误即可。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-04
  • 2022-02-20
  • 2021-05-11
  • 2021-06-09
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-20
  • 2021-10-19
  • 2022-12-23
  • 2022-03-02
  • 2021-09-10
  • 2022-12-23
相关资源
相似解决方案