您好,登錄后才能下訂單哦!
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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。