参考的书籍是基于PyQt4的,不过我所配置的环境是Python3.7+PyQt5的,有所差异,故做此记录:
PyQt5 的官方网站是:www.riverbankcomputing.co.uk
安装教程
1、首先安装完成python后配置好环境变量
2、win+r输入cmd打开命令符输入pip install wheel,将出现下图所示界面,表示安装wheel成功。(.whl就是需要wheel工具安装)
3、安装完wheel之后,进入PyQt5文件所在路径中,输入pip install PyQt5-5.8.2-5.8.0-cp35.cp36.cp37-none-win_amd64.whl
1、NameError: name ‘QApplication’ is not defined
from PyQt5.QtWidgets import QApplication #QApplication 已经定位到PyQt5.QtWidgets这个模块
2、 NameError: name ‘QLabel’ is not defined
from PyQt5.QtWidgets import *
3、 AttributeError: ‘Form’ object has no attribute ‘connect’
3.1常规:
self.connect(self.fromComboBox,SIGNAL("currentIndexChanged(int)"), self.updateUi)
self.connect(self.toComboBox, SIGNAL("currentIndexChanged(int)"), self.updateUi)
self.connect(self.fromSpinBox, SIGNAL("valueChanged(double)"), self.updateUi)
connect方法已经变了。要变成:
self.fromComboBox.currentIndexChanged.connect(self.updateUi)
self.toComboBox.currentIndexChanged.connect(self.updateUi)
self.fromSpinBox.valueChanged.connect(self.updateUi)
3.2 self.connect(dial,SIGNAL("valueChanged(init)"),spinbox.setValue)
应改为:dial.valueChanged.connect(spinbox.setValue)
4、另外,还要把带有unicode的函数去掉,因为python3.x默认就是unicode编
to = unicode(self.toComboBox.currentText())
改为:
to = self.toComboBox.currentText()#把unicode去掉
5、 except Exception as e: #在python3.x中,要将逗号改成as
except Exception,e:
改为:
except Exception as e:
6、 在Python3.x中,urllib2已变成了urllib,使用方法也大不相同。
import urllib2
更改:
import urllib.request #把urllib2改成urllib
7、 自定义的atzero”信号, 在PYQT5中,这种自定义的信号必须先用 pyqtSignal初始化
初始化
atzero = pyqtSignal(int)
原句: self.emit(SIGNAL("atzero"), self.zeros)
改为:self.atzero.emit(self.zeros) 与connect方法类似
核心机制是,信号与槽
函数:
QLineEdit()
QLabel()
QSpinBox()
setSuffix()
setValue()
QPushButton(,self)
button.clicked
setGeometry
setWindowTitle
布局:
QGridLayout()
grid.addWidget(button,5,0)