Python使用PyQt5库 一、Designer界面的使用

时间:2021-06-10 23:04:22

台平:Mac OS
不介绍PyQt5的安装与Designer的安装,这里使用的是Qt 5.7自带的Designer界面编辑器
打开Designer界面后创建一个界面。

这里附上这个文件代码:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>336</width>
<height>463</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="Process_textLine">
<property name="geometry">
<rect>
<x>70</x>
<y>380</y>
<width>181</width>
<height>32</height>
</rect>
</property>
<property name="text">
<string>处理文件行内容无重复</string>
</property>
</widget>
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>50</x>
<y>11</y>
<width>242</width>
<height>162</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QLineEdit" name="PathSource">
<property name="enabled">
<bool>true</bool>
</property>
<property name="maximumSize">
<size>
<width>133</width>
<height>21</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="SelectFileButton">
<property name="text">
<string>数据文件1</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QLineEdit" name="PathSource_2">
<property name="maximumSize">
<size>
<width>133</width>
<height>21</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="SelectFileButton_2">
<property name="text">
<string>数据文件2</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QLineEdit" name="lineEdit">
<property name="maximumSize">
<size>
<width>133</width>
<height>21</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="outputFile">
<property name="text">
<string>输出文件</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QPushButton" name="screening">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>40</height>
</size>
</property>
<property name="text">
<string>筛选</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>100</x>
<y>250</y>
<width>131</width>
<height>69</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<property name="spacing">
<number>10</number>
</property>
<item>
<widget class="QSpinBox" name="spinBox">
<property name="maximum">
<number>9</number>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QLabel" name="label">
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="text">
<string>To</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QSpinBox" name="spinBox_2">
<property name="maximum">
<number>9</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QPushButton" name="Process_text4last">
<property name="text">
<string>处理</string>
</property>
</widget>
</item>
</layout>
</widget>
<zorder>PathSource</zorder>
<zorder>SelectFileButton</zorder>
<zorder>SelectFileButton</zorder>
<zorder>PathSource_2</zorder>
<zorder>SelectFileButton_2</zorder>
<zorder>screening</zorder>
<zorder>Process_textLine</zorder>
<zorder>lineEdit</zorder>
<zorder>outputFile</zorder>
<zorder>spinBox</zorder>
<zorder>spinBox_2</zorder>
<zorder>Process_text4last</zorder>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

安装PyQt5库后会有工具将.ui转换成python界面代码
pyuic5

使命令行进入到.ui存在的目录下
pyuic5 test.ui -o AppMain.py
这个命令会将test.ui界面文件生成 AppMain.py文件,AppMain.py文件里则是输出的Python语言pyqt5的相关代码类

附所输出的代码

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.7
#
# 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(336, 463)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.Process_textLine = QtWidgets.QPushButton(self.centralwidget)
self.Process_textLine.setGeometry(QtCore.QRect(70, 380, 181, 32))
self.Process_textLine.setObjectName("Process_textLine")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(50, 11, 242, 162))
self.widget.setObjectName("widget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setSpacing(0)
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setSpacing(0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.PathSource = QtWidgets.QLineEdit(self.widget)
self.PathSource.setEnabled(True)
self.PathSource.setMaximumSize(QtCore.QSize(133, 21))
self.PathSource.setObjectName("PathSource")
self.horizontalLayout.addWidget(self.PathSource)
self.SelectFileButton = QtWidgets.QPushButton(self.widget)
self.SelectFileButton.setObjectName("SelectFileButton")
self.horizontalLayout.addWidget(self.SelectFileButton)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setSpacing(0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.PathSource_2 = QtWidgets.QLineEdit(self.widget)
self.PathSource_2.setMaximumSize(QtCore.QSize(133, 21))
self.PathSource_2.setObjectName("PathSource_2")
self.horizontalLayout_2.addWidget(self.PathSource_2)
self.SelectFileButton_2 = QtWidgets.QPushButton(self.widget)
self.SelectFileButton_2.setObjectName("SelectFileButton_2")
self.horizontalLayout_2.addWidget(self.SelectFileButton_2)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setSpacing(0)
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.lineEdit = QtWidgets.QLineEdit(self.widget)
self.lineEdit.setMaximumSize(QtCore.QSize(133, 21))
self.lineEdit.setObjectName("lineEdit")
self.horizontalLayout_3.addWidget(self.lineEdit)
self.outputFile = QtWidgets.QPushButton(self.widget)
self.outputFile.setObjectName("outputFile")
self.horizontalLayout_3.addWidget(self.outputFile)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.screening = QtWidgets.QPushButton(self.widget)
self.screening.setMaximumSize(QtCore.QSize(16777215, 40))
self.screening.setObjectName("screening")
self.verticalLayout.addWidget(self.screening)
self.widget1 = QtWidgets.QWidget(self.centralwidget)
self.widget1.setGeometry(QtCore.QRect(100, 250, 131, 69))
self.widget1.setObjectName("widget1")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.widget1)
self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setSpacing(10)
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.spinBox = QtWidgets.QSpinBox(self.widget1)
self.spinBox.setMaximum(9)
self.spinBox.setObjectName("spinBox")
self.horizontalLayout_4.addWidget(self.spinBox)
self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.label = QtWidgets.QLabel(self.widget1)
self.label.setLayoutDirection(QtCore.Qt.LeftToRight)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.horizontalLayout_5.addWidget(self.label)
self.horizontalLayout_4.addLayout(self.horizontalLayout_5)
self.spinBox_2 = QtWidgets.QSpinBox(self.widget1)
self.spinBox_2.setMaximum(9)
self.spinBox_2.setObjectName("spinBox_2")
self.horizontalLayout_4.addWidget(self.spinBox_2)
self.verticalLayout_2.addLayout(self.horizontalLayout_4)
self.Process_text4last = QtWidgets.QPushButton(self.widget1)
self.Process_text4last.setObjectName("Process_text4last")
self.verticalLayout_2.addWidget(self.Process_text4last)
self.PathSource.raise_()
self.SelectFileButton.raise_()
self.SelectFileButton.raise_()
self.PathSource_2.raise_()
self.SelectFileButton_2.raise_()
self.screening.raise_()
self.Process_textLine.raise_()
self.lineEdit.raise_()
self.outputFile.raise_()
self.spinBox.raise_()
self.spinBox_2.raise_()
self.Process_text4last.raise_()
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.Process_textLine.setText(_translate("MainWindow", "处理文件行内容无重复"))
self.SelectFileButton.setText(_translate("MainWindow", "数据文件1"))
self.SelectFileButton_2.setText(_translate("MainWindow", "数据文件2"))
self.outputFile.setText(_translate("MainWindow", "输出文件"))
self.screening.setText(_translate("MainWindow", "筛选"))
self.label.setText(_translate("MainWindow", "To"))
self.Process_text4last.setText(_translate("MainWindow", "处理"))

#END

现在测试一下这个界面

打开AppMain.py
在#END后添加以下代码

class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setupUi(self)

if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
mywindow = MyWindow()
mywindow.show()
sys.exit(app.exec_())

我使用的是继承的方式,当然还有其他方式,这样的话可以使视图(界面)与功能区分开来,方便维护。

当然肯定是运行成功的!
我就不附图了,本人比较懒。