pyside6常用组件的示例
一、制作界面
1.绘制界面
2.生成代码
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file 't1gui.ui'
##
## Created by: Qt User Interface Compiler version 6.5.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QCheckBox, QComboBox, QLabel,
QLineEdit, QPushButton, QRadioButton, QSizePolicy,
QWidget)
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(400, 300)
self.pushButton = QPushButton(Form)
self.pushButton.setObjectName(u"pushButton")
self.pushButton.setGeometry(QRect(50, 220, 75, 23))
self.lineEdit_name = QLineEdit(Form)
self.lineEdit_name.setObjectName(u"lineEdit_name")
self.lineEdit_name.setGeometry(QRect(90, 40, 113, 21))
self.label = QLabel(Form)
self.label.setObjectName(u"label")
self.label.setGeometry(QRect(20, 40, 53, 15))
self.label_2 = QLabel(Form)
self.label_2.setObjectName(u"label_2")
self.label_2.setGeometry(QRect(20, 70, 51, 16))
self.lineEdit_pass = QLineEdit(Form)
self.lineEdit_pass.setObjectName(u"lineEdit_pass")
self.lineEdit_pass.setGeometry(QRect(90, 70, 113, 21))
self.radioButton_yy = QRadioButton(Form)
self.radioButton_yy.setObjectName(u"radioButton_yy")
self.radioButton_yy.setGeometry(QRect(20, 110, 95, 19))
self.radioButton_zw = QRadioButton(Form)
self.radioButton_zw.setObjectName(u"radioButton_zw")
self.radioButton_zw.setGeometry(QRect(140, 110, 95, 19))
self.radioButton_ey = QRadioButton(Form)
self.radioButton_ey.setObjectName(u"radioButton_ey")
self.radioButton_ey.setGeometry(QRect(270, 110, 95, 19))
self.checkBox_c = QCheckBox(Form)
self.checkBox_c.setObjectName(u"checkBox_c")
self.checkBox_c.setGeometry(QRect(20, 170, 80, 19))
self.checkBox_py = QCheckBox(Form)
self.checkBox_py.setObjectName(u"checkBox_py")
self.checkBox_py.setGeometry(QRect(100, 170, 80, 19))
self.checkBox_go = QCheckBox(Form)
self.checkBox_go.setObjectName(u"checkBox_go")
self.checkBox_go.setGeometry(QRect(200, 170, 80, 19))
self.comboBox = QComboBox(Form)
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.setObjectName(u"comboBox")
self.comboBox.setGeometry(QRect(300, 200, 69, 22))
self.checkBox_all = QCheckBox(Form)
self.checkBox_all.setObjectName(u"checkBox_all")
self.checkBox_all.setGeometry(QRect(280, 170, 80, 19))
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
# setupUi
def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
self.pushButton.setText(QCoreApplication.translate("Form", u"\u8fd0\u884c\u7a0b\u5e8f", None))
self.label.setText(QCoreApplication.translate("Form", u"\u7528\u6237\u540d", None))
self.label_2.setText(QCoreApplication.translate("Form", u"\u5bc6\u7801", None))
self.radioButton_yy.setText(QCoreApplication.translate("Form", u"\u82f1\u8bed", None))
self.radioButton_zw.setText(QCoreApplication.translate("Form", u"\u4e2d\u6587", None))
self.radioButton_ey.setText(QCoreApplication.translate("Form", u"\u4fc4\u8bed", None))
self.checkBox_c.setText(QCoreApplication.translate("Form", u"c\u8bed\u8a00", None))
self.checkBox_py.setText(QCoreApplication.translate("Form", u"python\u8bed\u8a00", None))
self.checkBox_go.setText(QCoreApplication.translate("Form", u"go\u8bed\u8a00", None))
self.comboBox.setItemText(0, QCoreApplication.translate("Form", u"\u897f\u5b89", None))
self.comboBox.setItemText(1, QCoreApplication.translate("Form", u"gl", None))
self.comboBox.setItemText(2, QCoreApplication.translate("Form", u"pycckuv", None))
self.comboBox.setItemText(3, QCoreApplication.translate("Form", u"\u5317\u4eac", None))
self.comboBox.setItemText(4, QCoreApplication.translate("Form", u"\u4e0a\u6d77", None))
self.checkBox_all.setText(QCoreApplication.translate("Form", u"\u6240\u6709", None))
# retranslateUi
二、核心代码
2.1核心代码
# 导入 t1gui_ui _ui
from t1gui_ui import Ui_Form
from PySide6.QtWidgets import QApplication, QMainWindow
import sys
# 继承 Ui_MainWindow类
class MyMainWindow(QMainWindow,Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.bind()
def bind(self):
# comboBox的内容
self.pushButton.clicked.connect(self.showDialog)
self.comboBox.currentTextChanged.connect(self.showCom)
# checkbox
self.checkBox_c.stateChanged.connect(self.showCheckBox)
self.checkBox_py.stateChanged.connect(self.showCheckBox)
self.checkBox_go.stateChanged.connect(self.showCheckBox)
self.checkBox_all.stateChanged.connect(self.showCheckBox)
# radio,是否被选中状态
self.radioButton_yy.toggled.connect(self.showRadio)
def showRadio(self):
print(self.radioButton_yy.isChecked())
# checkbox函数
def showCheckBox(self):
if self.checkBox_c.isChecked():
print("checkbox_c is checked")
# else:
# print("checkbox is unchecked")
elif self.checkBox_py.isChecked():
print("checkbox_py is checked")
elif self.checkBox_go.isChecked():
print("checkbox_go is checked")
elif self.checkBox_all.isChecked():
print("checkbox_all is checked")
def showDialog(self):
# label lineedit的内容设置
self.lineEdit_name.setText("hello,world!")
self.label.setText(self.lineEdit_name.text())
def showCom(self):
# 打印你点的comboBox的文本内容
print(self.comboBox.currentText())
# 打印你点的comboBox的索引数字
print(self.comboBox.currentIndex())
if __name__ == "__main__":
app = QApplication(sys.argv)
myWin = MyMainWindow()
myWin.show()
sys.exit(app.exec_())
2.2事件触发调用过程--事件触发
2.3各个触发;“槽函数”
radio的触发函数
checkbox的函数
按钮+文本框label的触发时间
combox的触发函数
三、展示