基本原理就是作个子类继承生成的界面,在子类里写代码
工程结构
dialog_ui.py 界面子类
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QStandardItemModel, QStandardItem, QColor, QFont
from PyQt5.QtCore import Qt
import Ui_dialog
import sys
class myDialog(Ui_dialog.Ui_Dialog):#继承自UI_Diglog类,注意我把UI_Dialog放在了untitled.py中,如果你把这个类放在了XXX.py文件中,就应该是XXX.UI_Dialog
def __init__(self,Dialog):
super().setupUi(Dialog)#调用父类的setupUI函数
self.pushButton.clicked.connect(self.helloworld)#将按钮点击事件和helloworld函数绑定
model = QStandardItemModel(5, 7)
# 设置水平表头标签
model.setHorizontalHeaderLabels(['设置值', '板卡1', '板卡2', '板卡3', '板卡4', '板卡5', '板卡6'])
model.setVerticalHeaderLabels(['电压', '电流', '功率'])
# 填充表格数据
for row in range(5): # 遍历行
for column in range(7): # 遍历列
# 创建一个QStandardItem对象,表示单元格内容
item = QStandardItem('Item {row}, {column}')
# 将单元格内容添加到模型的指定位置
model.setItem(row, column, item)
self.tableView.setModel(model)
def helloworld(self):#label上显示文字hello world
self.label.setText("hello world")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = myDialog(MainWindow)#注意把类名修改为myDialog
#ui.setupUi(MainWindow) myDialog类的构造函数已经调用了这个函数,这行代码可以删去
MainWindow.show()
sys.exit(app.exec_())
界面及转换后的界面代码Ui_dialog.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(800, 600)
self.frame = QtWidgets.QFrame(Dialog)
self.frame.setGeometry(QtCore.QRect(10, 60, 781, 531))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.frame.sizePolicy().hasHeightForWidth())
self.frame.setSizePolicy(sizePolicy)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.tableView = QtWidgets.QTableView(self.frame)
self.tableView.setGeometry(QtCore.QRect(0, 0, 781, 531))
self.tableView.setObjectName("tableView")
self.layoutWidget = QtWidgets.QWidget(Dialog)
self.layoutWidget.setGeometry(QtCore.QRect(50, 20, 701, 26))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.pushButton = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton.setObjectName("pushButton")
self.horizontalLayout.addWidget(self.pushButton)
self.checkBox = QtWidgets.QCheckBox(self.layoutWidget)
self.checkBox.setObjectName("checkBox")
self.horizontalLayout.addWidget(self.checkBox, 0, QtCore.Qt.AlignHCenter)
self.pushButton_2 = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.horizontalLayout.addWidget(self.pushButton_2)
self.label = QtWidgets.QLabel(self.layoutWidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.pushButton_3 = QtWidgets.QPushButton(self.layoutWidget)
self.pushButton_3.setObjectName("pushButton_3")
self.horizontalLayout.addWidget(self.pushButton_3)
self.label_2 = QtWidgets.QLabel(self.layoutWidget)
self.label_2.setObjectName("label_2")
self.horizontalLayout.addWidget(self.label_2)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "功率分析仪调试工具"))
self.pushButton.setText(_translate("Dialog", "启动调试"))
self.checkBox.setText(_translate("Dialog", "滤波"))
self.pushButton_2.setText(_translate("Dialog", "输入源配置"))
self.label.setText(_translate("Dialog", "model: fluk1123 on line"))
self.pushButton_3.setText(_translate("Dialog", "调试板卡配置"))
self.label_2.setText(_translate("Dialog", "sn:12345678"))