Python的GUI框架PySide的安装配置教程

时间:2021-09-07 12:54:15

(一)说在前面

    Python自带了GUI模块Tkinter,只是界面风格有些老旧。另外就是各种GUI框架了。

    之前安装过WxPython,并做了简单的界面。遂最近又重新搜索了一下网上关于Python GUI框架的问题,发现还是Qt呀。

    Python的Qt有PyQt和PySide吧。PyQt 是商业及 GPL 的版权, 而 PySide 是 LGPL。大意也就是PyQt开发商业软件是要购买授权的,而PySide则不需要。二者代码基本一致,修改下import 基本剩余的代码皆可通用。所以毫不犹豫的选择了PySide。

(二)PySide的安装

(1)比较快捷省事的安装

?
1
dizzy@dizzy-pc:~$ sudo add-apt-repository ppa:pyside

 

?
1
2
You are about to add the following PPA to your system:
 Official Ubuntu packages for PySide Qt Bindings.

 

?
1
dizzy@dizzy-pc:~$ sudo apt-get update
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# ... ...
 
dizzy@dizzy-pc:~$ sudo apt-get install python-pyside
Reading package lists... Done
Building dependency tree   
Reading state information... Done
The following extra packages will be installed:
 libphonon4 libpyside1.1 libqt4-help libqt4-scripttools libqt4-test
 libqtwebkit4 libshiboken1.1 phonon phonon-backend-gstreamer
 python-pyside.phonon python-pyside.qtcore python-pyside.qtdeclarative
 python-pyside.qtgui python-pyside.qthelp python-pyside.qtnetwork
 python-pyside.qtopengl python-pyside.qtscript python-pyside.qtsql
 python-pyside.qtsvg python-pyside.qttest python-pyside.qtuitools
 python-pyside.qtwebkit python-pyside.qtxml

   

(2)测试是否安装成功

?
1
dizzy@dizzy-pc:~$ python
?
1
2
3
Python 2.7.3 (default, Feb 27 2014, 20:00:17)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
?
1
2
>>> import PySide
>>> dir(PySide)

 

复制代码 代码如下:

 

['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '__version_info__']

 

 

?
1
>>> PySide.__version__
?
1
'1.1.2'
?
1
>>> PySide.QtCore.__version__
?
1
'4.8.1'

(三)PySide初试

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# -*- coding: utf-8 -*-
 
# Import PySide classes
import sys
from PySide.QtCore import *
from PySide.QtGui import *
 
 
# Create a Qt application
app = QApplication(sys.argv)
# Create a Label and show it
label = QLabel("Hello World")
label.show()
# Enter Qt application main loop
app.exec_()
sys.exit()

    这样运行就可以看到一个最简单的窗口了。Hello World .

(四)Hello,PySide
第一个使用PySide的示例,新建一个Hello.py文件,输入以下内容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Import PySide classes
import sys
from PySide.QtCore import *
from PySide.QtGui import *
 
 
# Create a Qt application
app = QApplication(sys.argv)
# Create a Label and show it
label = QLabel("Hello World")
label.show()
# Enter Qt application main loop
app.exec_()
sys.exit()

双击该文件,运行结果:

Python的GUI框架PySide的安装配置教程