界面代码
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'try_gui.ui'
#
# Created by: PyQt5 UI code generator 5.13.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(493, 473)
self.xianshi2 = QtWidgets.QTextEdit(Form)
self.xianshi2.setGeometry(QtCore.QRect(30, 100, 251, 361))
self.xianshi2.setLineWrapMode(QtWidgets.QTextEdit.WidgetWidth)
self.xianshi2.setReadOnly(True)
self.xianshi2.setObjectName("xianshi2")
self.pb_gaojing = QtWidgets.QPushButton(Form)
self.pb_gaojing.setGeometry(QtCore.QRect(310, 322, 80, 23))
self.pb_gaojing.setObjectName("pb_gaojing")
self.pb_shebeiID = QtWidgets.QPushButton(Form)
self.pb_shebeiID.setEnabled(True)
self.pb_shebeiID.setGeometry(QtCore.QRect(310, 351, 80, 23))
self.pb_shebeiID.setSizeIncrement(QtCore.QSize(0, 0))
self.pb_shebeiID.setObjectName("pb_shebeiID")
self.pb_shujuleixing = QtWidgets.QPushButton(Form)
self.pb_shujuleixing.setGeometry(QtCore.QRect(310, 380, 80, 23))
self.pb_shujuleixing.setObjectName("pb_shujuleixing")
self.label_daoru = QtWidgets.QLabel(Form)
self.label_daoru.setGeometry(QtCore.QRect(10, 18, 54, 12))
self.label_daoru.setObjectName("label_daoru")
self.label_zhuangtai = QtWidgets.QLabel(Form)
self.label_zhuangtai.setGeometry(QtCore.QRect(10, 80, 61, 16))
self.label_zhuangtai.setObjectName("label_zhuangtai")
self.label_gongneng = QtWidgets.QLabel(Form)
self.label_gongneng.setGeometry(QtCore.QRect(298, 130, 54, 12))
self.label_gongneng.setObjectName("label_gongneng")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(398, 355, 54, 16))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(Form)
self.label_4.setGeometry(QtCore.QRect(398, 385, 48, 16))
self.label_4.setObjectName("label_4")
self.pb_kongge = QtWidgets.QPushButton(Form)
self.pb_kongge.setEnabled(False)
self.pb_kongge.setGeometry(QtCore.QRect(311, 413, 75, 23))
self.pb_kongge.setObjectName("pb_kongge")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setEnabled(False)
self.pushButton_2.setGeometry(QtCore.QRect(311, 443, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.layoutWidget = QtWidgets.QWidget(Form)
self.layoutWidget.setGeometry(QtCore.QRect(308, 150, 179, 54))
self.layoutWidget.setObjectName("layoutWidget")
self.formLayout = QtWidgets.QFormLayout(self.layoutWidget)
self.formLayout.setContentsMargins(0, 0, 0, 0)
self.formLayout.setObjectName("formLayout")
self.pb_kongzhiwei = QtWidgets.QPushButton(self.layoutWidget)
self.pb_kongzhiwei.setObjectName("pb_kongzhiwei")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.pb_kongzhiwei)
self.pb_dianhao = QtWidgets.QPushButton(self.layoutWidget)
self.pb_dianhao.setObjectName("pb_dianhao")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.pb_dianhao)
self.label = QtWidgets.QLabel(self.layoutWidget)
self.label.setTextFormat(QtCore.Qt.AutoText)
self.label.setObjectName("label")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.label)
self.layoutWidget1 = QtWidgets.QWidget(Form)
self.layoutWidget1.setGeometry(QtCore.QRect(308, 220, 119, 83))
self.layoutWidget1.setObjectName("layoutWidget1")
self.formLayout_2 = QtWidgets.QFormLayout(self.layoutWidget1)
self.formLayout_2.setContentsMargins(0, 0, 0, 0)
self.formLayout_2.setObjectName("formLayout_2")
self.pb_xinghaoleixing = QtWidgets.QPushButton(self.layoutWidget1)
self.pb_xinghaoleixing.setObjectName("pb_xinghaoleixing")
self.formLayout_2.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.pb_xinghaoleixing)
self.label_2 = QtWidgets.QLabel(self.layoutWidget1)
self.label_2.setObjectName("label_2")
self.formLayout_2.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.label_2)
self.pb_guangzi = QtWidgets.QPushButton(self.layoutWidget1)
self.pb_guangzi.setObjectName("pb_guangzi")
self.formLayout_2.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.pb_guangzi)
self.pb_qufan = QtWidgets.QPushButton(self.layoutWidget1)
self.pb_qufan.setObjectName("pb_qufan")
self.formLayout_2.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.pb_qufan)
self.layoutWidget2 = QtWidgets.QWidget(Form)
self.layoutWidget2.setGeometry(QtCore.QRect(31, 39, 421, 25))
self.layoutWidget2.setObjectName("layoutWidget2")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget2)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.xianshi1 = QtWidgets.QLineEdit(self.layoutWidget2)
self.xianshi1.setReadOnly(True)
self.xianshi1.setObjectName("xianshi1")
self.horizontalLayout.addWidget(self.xianshi1)
self.pb_daorutxt = QtWidgets.QPushButton(self.layoutWidget2)
self.pb_daorutxt.setObjectName("pb_daorutxt")
self.horizontalLayout.addWidget(self.pb_daorutxt)
self.pb_shuchutxt = QtWidgets.QPushButton(self.layoutWidget2)
self.pb_shuchutxt.setObjectName("pb_shuchutxt")
self.horizontalLayout.addWidget(self.pb_shuchutxt)
self.label_gongneng_2 = QtWidgets.QLabel(Form)
self.label_gongneng_2.setGeometry(QtCore.QRect(300, 80, 101, 16))
self.label_gongneng_2.setObjectName("label_gongneng_2")
self.lineEdit_zhanmingsuoxie = QtWidgets.QLineEdit(Form)
self.lineEdit_zhanmingsuoxie.setGeometry(QtCore.QRect(310, 100, 71, 20))
self.lineEdit_zhanmingsuoxie.setObjectName("lineEdit_zhanmingsuoxie")
self.retranslateUi(Form)
self.pb_daorutxt.clicked.connect(Form.pb_daorutxt1)
self.pb_xinghaoleixing.clicked.connect(Form.pb_xinghaoleixing1)
self.pb_shuchutxt.clicked.connect(Form.pb_shuchutxt1)
self.pb_kongzhiwei.clicked.connect(Form.pb_kongzhiwei1)
self.pb_dianhao.clicked.connect(Form.pb_dianhao1)
self.pb_guangzi.clicked.connect(Form.pb_guangzipai1)
self.pb_qufan.clicked.connect(Form.pb_qufan1)
self.pb_gaojing.clicked.connect(Form.pb_gaojing1)
self.pb_shebeiID.clicked.connect(Form.pb_shebeiID1)
self.pb_shujuleixing.clicked.connect(Form.pb_xinghaoleixing1)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pb_gaojing.setText(_translate("Form", "生成告警级别"))
self.pb_shebeiID.setText(_translate("Form", "生成设备ID"))
self.pb_shujuleixing.setText(_translate("Form", "生成数据类型"))
self.label_daoru.setText(_translate("Form", "导入模块"))
self.label_zhuangtai.setText(_translate("Form", "状态显示栏"))
self.label_gongneng.setText(_translate("Form", "功能模块"))
self.label_3.setText(_translate("Form", "CB911那个"))
self.label_4.setText(_translate("Form", "软硬接点"))
self.pb_kongge.setText(_translate("Form", "替换空格"))
self.pushButton_2.setText(_translate("Form", "替换双引号"))
self.pb_kongzhiwei.setText(_translate("Form", "控制位=0"))
self.pb_dianhao.setText(_translate("Form", "初始化点号"))
self.label.setText(_translate("Form", "最后几列全部置-1"))
self.pb_xinghaoleixing.setText(_translate("Form", "信号类型=1"))
self.label_2.setText(_translate("Form", "第一列"))
self.pb_guangzi.setText(_translate("Form", "光字牌=1"))
self.pb_qufan.setText(_translate("Form", "取反=0"))
self.pb_daorutxt.setText(_translate("Form", "导入TXT"))
self.pb_shuchutxt.setText(_translate("Form", "输出TXT"))
self.label_gongneng_2.setText(_translate("Form", "输入站名缩写:"))
逻辑代码,基于多继承
import pandas as pd
import re
from PyQt5.Qt import *
from try_gui import Ui_Form
class Res(QWidget, Ui_Form):
hao = pd.DataFrame
txtpath = ''
def __init__(self):
super().__init__()
self.setupUi(self)
# 导入TXT
def pb_daorutxt1(self):
h2 = QFileDialog.getOpenFileName(self, '打开文件', '.', '点表文件(*.txt)')
self.xianshi1.setText(h2[0])
self.txtpath = h2[0]
h2 = pd.read_csv(h2[0], sep='\t', encoding='gbk')
bzb = pd.read_excel('./标准表.xls')
self.hao = h2
# 0列,信号类型
def pb_xinghaoleixing1(self):
for i in self.hao.index:
self.hao['信号类型'].at[i] = 1
self.xianshi2.insertPlainText('信号类型置1-完成:)\n')
# 设置滚动条始终在最下面
self.xianshi2.verticalScrollBar().setValue(self.xianshi2.verticalScrollBar().maximum())
# print(11)
#2列,设备ID
def pb_shebeiID1(self):
ststionname = self.lineEdit_zhanmingsuoxie.text()
for i in self.hao.index:
jgmc_p = r'开关'
jgmc_q = r'主变'
jgmc_s = str(self.hao['间隔名称'].at[i])
jgmc_match1 = re.sub(jgmc_p, '', jgmc_s) # 取到调号
jgmc_match2 = re.sub(jgmc_q, '', jgmc_s) # 取到调号
# print(jgmc_match1)
# print(jgmc_match2)
if '开关' in str(self.hao['间隔名称'].at[i]):
self.hao['所属设备ID'].at[i] = ststionname + 'CB' + jgmc_match1
elif '主变' in str(self.hao['间隔名称'].at[i]):
self.hao['所属设备ID'].at[i] = ststionname + 'XF' + jgmc_match2
else:
pass
self.xianshi2.insertPlainText('生成设备ID-完成:)\n')
#4列,数据类型
def pb_shujuleixing1(self):
for i in self.hao.index:
if pd.isnull(self.hao.iloc[i, 10]): # 若装置名称为空,则数据类型赋值1
self.hao['数据类型'].at[i] = 1
else:
self.hao['数据类型'].at[i] = 0
self.xianshi2.insertPlainText('生成软硬接点-完成:)\n')
# 5列,告警级别
def pb_gaojing1(self):
bzb = pd.read_excel('./标准表.xls')
for j in bzb.index:
for i in self.hao.index:
if str(bzb['46类信号'].at[j]) in self.hao['规范后名称'].at[i]:
self.hao['告警级别'].at[i] = '4,6'
for j in bzb.index:
for i in self.hao.index:
if str(bzb['3类信号'].at[j]) in self.hao['规范后名称'].at[i]:
self.hao['告警级别'].at[i] = '3'
for j in bzb.index:
for i in self.hao.index:
if str(bzb['2类信号'].at[j]) in self.hao['规范后名称'].at[i]:
self.hao['告警级别'].at[i] = '2'
for j in bzb.index:
for i in self.hao.index:
if str(bzb['1类信号'].at[j]) in self.hao['规范后名称'].at[i]:
self.hao['告警级别'].at[i] = '1'
self.xianshi2.insertPlainText('生成告警级别-完成:)\n')
#6列,控制位
def pb_kongzhiwei1(self):
for i in self.hao.index:
self.hao['控制位'].at[i] = 0
self.xianshi2.insertPlainText('控制位置0-完成:)\n')
#15列,光字牌
def pb_guangzipai1(self):
for i in self.hao.index:
self.hao['是否上光字牌'].at[i] = 1
self.xianshi2.insertPlainText('是否上光字牌置1-完成:)\n')
#16列,取反
def pb_qufan1(self):
for i in self.hao.index:
self.hao['是否取反'].at[i] = 0
self.xianshi2.insertPlainText('取反置0-完成:)\n')
#初始化最后一大块
def pb_dianhao1(self):
for i in self.hao.index:
self.hao.iloc[i, [17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28]] = -1
self.xianshi2.insertPlainText('最后一大块的初始化-完成:)\n')
#输出TXT
def pb_shuchutxt1(self):
# print(22)
# print(self.txtpath)
url2 = self.txtpath
pattern2 = r'[.]' # 分割字符串做输出路径
burl = re.split(pattern2, url2)
self.hao.to_csv(burl[0] + 'out.txt', sep='\t', index=False)
self.xianshi2.insertPlainText('输出点表-完成:)\n')
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Res()
window.show()
sys.exit(app.exec_())
逻辑代码,基于单继承
关于逻辑的代码都用self
关于界面的代码都用self.ui
import pandas as pd
import re
import sys
from PyQt5.Qt import *
from try_gui import Ui_Form
class Res(QWidget):
hao = pd.DataFrame
txtpath = ''
def __init__(self,parent=None):
super().__init__(parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
# 导入TXT
def pb_daorutxt1(self):
h2 = QFileDialog.getOpenFileName(self, '打开文件', '.', '点表文件(*.txt)')
self.ui.xianshi1.setText(h2[0])
self.txtpath = h2[0]
h2 = pd.read_csv(h2[0], sep='\t', encoding='gbk')
bzb = pd.read_excel('./标准表.xls')
self.hao = h2
# 0列,信号类型
def pb_xinghaoleixing1(self):
for i in self.hao.index:
self.hao['信号类型'].at[i] = 1
self.ui.xianshi2.insertPlainText('信号类型置1-完成:)\n')
# 设置滚动条始终在最下面
self.ui.xianshi2.verticalScrollBar().setValue(self.ui.xianshi2.verticalScrollBar().maximum())
# print(11)
#2列,设备ID
def pb_shebeiID1(self):
ststionname = self.ui.lineEdit_zhanmingsuoxie.text()
for i in self.hao.index:
jgmc_p = r'开关'
jgmc_q = r'主变'
jgmc_s = str(self.hao['间隔名称'].at[i])
jgmc_match1 = re.sub(jgmc_p, '', jgmc_s) # 取到调号
jgmc_match2 = re.sub(jgmc_q, '', jgmc_s) # 取到调号
# print(jgmc_match1)
# print(jgmc_match2)
if '开关' in str(self.hao['间隔名称'].at[i]):
self.hao['所属设备ID'].at[i] = ststionname + 'CB' + jgmc_match1
elif '主变' in str(self.hao['间隔名称'].at[i]):
self.hao['所属设备ID'].at[i] = ststionname + 'XF' + jgmc_match2
else:
pass
self.ui.xianshi2.insertPlainText('生成设备ID-完成:)\n')
#4列,数据类型
def pb_shujuleixing1(self):
for i in self.hao.index:
if pd.isnull(self.hao.iloc[i, 10]): # 若装置名称为空,则数据类型赋值1
self.hao['数据类型'].at[i] = 1
else:
self.hao['数据类型'].at[i] = 0
self.ui.xianshi2.insertPlainText('生成软硬接点-完成:)\n')
# 5列,告警级别
def pb_gaojing1(self):
bzb = pd.read_excel('./标准表.xls')
for j in bzb.index:
for i in self.hao.index:
if str(bzb['46类信号'].at[j]) in self.hao['规范后名称'].at[i]:
self.hao['告警级别'].at[i] = '4,6'
for j in bzb.index:
for i in self.hao.index:
if str(bzb['3类信号'].at[j]) in self.hao['规范后名称'].at[i]:
self.hao['告警级别'].at[i] = '3'
for j in bzb.index:
for i in self.hao.index:
if str(bzb['2类信号'].at[j]) in self.hao['规范后名称'].at[i]:
self.hao['告警级别'].at[i] = '2'
for j in bzb.index:
for i in self.hao.index:
if str(bzb['1类信号'].at[j]) in self.hao['规范后名称'].at[i]:
self.hao['告警级别'].at[i] = '1'
self.ui.xianshi2.insertPlainText('生成告警级别-完成:)\n')
#6列,控制位
def pb_kongzhiwei1(self):
for i in self.hao.index:
self.hao['控制位'].at[i] = 0
self.ui.xianshi2.insertPlainText('控制位置0-完成:)\n')
#15列,光字牌
def pb_guangzipai1(self):
for i in self.hao.index:
self.hao['是否上光字牌'].at[i] = 1
self.ui.xianshi2.insertPlainText('是否上光字牌置1-完成:)\n')
#16列,取反
def pb_qufan1(self):
for i in self.hao.index:
self.hao['是否取反'].at[i] = 0
self.ui.xianshi2.insertPlainText('取反置0-完成:)\n')
#初始化最后一大块
def pb_dianhao1(self):
for i in self.hao.index:
self.hao.iloc[i, [17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28]] = -1
self.ui.xianshi2.insertPlainText('最后一大块的初始化-完成:)\n')
#输出TXT
def pb_shuchutxt1(self):
# print(22)
# print(self.txtpath)
url2 = self.txtpath
pattern2 = r'[.]' # 分割字符串做输出路径
burl = re.split(pattern2, url2)
self.hao.to_csv(burl[0] + 'out.txt', sep='\t', index=False)
self.ui.xianshi2.insertPlainText('输出点表-完成:)')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Res()
window.show()
sys.exit(app.exec_())
来源:CSDN
作者:好多米的秃头之路1024
链接:https://blog.csdn.net/weixin_46276803/article/details/104378480