可以学习到:qt designer 修改字体,部件拖入layout,引用time模块延时,正则表达式判断输入,进制转换,部件固定大小,graphics view显示图片,消息提示框使用
1,qt designer 修改字体,比较简单,选中部件,右侧找到如下:
2,qt designer 部件拖入layout:拖动部件,直接移动到右侧的如图所示的layout里面即可:
3,引用time模块延时:代码如下
import time time.sleep(1)
4,正则表达式判断输入以及进制转换:字符转换为进制格式使用bin(),hex()等函数,转换为整型使用int()函数,return表示结束函数
import re pattern = '0[x|X][0|1][0-9a-fA-F]'#判断模板:0x** 十六进制格式 ADDR = self.my_ui.lineEdit.text() if len(ADDR) == 4: flag = re.match(pattern, ADDR) # 如何判断输入的字符是否符合相应的要求呢?使用正则表达式,即使用re模块。 if flag: ADDR_Bin = bin(int(ADDR, 16))[2:] # 转换为二进制,并且去掉“0b”,也就是前两位字符不要 ADDR_Bin = ADDR_Bin.rjust(5, '0') # 原字符串右侧对齐, 左侧补零,补齐后总位宽为8 else: QMessageBox.warning(my_MainWindow, "警告", "输入ADDR不对哟", QMessageBox.Yes) return else: QMessageBox.warning(my_MainWindow, "警告", "输入ADDR只能是4个字符呐", QMessageBox.Yes) return
5,部件固定大小:
self.my_ui.textBrowser.setFixedHeight(300) self.my_ui.textBrowser_2.setFixedHeight(300) self.my_ui.textBrowser_3.setFixedHeight(300) self.my_ui.textBrowser.setFixedWidth(300) self.my_ui.textBrowser_2.setFixedWidth(200) self.my_ui.textBrowser_3.setFixedWidth(200)
6,graphics view显示图片:这里加入视图部件后,信号与槽机制要使用lambda:将窗口作为参数传递出去才有效
self.pix = QtGui.QPixmap('2.png') self.scene = QtWidgets.QGraphicsScene()#创建场景 self.item = QGraphicsPixmapItem(self.pix) # 创建像素图元 self.item.setScale(1.49)#放大1.49倍 self.scene.addItem(self.item)#将图元添加到场景中 self.my_ui.graphicsView.setScene(self.scene)#将场景加入到视图中显示出来
完整代码:
1,gui转换代码:
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'GUI.ui' # # Created by: PyQt5 UI code generator 5.14.2 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(1172, 871) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth()) MainWindow.setSizePolicy(sizePolicy) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget) self.verticalLayout.setObjectName("verticalLayout") spacerItem = QtWidgets.QSpacerItem(20, 20, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed) self.verticalLayout.addItem(spacerItem) self.horizontalLayout_2 = QtWidgets.QHBoxLayout() self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.verticalLayout_18 = QtWidgets.QVBoxLayout() self.verticalLayout_18.setObjectName("verticalLayout_18") self.label_25 = QtWidgets.QLabel(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_25.sizePolicy().hasHeightForWidth()) self.label_25.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.label_25.setFont(font) self.label_25.setAlignment(QtCore.Qt.AlignCenter) self.label_25.setObjectName("label_25") self.verticalLayout_18.addWidget(self.label_25) self.comboBox = QtWidgets.QComboBox(self.centralwidget) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.comboBox.setFont(font) self.comboBox.setObjectName("comboBox") self.verticalLayout_18.addWidget(self.comboBox) self.horizontalLayout_2.addLayout(self.verticalLayout_18) self.verticalLayout_17 = QtWidgets.QVBoxLayout() self.verticalLayout_17.setObjectName("verticalLayout_17") self.label_23 = QtWidgets.QLabel(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_23.sizePolicy().hasHeightForWidth()) self.label_23.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.label_23.setFont(font) self.label_23.setAlignment(QtCore.Qt.AlignCenter) self.label_23.setObjectName("label_23") self.verticalLayout_17.addWidget(self.label_23) self.spinBox_3 = QtWidgets.QSpinBox(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.spinBox_3.sizePolicy().hasHeightForWidth()) self.spinBox_3.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.spinBox_3.setFont(font) self.spinBox_3.setMaximum(1) self.spinBox_3.setObjectName("spinBox_3") self.verticalLayout_17.addWidget(self.spinBox_3) self.horizontalLayout_2.addLayout(self.verticalLayout_17) self.verticalLayout_16 = QtWidgets.QVBoxLayout() self.verticalLayout_16.setObjectName("verticalLayout_16") self.label_22 = QtWidgets.QLabel(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_22.sizePolicy().hasHeightForWidth()) self.label_22.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.label_22.setFont(font) self.label_22.setAlignment(QtCore.Qt.AlignCenter) self.label_22.setObjectName("label_22") self.verticalLayout_16.addWidget(self.label_22) self.spinBox_4 = QtWidgets.QSpinBox(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.spinBox_4.sizePolicy().hasHeightForWidth()) self.spinBox_4.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.spinBox_4.setFont(font) self.spinBox_4.setMaximum(1) self.spinBox_4.setObjectName("spinBox_4") self.verticalLayout_16.addWidget(self.spinBox_4) self.horizontalLayout_2.addLayout(self.verticalLayout_16) self.verticalLayout_15 = QtWidgets.QVBoxLayout() self.verticalLayout_15.setObjectName("verticalLayout_15") self.label_21 = QtWidgets.QLabel(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_21.sizePolicy().hasHeightForWidth()) self.label_21.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.label_21.setFont(font) self.label_21.setAlignment(QtCore.Qt.AlignCenter) self.label_21.setObjectName("label_21") self.verticalLayout_15.addWidget(self.label_21) self.spinBox_5 = QtWidgets.QSpinBox(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.spinBox_5.sizePolicy().hasHeightForWidth()) self.spinBox_5.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.spinBox_5.setFont(font) self.spinBox_5.setMaximum(1) self.spinBox_5.setObjectName("spinBox_5") self.verticalLayout_15.addWidget(self.spinBox_5) self.horizontalLayout_2.addLayout(self.verticalLayout_15) self.verticalLayout_14 = QtWidgets.QVBoxLayout() self.verticalLayout_14.setObjectName("verticalLayout_14") self.label_20 = QtWidgets.QLabel(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_20.sizePolicy().hasHeightForWidth()) self.label_20.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.label_20.setFont(font) self.label_20.setAlignment(QtCore.Qt.AlignCenter) self.label_20.setObjectName("label_20") self.verticalLayout_14.addWidget(self.label_20) self.spinBox_6 = QtWidgets.QSpinBox(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.spinBox_6.sizePolicy().hasHeightForWidth()) self.spinBox_6.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.spinBox_6.setFont(font) self.spinBox_6.setMaximum(1) self.spinBox_6.setObjectName("spinBox_6") self.verticalLayout_14.addWidget(self.spinBox_6) self.horizontalLayout_2.addLayout(self.verticalLayout_14) self.verticalLayout_13 = QtWidgets.QVBoxLayout() self.verticalLayout_13.setObjectName("verticalLayout_13") self.label_19 = QtWidgets.QLabel(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_19.sizePolicy().hasHeightForWidth()) self.label_19.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.label_19.setFont(font) self.label_19.setAlignment(QtCore.Qt.AlignCenter) self.label_19.setObjectName("label_19") self.verticalLayout_13.addWidget(self.label_19) self.spinBox_7 = QtWidgets.QSpinBox(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.spinBox_7.sizePolicy().hasHeightForWidth()) self.spinBox_7.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.spinBox_7.setFont(font) self.spinBox_7.setMaximum(1) self.spinBox_7.setObjectName("spinBox_7") self.verticalLayout_13.addWidget(self.spinBox_7) self.horizontalLayout_2.addLayout(self.verticalLayout_13) self.verticalLayout_12 = QtWidgets.QVBoxLayout() self.verticalLayout_12.setObjectName("verticalLayout_12") self.label_18 = QtWidgets.QLabel(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_18.sizePolicy().hasHeightForWidth()) self.label_18.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.label_18.setFont(font) self.label_18.setAlignment(QtCore.Qt.AlignCenter) self.label_18.setObjectName("label_18") self.verticalLayout_12.addWidget(self.label_18) self.spinBox_8 = QtWidgets.QSpinBox(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.spinBox_8.sizePolicy().hasHeightForWidth()) self.spinBox_8.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.spinBox_8.setFont(font) self.spinBox_8.setMaximum(1) self.spinBox_8.setObjectName("spinBox_8") self.verticalLayout_12.addWidget(self.spinBox_8) self.horizontalLayout_2.addLayout(self.verticalLayout_12) self.verticalLayout_11 = QtWidgets.QVBoxLayout() self.verticalLayout_11.setObjectName("verticalLayout_11") self.label_17 = QtWidgets.QLabel(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_17.sizePolicy().hasHeightForWidth()) self.label_17.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.label_17.setFont(font) self.label_17.setAlignment(QtCore.Qt.AlignCenter) self.label_17.setObjectName("label_17") self.verticalLayout_11.addWidget(self.label_17) self.spinBox_9 = QtWidgets.QSpinBox(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.spinBox_9.sizePolicy().hasHeightForWidth()) self.spinBox_9.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.spinBox_9.setFont(font) self.spinBox_9.setMaximum(1) self.spinBox_9.setObjectName("spinBox_9") self.verticalLayout_11.addWidget(self.spinBox_9) self.horizontalLayout_2.addLayout(self.verticalLayout_11) self.verticalLayout_10 = QtWidgets.QVBoxLayout() self.verticalLayout_10.setObjectName("verticalLayout_10") self.label_16 = QtWidgets.QLabel(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_16.sizePolicy().hasHeightForWidth()) self.label_16.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.label_16.setFont(font) self.label_16.setAlignment(QtCore.Qt.AlignCenter) self.label_16.setObjectName("label_16") self.verticalLayout_10.addWidget(self.label_16) self.spinBox_10 = QtWidgets.QSpinBox(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.spinBox_10.sizePolicy().hasHeightForWidth()) self.spinBox_10.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.spinBox_10.setFont(font) self.spinBox_10.setMaximum(1) self.spinBox_10.setObjectName("spinBox_10") self.verticalLayout_10.addWidget(self.spinBox_10) self.horizontalLayout_2.addLayout(self.verticalLayout_10) self.verticalLayout_9 = QtWidgets.QVBoxLayout() self.verticalLayout_9.setObjectName("verticalLayout_9") self.label_15 = QtWidgets.QLabel(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_15.sizePolicy().hasHeightForWidth()) self.label_15.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.label_15.setFont(font) self.label_15.setAlignment(QtCore.Qt.AlignCenter) self.label_15.setObjectName("label_15") self.verticalLayout_9.addWidget(self.label_15) self.spinBox_11 = QtWidgets.QSpinBox(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.spinBox_11.sizePolicy().hasHeightForWidth()) self.spinBox_11.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.spinBox_11.setFont(font) self.spinBox_11.setMaximum(1) self.spinBox_11.setObjectName("spinBox_11") self.verticalLayout_9.addWidget(self.spinBox_11) self.horizontalLayout_2.addLayout(self.verticalLayout_9) self.verticalLayout_8 = QtWidgets.QVBoxLayout() self.verticalLayout_8.setObjectName("verticalLayout_8") self.label_14 = QtWidgets.QLabel(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_14.sizePolicy().hasHeightForWidth()) self.label_14.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.label_14.setFont(font) self.label_14.setAlignment(QtCore.Qt.AlignCenter) self.label_14.setObjectName("label_14") self.verticalLayout_8.addWidget(self.label_14) self.spinBox = QtWidgets.QSpinBox(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.spinBox.sizePolicy().hasHeightForWidth()) self.spinBox.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.spinBox.setFont(font) self.spinBox.setSuffix("") self.spinBox.setMaximum(1) self.spinBox.setSingleStep(1) self.spinBox.setObjectName("spinBox") self.verticalLayout_8.addWidget(self.spinBox) self.horizontalLayout_2.addLayout(self.verticalLayout_8) self.verticalLayout.addLayout(self.horizontalLayout_2) self.horizontalLayout_5 = QtWidgets.QHBoxLayout() self.horizontalLayout_5.setObjectName("horizontalLayout_5") self.verticalLayout_7 = QtWidgets.QVBoxLayout() self.verticalLayout_7.setObjectName("verticalLayout_7") self.label_5 = QtWidgets.QLabel(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_5.sizePolicy().hasHeightForWidth()) self.label_5.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.label_5.setFont(font) self.label_5.setAlignment(QtCore.Qt.AlignCenter) self.label_5.setObjectName("label_5") self.verticalLayout_7.addWidget(self.label_5) self.lineEdit = QtWidgets.QLineEdit(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.lineEdit.sizePolicy().hasHeightForWidth()) self.lineEdit.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.lineEdit.setFont(font) self.lineEdit.setText("") self.lineEdit.setAlignment(QtCore.Qt.AlignCenter) self.lineEdit.setObjectName("lineEdit") self.verticalLayout_7.addWidget(self.lineEdit) self.horizontalLayout_5.addLayout(self.verticalLayout_7) self.verticalLayout_6 = QtWidgets.QVBoxLayout() self.verticalLayout_6.setObjectName("verticalLayout_6") self.label_3 = QtWidgets.QLabel(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_3.sizePolicy().hasHeightForWidth()) self.label_3.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.label_3.setFont(font) self.label_3.setAlignment(QtCore.Qt.AlignCenter) self.label_3.setObjectName("label_3") self.verticalLayout_6.addWidget(self.label_3) self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.comboBox_2.sizePolicy().hasHeightForWidth()) self.comboBox_2.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.comboBox_2.setFont(font) self.comboBox_2.setObjectName("comboBox_2") self.verticalLayout_6.addWidget(self.comboBox_2) self.horizontalLayout_5.addLayout(self.verticalLayout_6) self.verticalLayout.addLayout(self.horizontalLayout_5) self.pushButton = QtWidgets.QPushButton(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth()) self.pushButton.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("楷体") font.setPointSize(12) self.pushButton.setFont(font) self.pushButton.setObjectName("pushButton") self.verticalLayout.addWidget(self.pushButton) self.line = QtWidgets.QFrame(self.centralwidget) self.line.setFrameShape(QtWidgets.QFrame.HLine) self.line.setFrameShadow(QtWidgets.QFrame.Sunken) self.line.setObjectName("line") self.verticalLayout.addWidget(self.line) self.horizontalLayout_4 = QtWidgets.QHBoxLayout() self.horizontalLayout_4.setObjectName("horizontalLayout_4") self.verticalLayout_4 = QtWidgets.QVBoxLayout() self.verticalLayout_4.setSpacing(7) self.verticalLayout_4.setObjectName("verticalLayout_4") self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.pushButton_5.sizePolicy().hasHeightForWidth()) self.pushButton_5.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("楷体") font.setPointSize(12) self.pushButton_5.setFont(font) self.pushButton_5.setObjectName("pushButton_5") self.verticalLayout_4.addWidget(self.pushButton_5) self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.pushButton_2.sizePolicy().hasHeightForWidth()) self.pushButton_2.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("楷体") font.setPointSize(12) self.pushButton_2.setFont(font) self.pushButton_2.setObjectName("pushButton_2") self.verticalLayout_4.addWidget(self.pushButton_2) self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.pushButton_4.sizePolicy().hasHeightForWidth()) self.pushButton_4.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("楷体") font.setPointSize(12) self.pushButton_4.setFont(font) self.pushButton_4.setObjectName("pushButton_4") self.verticalLayout_4.addWidget(self.pushButton_4) self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget) font = QtGui.QFont() font.setFamily("楷体") font.setPointSize(12) self.pushButton_6.setFont(font) self.pushButton_6.setObjectName("pushButton_6") self.verticalLayout_4.addWidget(self.pushButton_6) self.horizontalLayout = QtWidgets.QHBoxLayout() self.horizontalLayout.setObjectName("horizontalLayout") self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.pushButton_3.sizePolicy().hasHeightForWidth()) self.pushButton_3.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("楷体") font.setPointSize(12) self.pushButton_3.setFont(font) self.pushButton_3.setObjectName("pushButton_3") self.horizontalLayout.addWidget(self.pushButton_3) self.verticalLayout_4.addLayout(self.horizontalLayout) self.horizontalLayout_4.addLayout(self.verticalLayout_4) self.line_2 = QtWidgets.QFrame(self.centralwidget) self.line_2.setFrameShape(QtWidgets.QFrame.VLine) self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken) self.line_2.setObjectName("line_2") self.horizontalLayout_4.addWidget(self.line_2) spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout_4.addItem(spacerItem1) self.verticalLayout_3 = QtWidgets.QVBoxLayout() self.verticalLayout_3.setObjectName("verticalLayout_3") self.label_2 = QtWidgets.QLabel(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_2.sizePolicy().hasHeightForWidth()) self.label_2.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("楷体") font.setPointSize(12) self.label_2.setFont(font) self.label_2.setAlignment(QtCore.Qt.AlignCenter) self.label_2.setObjectName("label_2") self.verticalLayout_3.addWidget(self.label_2) self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.MinimumExpanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.textBrowser.sizePolicy().hasHeightForWidth()) self.textBrowser.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("楷体") font.setPointSize(12) self.textBrowser.setFont(font) self.textBrowser.setObjectName("textBrowser") self.verticalLayout_3.addWidget(self.textBrowser) self.horizontalLayout_4.addLayout(self.verticalLayout_3) spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout_4.addItem(spacerItem2) self.verticalLayout_5 = QtWidgets.QVBoxLayout() self.verticalLayout_5.setObjectName("verticalLayout_5") self.label_4 = QtWidgets.QLabel(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.label_4.sizePolicy().hasHeightForWidth()) self.label_4.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("楷体") font.setPointSize(12) self.label_4.setFont(font) self.label_4.setAlignment(QtCore.Qt.AlignCenter) self.label_4.setObjectName("label_4") self.verticalLayout_5.addWidget(self.label_4) self.textBrowser_2 = QtWidgets.QTextBrowser(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.MinimumExpanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.textBrowser_2.sizePolicy().hasHeightForWidth()) self.textBrowser_2.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.textBrowser_2.setFont(font) self.textBrowser_2.setObjectName("textBrowser_2") self.verticalLayout_5.addWidget(self.textBrowser_2) self.horizontalLayout_4.addLayout(self.verticalLayout_5) spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout_4.addItem(spacerItem3) self.verticalLayout_2 = QtWidgets.QVBoxLayout() self.verticalLayout_2.setObjectName("verticalLayout_2") self.label_6 = QtWidgets.QLabel(self.centralwidget) font = QtGui.QFont() font.setFamily("楷体") font.setPointSize(12) self.label_6.setFont(font) self.label_6.setAlignment(QtCore.Qt.AlignCenter) self.label_6.setObjectName("label_6") self.verticalLayout_2.addWidget(self.label_6) self.textBrowser_3 = QtWidgets.QTextBrowser(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.MinimumExpanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.textBrowser_3.sizePolicy().hasHeightForWidth()) self.textBrowser_3.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setFamily("Times New Roman") font.setPointSize(12) self.textBrowser_3.setFont(font) self.textBrowser_3.setObjectName("textBrowser_3") self.verticalLayout_2.addWidget(self.textBrowser_3) self.horizontalLayout_4.addLayout(self.verticalLayout_2) spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout_4.addItem(spacerItem4) spacerItem5 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) self.horizontalLayout_4.addItem(spacerItem5) self.verticalLayout.addLayout(self.horizontalLayout_4) self.line_3 = QtWidgets.QFrame(self.centralwidget) self.line_3.setFrameShape(QtWidgets.QFrame.HLine) self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken) self.line_3.setObjectName("line_3") self.verticalLayout.addWidget(self.line_3) self.horizontalLayout_3 = QtWidgets.QHBoxLayout() self.horizontalLayout_3.setObjectName("horizontalLayout_3") self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.graphicsView.sizePolicy().hasHeightForWidth()) self.graphicsView.setSizePolicy(sizePolicy) self.graphicsView.setObjectName("graphicsView") self.horizontalLayout_3.addWidget(self.graphicsView) self.verticalLayout.addLayout(self.horizontalLayout_3) MainWindow.setCentralWidget(self.centralwidget) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.label_25.setText(_translate("MainWindow", "STDE")) self.label_23.setText(_translate("MainWindow", "DATA9")) self.label_22.setText(_translate("MainWindow", "DATA8")) self.label_21.setText(_translate("MainWindow", "DATA7")) self.label_20.setText(_translate("MainWindow", "DATA6")) self.label_19.setText(_translate("MainWindow", "DATA5")) self.label_18.setText(_translate("MainWindow", "DATA4")) self.label_17.setText(_translate("MainWindow", "DATA3")) self.label_16.setText(_translate("MainWindow", "DATA2")) self.label_15.setText(_translate("MainWindow", "DATA1")) self.label_14.setText(_translate("MainWindow", "DATA0")) self.label_5.setText(_translate("MainWindow", "ADDR")) self.label_3.setText(_translate("MainWindow", "STDE_NUM")) self.pushButton.setText(_translate("MainWindow", "确定")) self.pushButton_5.setText(_translate("MainWindow", "读出寄存器")) self.pushButton_2.setText(_translate("MainWindow", "写入寄存器")) self.pushButton_4.setText(_translate("MainWindow", "存入存储器")) self.pushButton_6.setText(_translate("MainWindow", "存入初始化")) self.pushButton_3.setText(_translate("MainWindow", "初始化SPI")) self.label_2.setText(_translate("MainWindow", "预览数据")) self.label_4.setText(_translate("MainWindow", "读取数据")) self.label_6.setText(_translate("MainWindow", "初始化指令"))
2,主代码:
from PyQt5 import QtWidgets from PyQt5 import QtGui from PyQt5.QtWidgets import QMessageBox, QGraphicsPixmapItem import GUI import sys import ControlSPI import time import re class my_mainwindow(QtWidgets.QMainWindow): def __init__(self): #PyQt5中,每个应用程序都必须实例化一个QApplication(): app = QtWidgets.QApplication(sys.argv) my_MainWindow = MainWindow()#这里用重写的那个mainwindow类 self.my_ui = GUI.Ui_MainWindow() self.my_ui.setupUi(my_MainWindow) ############################################################################################################ self.my_ui.textBrowser.setFixedHeight(300) self.my_ui.textBrowser_2.setFixedHeight(300) self.my_ui.textBrowser_3.setFixedHeight(300) self.my_ui.textBrowser.setFixedWidth(300) self.my_ui.textBrowser_2.setFixedWidth(200) self.my_ui.textBrowser_3.setFixedWidth(200) ##################################################################################################### self.STDE_infomation = ["L-STDE-A", "H-STDE-A", "L-STDE-B", "H-STDE-B", "L-STDE-C", "H-STDE-C"] self.STDE_num = ["1", "2", "3", "4", "5", "6"] self.my_ui.comboBox.addItems(self.STDE_infomation) self.my_ui.comboBox_2.addItems(self.STDE_num) self.my_ui.lineEdit.setPlaceholderText('0x+16进制数字') self.pix = QtGui.QPixmap('2.png') self.scene = QtWidgets.QGraphicsScene()#创建场景 self.item = QGraphicsPixmapItem(self.pix) # 创建像素图元 self.item.setScale(1.49)#放大1.49倍 self.scene.addItem(self.item)#将图元添加到场景中 self.my_ui.graphicsView.setScene(self.scene)#将场景加入到视图中显示出来 self.click_pushbutton(my_MainWindow) self.my_ui.comboBox.activated.connect(lambda :self.Check_process(my_MainWindow))#combobox动作了之后,执行check_process函数 ######################################################################################################################### #调用QWidget对象的show()方法将创建的窗口显示出来 #调用应用程序对象的exec_()方法来运行程序的主循环,并使用sys.exit()方法确保程序能够完美的退出。 my_MainWindow.show() sys.exit(app.exec_()) def click_pushbutton(self,my_MainWindow): self.my_ui.pushButton.clicked.connect(lambda:self.DATA_get(my_MainWindow)) self.my_ui.pushButton_2.clicked.connect(lambda:self.Check_process(my_MainWindow)) self.my_ui.pushButton_4.clicked.connect(lambda:self.Check_process(my_MainWindow)) self.my_ui.pushButton_3.clicked.connect(lambda:self.Check_process(my_MainWindow)) self.my_ui.pushButton_5.clicked.connect(lambda:self.Check_process(my_MainWindow)) self.my_ui.pushButton_6.clicked.connect(lambda: self.Check_process(my_MainWindow)) def DATA_get(self, my_MainWindow): pattern = '0[x|X][0|1][0-9a-fA-F]'#判断模板:0x** 十六进制格式 ADDR = self.my_ui.lineEdit.text() if len(ADDR) == 4: flag = re.match(pattern, ADDR) # 如何判断输入的字符是否符合相应的要求呢?使用正则表达式,即使用re模块。 if flag: ADDR_Bin = bin(int(ADDR, 16))[2:] # 转换为二进制,并且去掉“0b” ADDR_Bin = ADDR_Bin.rjust(5, '0') # 原字符串右侧对齐, 左侧补零,补齐后总位宽为8 else: QMessageBox.warning(my_MainWindow, "警告", "输入ADDR不对哟", QMessageBox.Yes) return else: QMessageBox.warning(my_MainWindow, "警告", "输入ADDR只能是4个字符呐", QMessageBox.Yes) return QMessageBox.warning(my_MainWindow, "警告", "你成功引起了我的注意", QMessageBox.Yes) def Check_process(self,my_MainWindow): time.sleep(1) QMessageBox.warning(my_MainWindow, "警告", "你成功引起了我的注意", QMessageBox.Yes) class MainWindow(QtWidgets.QMainWindow): """对QMainWindow类重写,实现一些功能""" def closeEvent(self, event): """ 重写closeEvent方法,实现QMainWindow窗体关闭时执行一些代码 :param event: close()触发的事件 :return: None """ reply = QtWidgets.QMessageBox.question(self, '本程序', "是否要退出程序?", QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No) if reply == QtWidgets.QMessageBox.Yes: event.accept() # Close device ControlSPI.VSI_CloseDevice(ControlSPI.VSI_USBSPI, 0) else: event.ignore() if __name__ == "__main__": my_mainwindow()