我有一个正在运行的应用程序,它将获取一个CSV文件并取出所有电子邮件地址并将其显示在QTableWidget上。不幸的是,它只能做一次。如果我返回选择另一个CSV文件,则电子邮件TableWidget将不会更新,并且参数将转移到EmailList。这也是为什么我不能在EmailList的constructer方法中使用init_ui的原因。我想知道如何使用新文件输入重复更新显示。这是一个适合自己运行的CSV文件。 http://www.sharecsv.com/s/ed7c7a2154478339603a192921a352eb/csv_test.csv
email_list.py
import sys
import os
import re
import io
from construct import ConstructMessage
from PyQt5.QtWidgets import QFileDialog, QTableWidget,QTableWidgetItem, QVBoxLayout,QHBoxLayout, QWidget, QAction, QPushButton
from PyQt5.QtCore import Qt, pyqtSignal, QFileInfo, QDir
class EmailList(QWidget):
buttonClicked = pyqtSignal()
email_list = []
def __init__(self):
super().__init__()
print("Initzd")
#self.init_ui()
def init_ui(self):
self.v_layout = QVBoxLayout()
self.h_layout = QHBoxLayout()
self.email_widget = QTableWidget()
self.email_widget.setColumnCount(1)
for row_data in EmailList.email_list:
print("Email list: " + row_data)
row = self.email_widget.rowCount()
self.email_widget.insertRow(row)
for column, col in enumerate(row_data):
item = QTableWidgetItem(row_data)
self.email_widget.setItem(row, column, item)
width = 200
self.open_button = QPushButton("Open")
self.open_button.setMaximumWidth(width)
self.continue_button = QPushButton("Continue")
self.continue_button.setMaximumWidth(width)
self.back_button = QPushButton("Go back")
self.back_button.setMaximumWidth(width)
self.open_button.clicked.connect(lambda: self.open_click(self.open_button,"Open",self.widget))
self.continue_button.clicked.connect(lambda: self.continue_click(self.continue_button,"Continue"))
self.back_button.clicked.connect(lambda: self.on_back_clicked())
self.v_layout.addWidget(self.email_widget)
self.h_layout.addWidget(self.open_button)
self.h_layout.addWidget(self.continue_button)
self.h_layout.addWidget(self.back_button)
self.v_layout.addLayout(self.h_layout)
self.setLayout(self.v_layout)
self.setWindowTitle("Email List")
print("Welcome to the email_list widget")
self.show()
def on_back_clicked(self):
print("You are going back")
self.buttonClicked.emit()
def update_emails(self, imported_email_list):
EmailList.email_list = imported_email_list
def continue_click(self,continue_button,string):
pass
from sheet import Sheet
sheet.py
import sys
import os
import re
import io
import csv
from PyQt5.QtWidgets import QFileDialog, QTableWidget, QTableWidgetItem, QVBoxLayout,QHBoxLayout, QWidget, QAction, QPushButton
from PyQt5.QtCore import Qt, pyqtSignal, QFileInfo, QDir
class Sheet(QWidget):
buttonClicked = pyqtSignal()
email_list = []
def __init__(self,r,c):
super().__init__()
self.init_ui(r,c)
def init_ui(self,r,c):
self.v_layout = QVBoxLayout()
self.h_layout = QHBoxLayout()
self.sheet = QTableWidget()
self.sheet.setRowCount(r)
self.sheet.setColumnCount(c)
width = 200
self.open_button = QPushButton("Open")
self.open_button.setMaximumWidth(width)
self.continue_button = QPushButton("Continue")
self.continue_button.setMaximumWidth(width)
self.file_path = None
self.open_button.clicked.connect(lambda: self.open_click(self.open_button,"Open",self.sheet))
self.continue_button.clicked.connect(lambda: self.continue_click(self.continue_button,"Continue"))
self.v_layout.addWidget(self.sheet)
self.h_layout.addWidget(self.open_button)
self.h_layout.addWidget(self.continue_button)
self.v_layout.addLayout(self.h_layout)
self.setLayout(self.v_layout)
self.setWindowTitle("CSV Reader")
self.show()
def continue_click(self,continue_button,string):
self.buttonClicked.emit()
def open_click(self,open_button,string,sheet):
self.path = QFileDialog.getOpenFileName(self, 'Open CSV', os.getenv('HOME'), 'CSV(*.csv)')
if self.path[0] != '':
with open(self.path[0], 'rU') as csv_file:
self.sheet.setRowCount(0)
my_file = csv.reader(csv_file, delimiter=',', quotechar='|')
for row_data in my_file:
row = self.sheet.rowCount()
self.sheet.insertRow(row)
self.sheet.setColumnCount(len(row_data))
for column, col in enumerate(row_data):
item = QTableWidgetItem(col)
self.sheet.setItem(row, column, item)
self.file_path = self.path[0]
Sheet.email_list = re.findall(r'[\w_]+@[\w_]+\.[\w_]+', open(self.file_path).read())
EmailList.update_emails(self, Sheet.email_list)
from email_list import EmailList
最后是main.py
import sys
import os
import io
from sheet import Sheet
from email_list import EmailList
from PyQt5.QtWidgets import QApplication, QMainWindow, QStackedWidget
from PyQt5.QtCore import Qt
class Main(QMainWindow):
def __init__(self):
super().__init__()
self.central_widget = QStackedWidget()
self.setCentralWidget(self.central_widget)
self.sheet_widget = Sheet(10,10)
self.email_list_widget = EmailList()
self.central_widget.addWidget(self.sheet_widget)
self.central_widget.addWidget(self.email_list_widget)
self.central_widget.setCurrentWidget(self.sheet_widget)
self.sheet_widget.buttonClicked.connect(lambda: self.central_widget.setCurrentWidget(self.email_list_widget))
self.sheet_widget.buttonClicked.connect(self.email_list_widget.init_ui)
self.email_list_widget.buttonClicked.connect(lambda: self.central_widget.setCurrentWidget(self.sheet_widget))
app = QApplication(sys.argv)
run = Main()
run.resize(500,500)
run.show()
sys.exit(app.exec_())
任何有关通过模块从小部件无缝更新UI的帮助或见解都是超级的!我在想一个刷新方法,但email_list已经更新了。用户界面应该自行更新,但事实并非如此,我错过了一些东西。