零、介绍
PyQt是Python语言的GUI编程解决方案之一。可以用来代替Python内置的Tkinter。其它替代者还有PyGTK、wxPython等。与Qt一样,PyQt是一个自由软件。PyQt是PyKDE的基础。 PyQt的开发者是英国的“Riverbank Computing”公司。与4.5版本之前的。
PySide是跨平台的应用程序框架Qt的Python绑定版本。 在2009年8月,PySide首次发布。提供和PyQt类似的功能,并相容API。但与PyQt不同处为使用LGPL授权。
一、安装PySide2与PyQt5
PyQt5:
PyQt5不再提供常用Qt工具,比如图形界面开发工具Qt Designer、国际化翻译工具Liguist,必须自行安装Qt工具。
可使用如下命令安装Qt工具。
pip install PyQt5-tools
或
pip install PyQt5-tools -i https://pypi.douban.com/simple
PySide2:
pip install PySide2
二、使用测试
PyQt5:
安装完成后在Python安装目录下的Lib\site-packages目录中可以看到PyQt5、pyqt5-tools目录。
...: def magic(self):
...: self.text.setText(random.choice(self.hello))
...:
...:
...: if __name__ == "__main__":
...: app = QtWidgets.QApplication([])
...:
...: widget = MyWidget()
...: widget.resize(800, 600)
...: widget.show()
...:
...: sys.exit(app.exec_()
或者实现点击事件界面:
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MainWindow(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.layout = QHBoxLayout()
label = QLabel("Set Style:",self)
combo = QComboBox(self)
combo.addItems(QStyleFactory.keys())
# 选择当前窗口风格
index = combo.findText(QApplication.style().objectName(), Qt.MatchFixedString)
# 设置当前窗口风格
combo.setCurrentIndex(index)
combo.activated[str].connect(self.onCurrentIndexChanged)
self.layout.addWidget(label)
self.layout.addWidget(combo)
self.setLayout(self.layout)
self.setWindowTitle("Application Style")
self.resize(300, 100)
def onCurrentIndexChanged(self, style):
QApplication.setStyle(style)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
PySide2:
qt5_design.py:
# -*- coding: utf-8 -*-
from PySide2.QtCore import QMetaObject, QCoreApplication
from PySide2.QtWidgets import QWidget, QVBoxLayout, QListWidget, QPushButton
# 在Qt Designer中自动生成的代码,但需要部分修改
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(400, 300)
# 唤醒窗口
MainWindow.raise_()
# 必须要手动创建一个总部件,并把子部件加载于此,否则会出现以下报错:
# QLayout: Attempting to add QLayout "" to WoHeYunApp "MainWindow", which already has a layout
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.centralwidget.resize(400, 300)
self.verticalLayout_2 = QVBoxLayout(self.centralwidget)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.verticalLayout = QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.listWidget = QListWidget(self.centralwidget)
self.listWidget.setObjectName("listWidget")
self.verticalLayout.addWidget(self.listWidget)
self.pushButton = QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.verticalLayout.addWidget(self.pushButton)
self.verticalLayout_2.addLayout(self.verticalLayout)
self.retranslateUi(self.centralwidget)
QMetaObject.connectSlotsByName(self.centralwidget)
# 窗口自适应部件
MainWindow.setCentralWidget(self.centralwidget)
def retranslateUi(self, MainWindow):
_translate = QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Pick a folder"))
main.py:
# -*- coding: utf-8 -*-
import sys, os
from PySide2.QtWidgets import QMainWindow, QFileDialog, QApplication
import qt5_design
# 创建APP类,并继承UI的类
class WoHeYunApp(QMainWindow, qt5_design.Ui_MainWindow):
# 初始化构造函数
def __init__(self):
# 继承: super指父类(子类,实例).构造函数
super(WoHeYunApp, self).__init__()
self.setupUi(self)
# 把UI中的控件连接函数功能(事件)
self.pushButton.clicked.connect(self.browse_folder)
# 功能函数
def browse_folder(self):
# 清除列表内容
self.listWidget.clear()
# 创建文件夹选择对话框
directory = QFileDialog.getExistingDirectory(self, "Pick a folder")
# 如果有范围的选择路径则搜索该路径下所有存在的文件,并将其名字加载都列表中
if directory:
for file_name in os.listdir(directory):
self.listWidget.addItem(file_name)
# 创建实例
def main():
# 创建新的实例应用
app = QApplication(sys.argv)
# 我们将表单设置为WoHeYunApp
widgets = WoHeYunApp()
# 显示我们的表单
widgets.show()
# 退出程序
app.exec_()
# 如果我们直接运行文件而不是导入它,则执行
if __name__ == '__main__':
main()
执行结果:
三、PyQt与PySide的区别
两者都是QT与Python结合的桥梁。
协议不同
PyQt是GPLv3协议,大意是你的程序中用了它,你的程序就要开源,如果闭源商用就会违反协议(后果自负,脸皮够厚无所谓)。除非你搞封装动态加载那一套来强行规避。
PySide是LGPL协议,如果你只是作为库用用它,你的程序还是可以闭源商用。
所以很多人喜欢PySide。如果不做商业项目,强烈建议使用PyQt,资料多,稳定。需要开发闭源商用软件的就用PySide。
GPL(General Public License)和LGPL( Lesser General Public License)是GNU的两种License。越来越多的自由软件(Free Software)使用GPL作为其授权声明,如果对GPL一点都不了解,有可能在使用自由软件时违反了GPL的授权。如果是个人或不正规的公司倒也无所谓,但如果是有规模的公司,恐怕会有被起诉的风险。
LGPL是GPL的变种,也是GNU为了得到更多的甚至是商用软件开发商的支持而提出的。与 GPL的最大不同是,可以私有使用LGPL授权的自由软件,开发出来的新软件可以是私有的而不需要是自由软件。所以任何公司在使用自由软件之前应该保证在 LGPL或其它GPL变种的授权下。
对于PySide2,在2018.5QT公司退出Qt for Python,可以看作是重新命名和彻底改变的 PySide2,这是为 Python 语言提供 Qt 集成的模块。另一方面,pyside2 直接使用 pip install 安装该包,使用方法同 pyqt 类似,在导入包时直接将 import PyQt5.xxx --> import pyside2.xxx,当然有些不同,可以百度很容易替换,其中动态加载便不一样,pyside2包含r动态加载Designe设计的ui界面文件(xml语言描述文件),但是pyqt5没有包含该功能,需要加载导入pyuic这个包进行函数功能的调用。
PySide对Qt5提供支持的计划也从2014年开始筹备,也就是2015年上马的Qt for Python项目,该项目开发的模块命名为PySide2,以表示与老一代PySide的不同。所以其实PySide2只是PySide的升级版,PySide对标PyQt4,而PySide2对标PyQt5。
参考文献:
https://blog.csdn.net/tingguan/article/details/100892128
blog.csdn.net/The_Time_Runner
BlogCommendFromMachineLearn