Last active
February 26, 2024 17:58
-
-
Save Pymmdrza/f48aec44b51384801b12c292805a165f to your computer and use it in GitHub Desktop.
readfile class in pyqt5 with QThread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PyQt5.QtCore import QThread, pyqtSignal | |
class FileReader(QThread): | |
data_read = pyqtSignal(str) | |
def __init__(self, filename): | |
super().__init__() | |
self.filename = filename | |
def run(self): | |
with open(self.filename, 'r', encoding='utf-8') as file: | |
for line in file: | |
self.data_read.emit(line.strip()) | |
# use in another class : | |
# toolButton.clicked.connect(self.OnOpenFile) | |
class Main_Board_App(QMainWindow): | |
def __init__(self): | |
super(Main_Board_App, self).__init__() | |
# reader from connect toolbutton | |
def OnOpenFile(self): | |
fileDial = QFileDialog(self) | |
filePath, _ = fileDial.getOpenFileName(self, "Select Target Text File", "", "Text Files (*.txt)") | |
if filePath: | |
self.Line_Address_File.setText(filePath) | |
self.Label_Massage.setText("Successfully Select File") | |
self.index_openfile = True | |
self.reader = FileReader(filePath) | |
self.reader.data_read.connect(self.OnRead) | |
self.reader.start() | |
else: | |
return None | |
def OnRead(self, line): | |
self.z += 1 | |
self.Log(f"ADDR : {line}") | |
self.targetList.append(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment