重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

pyqt5_站点管理_stcd_windows

from PyQt5.QtWidgets import (QWidget,QTableWidget,QHBoxLayout,QVBoxLayout,QGridLayout,QGroupBox,QLineEdit,QLabel,QPushButton,
                             QHeaderView,QTableWidgetItem,QMessageBox)
import os,re
#定义站点信息窗口
class stcd_window(QWidget):
    def __init__(self):
        super().__init__()
        self.stcd_tablewidget = QTableWidget(0, 3)
        self.stcd_stcd = QLineEdit()
        self.stcd_rtu = QLineEdit()
        self.stcd_name = QLineEdit()
        self.list_stcd = []
        self.stcd_pik_path = './/DATA//STCDinfo.pik'
        self.stcd_txt_path = './/DATA//站点信息.txt'
        self.table_clicked_old = -1 #用来记录行号

        self.initUI()
    def initUI(self):
        #初始化窗口组件
        self.stcd_tablewidget.setHorizontalHeaderLabels(['站点编码', 'RTU编码', '站点名称'])
        self.stcd_tablewidget.setEditTriggers(QTableWidget.NoEditTriggers)
        table_group = QGroupBox('站点')
        table_vbox = QVBoxLayout()
        table_vbox.addWidget(self.stcd_tablewidget)
        table_group.setLayout(table_vbox)

        stcd_group = QGroupBox('站点信息配置')
        stcd_grid = QGridLayout()
        stcd_label = QLabel('站点编码:')
        rtu_label = QLabel('RTU编码:')
        name_label = QLabel('站点名称:')
        delete_button = QPushButton('删除')
        input_button = QPushButton('保存')
        delete_button.clicked.connect(self.delete_stcd_info)
        input_button.clicked.connect(self.save_stcd_info)
        self.stcd_tablewidget.cellClicked[int, int].connect(self.cell_clicked)
        stcd_grid.addWidget(stcd_label, 0, 0)
        stcd_grid.addWidget(self.stcd_stcd, 0, 1)
        stcd_grid.addWidget(rtu_label, 1, 0)
        stcd_grid.addWidget(self.stcd_rtu, 1, 1)
        stcd_grid.addWidget(name_label, 2, 0)
        stcd_grid.addWidget(self.stcd_name, 2, 1)
        button_grid = QGridLayout()
        button_grid.addWidget(delete_button, 0, 0)
        button_grid.addWidget(input_button, 0, 1)
        stcd_vbox = QVBoxLayout()
        stcd_vbox.addLayout(stcd_grid)
        stcd_vbox.addLayout(button_grid)
        stcd_group.setLayout(stcd_vbox)

        hbox = QHBoxLayout()
        hbox.addWidget(table_group)
        hbox.addWidget(stcd_group)
        self.setLayout(hbox)

        self.get_list_stcd()
        self.initTable()
    #初始化站点信息表单
    def get_list_stcd(self):
        try:
            if not os.path.exists(self.stcd_txt_path):
                with open(self.stcd_txt_path, 'w') as stcd_txt_file:
                    stcd_txt_file.write('站点编码;RTU站点;站点名称\n')
                return
            with open(self.stcd_txt_path, 'r') as stcd_txt_file:
                stcd_txt_file.readline()
                self.list_stcd.clear()
                txt_line = stcd_txt_file.readline()
                while txt_line:
                    txt_line = txt_line.strip('\n')
                    txt_line = txt_line.replace(';', ';')
                    x = re.findall(r';', txt_line)
                    if len(x) >1:
                        txt_line = txt_line.replace('—', '-')
                        line = txt_line.strip().split(';', 2)
                        if not line[0].strip() == '':
                            self.list_stcd.append([line[0].strip(), line[1].strip(), line[2].strip()])
                    txt_line = stcd_txt_file.readline()
            try:
                os.remove(self.stcd_txt_path)
            except Exception as error1:
                return
            with open(self.stcd_txt_path, 'a') as stcd_txt_file:
                stcd_txt_file.write('站点编码;RTU站点;站点名称\n')
                for line in self.list_stcd:
                    stcd_txt_file.write(line[0] + ';' + line[1] + ';' + line[2] + '\n')
        except Exception as error:
            return
    #初始化列表
    def initTable(self):
        x = 0
        for line in self.list_stcd:
            self.stcd_tablewidget.insertRow(x)
            self.stcd_tablewidget.setItem(x, 0, QTableWidgetItem(line[0]))
            self.stcd_tablewidget.setItem(x, 1, QTableWidgetItem(line[1]))
            self.stcd_tablewidget.setItem(x, 2, QTableWidgetItem(line[2]))
            x = x + 1
    #表格被选择
    def cell_clicked(self, x, y):
        if self.table_clicked_old == x:
            return
        self.table_clicked_old = x
        self.stcd_stcd.setText(self.stcd_tablewidget.item(x, 0).text())
        self.stcd_rtu.setText(self.stcd_tablewidget.item(x, 1).text())
        self.stcd_name.setText(self.stcd_tablewidget.item(x, 2).text())
    #保存按钮
    def save_stcd_info(self):
        if self.stcd_stcd.text().strip() == '' or self.stcd_rtu.text().strip() == '' or self.stcd_name.text().strip() == '':
            QMessageBox.warning(self,'注意','站点编码,RTU编码,站点名称\n不能为空.')
            return
        x = 0
        while x < len(self.list_stcd):
            if self.stcd_stcd.text().strip() == self.list_stcd[x][0]:
                flag = 0
                if self.stcd_rtu.text().strip() != self.list_stcd[x][1]:
                    del self.list_stcd[x][1]
                    self.list_stcd[x].insert(1,self.stcd_rtu.text().strip())
                    self.stcd_tablewidget.setItem(x,1,QTableWidgetItem(self.stcd_rtu.text().strip()))
                    flag = 1
                if self.stcd_name.text().strip() != self.list_stcd[x][2]:
                    del self.list_stcd[x][2]
                    self.list_stcd[x].insert(2, self.stcd_name.text().strip())
                    self.stcd_tablewidget.setItem(x, 2, QTableWidgetItem(self.stcd_name.text().strip()))
                    flag = 2
                if flag > 0:
                    os.remove(self.stcd_txt_path)
                    with open(self.stcd_txt_path, 'a') as r_file:
                        r_file.write('站点编码;RTU站点;站点名称\n')
                        for line in self.list_stcd:
                            r_file.write(line[0] + ';' + line[1] + ';' + line[2] + '\n')
                        r_file.flush()
                return
            x = x + 1
        self.stcd_tablewidget.insertRow(len(self.list_stcd))
        self.stcd_tablewidget.setItem(len(self.list_stcd), 0, QTableWidgetItem(self.stcd_stcd.text().strip()))
        self.stcd_tablewidget.setItem(len(self.list_stcd), 1, QTableWidgetItem(self.stcd_rtu.text().strip()))
        self.stcd_tablewidget.setItem(len(self.list_stcd), 2, QTableWidgetItem(self.stcd_name.text().strip()))
        self.list_stcd.append([self.stcd_stcd.text().strip(), self.stcd_rtu.text().strip(), self.stcd_name.text().strip()])
        with open(self.stcd_txt_path,'a') as file:
            file.write(self.stcd_stcd.text().strip() + ';' + self.stcd_rtu.text().strip() + ';' + self.stcd_name.text().strip() + '\n')
        self.table_clicked_old = -1
    #删除按钮
    def delete_stcd_info(self):
        if self.table_clicked_old == -1:
            QMessageBox.warning(self,'提示','请先选择要删除的站点,再删除.')
            return
        self.stcd_tablewidget.removeRow(self.table_clicked_old)
        del self.list_stcd[self.table_clicked_old]
        os.remove(self.stcd_txt_path)
        with open(self.stcd_txt_path, 'a') as de_file:
            de_file.write('站点编码;RTU站点;站点名称\n')
            for line in self.list_stcd:
                de_file.write(line[0] + ';' + line[1] + ';' + line[2] + '\n')
            de_file.flush()
        self.stcd_stcd.setText('')
        self.stcd_rtu.setText('')
        self.stcd_name.setText('')
        self.table_clicked_old = -1

新闻标题:pyqt5_站点管理_stcd_windows
标题来源:http://cqcxhl.cn/article/psddpd.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP