除了内建的模块外,Python还有大量的第三方模块。
基本上,所有的第三方模块都会在https://pypi.python.org/pypi上注册,只要找到对应的模块名字,即可用pip安装。
本章介绍常用的第三方模块。
1. PIL
PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了。PIL功能非常强大,但API却非常简单易用。
由于PIL仅支持到Python 2.7,加上年久失修,于是一群志愿者在PIL的基础上创建了兼容的版本,名字叫Pillow,https://github.com/python-pillow/Pillow,支持最新Python 3.x,又加入了许多新特性,因此,我们可以直接安装使用Pillow
使用 pip3 install pillow直接安装即可。
要详细了解PIL的强大功能,请请参考Pillow官方文档:
小结
PIL提供了操作图像的强大功能,可以通过简单的代码完成复杂的图像处理。
参考源码
2. Virtualenv
合理使用Virtualenv和linux下不同的python使用不同的名称(python2,python2.7,python3等)即可在一个主机上保留多套开发环境。
3. 图形界面
Python支持多种图形界面的第三方库,包括:Tk,wxWidgets,Qt,GTK等等。python自带的库是支持T看的Tkinter,无需安装任何包即可使用,即可进行GUI编程。
Tkinter
我们来梳理一下概念:
我们编写的Python代码会调用内置的Tkinter,Tkinter封装了访问Tk的接口;
Tk是一个图形库,支持多个操作系统,使用Tcl语言开发;
Tk会调用操作系统提供的本地GUI接口,完成最终的GUI。
所以,我们的代码只需要调用Tkinter提供的接口就可以了
#!/usr/env/bin python3#-*- coding: utf-8 -*-#filename: gui_test.py#function:
from tkinter import *
classApplication(Frame):def __init__(selfself, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()defcreateWidgets(self):
self.helloLabel= Label(self, text=‘Hello,world!‘)
self.helloLabel.pack()
self.quitButton= Button(self, text=‘Quit‘, command=self.quit)
self.quitButton.pack()
app=Application()#config window‘s title
app.master.title(‘Hello World‘)#the master circle
app.mainloop()
# 报错了,错误缺少个_tkinter,应该是OS中没有安装什么组件,yum search tk搜出来好多,不知道该安装哪个,网上搜了下都说tk不好用,所以就先跳过了。
/usr/local/Python-3.5.2/bin/python3.5 /opt/workspace/python3/gui_test.py
Traceback (most recent call last):
File "/opt/workspace/python3/gui_test.py", line 6, in
from tkinter import *
File "/usr/local/Python-3.5.2/lib/python3.5/tkinter/__init__.py", line 35, in
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named ‘_tkinter‘
Process finished with exit code 1
mark