.
QApplication是整个应用,QWidget是不同窗口,他们在PyQt5.QtWidgets下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import sys from PyQt5.QtWidgets import QApplication,QWidget
if __name__=='__main__': app=QApplication(sys.argv) w=QWidget() w.resize(300,150) w.move(300,300) w.setWindowTitle('桌面应用') w.show() sys.exit(app.exec_())
|
将ui文件转换为py文件
1
| pyuic5 -o demo.py demo.ui
|
通过ui生成的py文件生成窗口
1 2 3 4 5 6 7 8 9 10
| import sys import Ui_demo2 from PyQt5.QtWidgets import QApplication,QMainWindow if __name__=="__main__": app=QApplication(sys.argv) mainWindow=QMainWindow() ui=Ui_demo2.Ui_MainWindow() ui.setupUi(mainWindow) mainWindow.show() sys.exit(app.exec_())
|
热键
在每个标签后使用格式如(&A)
类的字符
主窗口类型
QMainWindow:包含菜单栏、工具栏、状态栏和标题栏
QWidget:是对话窗口的基类。无菜单栏、工具栏、状态栏
QDialog:不确定具体用途
获取窗口宽度和高度以及位置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import sys from PyQt5.QtWidgets import QHBoxLayout,QMainWindow,QApplication,QPushButton,QWidget from matplotlib.widgets import Widget def onClick_button(): print("Onclick") print(widget.x()) print(widget.y()) print(widget.width()) print(widget.height()) print(widget.geometry().x()) print(widget.geometry().y()) print(widget.geometry().width()) print(widget.geometry().height()) app=QApplication(sys.argv) widget=QWidget() btn=QPushButton(widget) btn.setText("按钮") btn.clicked.connect(onClick_button) btn.move(24,52) widget.resize(300,240) widget.move(250,200) widget.show() sys.exit(app.exec_())
|
LineEdit校验器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| from PyQt5.QtWidgets import * from PyQt5.QtGui import QIntValidator,QDoubleValidator,QRegExpValidator from PyQt5.QtCore import QRegExp import sys
class QLineEditValidator(QWidget): def __init__(self): super().__init__() self.initUI()
def initUI(self): formLayout=QFormLayout() self.setWindowTitle("校验器") intLineEdit=QLineEdit() doubleLineEdit=QLineEdit() validatorLineEdit=QLineEdit() formLayout.addRow('整数类型',intLineEdit) formLayout.addRow('浮点类型',doubleLineEdit) formLayout.addRow('正则表达式',validatorLineEdit)
intLineEdit.setPlaceholderText("数字") doubleLineEdit.setPlaceholderText("浮点") validatorLineEdit.setPlaceholderText("字母数字")
intvalidator=QIntValidator(self) intvalidator.setRange(1,99)
doublevalidator=QDoubleValidator(self) doublevalidator.setRange(-360,360) doublevalidator.setNotation(QDoubleValidator.StandardNotation) doublevalidator.setDecimals(2)
reg=QRegExp('[a-zA-Z0-9]+$') validator=QRegExpValidator(self) validator.setRegExp(reg)
intLineEdit.setValidator(intvalidator) doubleLineEdit.setValidator(doublevalidator) validatorLineEdit.setValidator(validator)
self.setLayout(formLayout)
if __name__ == "__main__": app = QApplication(sys.argv) main=QLineEditValidator() main.show() sys.exit(app.exec_())
|
使用掩码限制输入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| from PyQt5.QtWidgets import * import sys class QLineEditMask(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): formLayout=QFormLayout() self.setWindowTitle("掩码限制输入")
ipLineEdit=QLineEdit() macLineEdit=QLineEdit() dateLineEdit=QLineEdit() licenceLineEdit=QLineEdit()
ipLineEdit.setInputMask('000.000.000.000;_') macLineEdit.setInputMask('HH:HH:HH:HH:HH:HH;_') dateLineEdit.setInputMask('0000-00-00 00:00:00') licenceLineEdit.setInputMask('>AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#')
formLayout.addRow('数字掩码',ipLineEdit) formLayout.addRow('Mac掩码',macLineEdit) formLayout.addRow('日期掩码',dateLineEdit) formLayout.addRow('许可证掩码',licenceLineEdit)
self.setLayout(formLayout)
if __name__ == "__main__": app= QApplication(sys.argv) main=QLineEditMask() main.show() sys.exit(app.exec_())
|
下拉选择控件(大字体)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import *
class ComBoxDemo(QWidget): def __init__(self): super().__init__() self.initUI()
def initUI(self): layout=QVBoxLayout() self.label=QLabel("请选择编程语言") self.label.setFont(QFont('黑体', 100)) self.label.setAlignment(Qt.AlignCenter) self.combox=QComboBox() self.combox.setFont(QFont('黑体',50)) self.combox.addItem("C++") self.combox.addItem("Python") self.combox.addItems(["Java","C#","JavaScript"]) self.combox.currentIndexChanged.connect(self.selectChange) layout.addWidget(self.label) layout.addWidget(self.combox) self.setLayout(layout) def selectChange(self,i): self.label.setText(self.combox.currentText()) self.label.adjustSize() for count in range(self.combox.count()): print('items'+str(count)+'='+self.combox.itemText(count)) print('current index',i) if __name__ == "__main__": app = QApplication(sys.argv) main = ComBoxDemo() main.show() sys.exit(app.exec_())
|
滑动条控件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import *
class SliderDemo(QDialog): def __init__(self): super().__init__() self.initUI()
def initUI(self): layout=QVBoxLayout() self.label = QLabel('你好') self.label.setAlignment(Qt.AlignCenter) self.slider=QSlider(Qt.Horizontal) self.slider.setMinimum(12) self.slider.setMaximum(400) self.slider.setTickPosition(QSlider.TicksBelow) self.slider.setTickInterval(6) layout.addWidget(self.label) layout.addWidget(self.slider) self.slider.valueChanged.connect(self.getvalue) self.setLayout(layout) def getvalue(self): self.label.setFont(QFont('黑体', self.slider.value()))
if __name__ == "__main__": app = QApplication(sys.argv) main = SliderDemo() main.show() sys.exit(app.exec_())
|
自定义信号槽
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| from signal import signal from PyQt5.QtCore import * class MyTypeSignal(QObject): signal=pyqtSignal(str) signal2=pyqtSignal([int,str],[str]) def run(self): self.signal.emit("test") def run1(self): self.signal2.emit(1,'test') class MySlot(QObject): def get(self,msg): print(msg) def get1(self,msg,msg2): print(msg) print(msg2) if __name__=="__main__": signal=MyTypeSignal() slot=MySlot() signal.signal.connect(slot.get) signal.signal2.connect(slot.get1) signal.run() signal.run1()
|