PyQt5
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
PySide2
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2.QtGui import *
顶部应用
QApplication
app = QApplication([])
app.setStyle(QStyleFactory.create('Fusion'))
app.exec_()
部件
wg = QWidget()
窗口标题
wg.setWindowTitle('YEUNGCHIE')
窗口尺寸
wg.resize(300, 200)
wg.size()
wg.setMinimumSize(300, 200)
wg.setMaximumSize(300, 200)
wg.minimumSize()
wg.maximumSize()
wg.setFixedSize(300, 200)
wg.setFixedWidth(300)
wg.setFixedHeight(200)
wg.setMinimumWidth(300)
wg.setMaximumWidth(300)
wg.minimumWidth()
wg.maximumWidth()
wg.setMinimumHeight(200)
wg.setMaximumHeight(200)
wg.minimumHeight()
wg.maximumHeight()
窗口位置
wg.move(300, 200)
窗口置顶
wg.setWindowFlags(Qt.WindowStaysOnTopHint)
设置布局
wg.setLayout(self.gridLayout)
QLabel
label = QLabel()
文本
label.setText('LABEL')
label.text()
label.setIndent()
label.setToolTip( 'Tips' )
label.setAlignment( Qt.AlignCenter | Qt.AlignRight )
事件
label.linkClicked.connect(function)
label.setText('<a href="https://baidu.com">Baidu</a>')
label.linkHovered.connect(function)
QCheckBox
checkBox = QCheckBox()
文本
checkBox.setText( 'Enable' )
checkBox.text()
点击
checkBox.setChecked(True)
checkBox.setChecked(False)
checkBox.isChecked()
QLineEdit
lineEdit = QLineEdit()
文本
lineEdit.setText( 'working dir' )
lineEdit.text()
提示词
lineEdit.setPlaceholderText( '输入框内提示词' )
lineEdit.setToolTip( '鼠标悬停提示词' )
QComboBox
comboBox = QComboBox()
添加选项
comboBox.addItem('virtuoso')
comboBox.addItem('skipper')
comboBox.addItems('R0 R90 MX MY MYR90'.split(' '))
comboBox.insertItem(5, 'MXR90')
comboBox.insertItems(2, ['R180', 'R270'])
移除选项
comboBox.removeItem(0)
选择选项
comboBox.setCurrentText( 'virtuoso' )
comboBox.currentText()
comboBox.setCurrentIndex()
comboBox.currentIndex()
选项编辑
comboBox.setItemText(0, 'Calibre')
pushButton = QPushButton()
文本
pushButton.setText( 'OK' )
pushButton.text()
事件
pushButton.linkClicked.connect(function)
布局
formLayout = QFormLayout()
标签对齐
formLayout.setLabelAlignment( Qt.AlignCenter | Qt.AlignRight )
增加 row
formLayout.addRow("标签1 Label", label)
formLayout.addRow("标签2 CheckBox", checkBox)
QGridLayout
gridLayout = QGridLayout()
添加组件
gridLayout.addWidget(label, 0, 0)
gridLayout.addWidget(checkBox , 0, 1)
gridLayout.addWidget(lineEdit , 0, 2, 3, 1)
FAQ
启动失败可以打开 debug 模式
export QT_DEBUG_PLUGINS=1
缺少 qt5dxcb 安装即可
yum -y install qt5dxcb-plugin.x86_64
本文作者:YEUNGCHIE
本文链接:https://www.cnblogs.com/yeungchie/p/16219094.html