I’m trying to create multiple GUI elements to show the transcript of a video, the issue is that it freezes when creating the GUI as it creates all the GUI such as, QTextEdit, QLineEdit, QPushButtons and QGraphicsTextItems. It is required to create all those GUI elements so that the user can edit and change the text, at any time within the video.
I’ve tried, putting the creation of the GUI elements within a QThread, then sending the created GUI elements to main thread and I tried putting the SRT file reader function on a thread but both options didn’t work, due to GUI elements not being allowed to be created / managed within a thread and the srt file function not being the reason why its freezing the application:
import sys
from PySide6.QtCore import *
from PySide6.QtWidgets import *
class CreatePreviewText(QObject):
text_data_updated = Signal(list)
def __init__(self, scroll_layout):
super().__init__()
self.text_data = []
self.previous_text = {}
self.scroll_layout = scroll_layout
# SPLITS THE SRT TO, START / END TIMES AND TEXT
def load_srt_file(self, file_path):
try:
with open(file_path, 'r') as file:
srt_lines = file.readlines()
transcripts = []
for line in srt_lines:
if '-->' in line:
start, end = line.strip().split(' --> ')
transcripts.append({'start': start, 'end': end, 'text': ''})
else:
line = line.strip()
if line and transcripts:
transcripts[-1]['text'] += line + 'n'
file.close()
self.create_transcript_widgets(transcripts)
except Exception as e:
print("Error Writing Transcribe To Widget:", e)
# CREATES THE, BUTTONS WIDGET, LINE EDIT WIDGETS, TEXT EDIT WIDGETS
def create_transcript_widgets(self, transcripts):
text_and_duration = []
for transcript in transcripts:
start = transcript['start']
end = transcript['end']
text = transcript['text'].strip()
add_button = QPushButton()
add_button.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
remove_button = QPushButton()
remove_button.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.Maximum)
new_button_layout = QHBoxLayout()
new_button_layout.addWidget(add_button)
new_button_layout.addWidget(remove_button)
new_button_layout.addStretch(1)
new_duration_line = QLineEdit()
new_duration_line.setStyleSheet("border: none;")
new_duration_line.setReadOnly(False)
new_duration_line.setInputMask("99:99:99,999 --> 99:99:99,999")
new_text_edit = QTextEdit()
new_text_edit.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
new_text_edit.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
new_text_edit.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
new_text_edit.setText(text)
duration_str = f"{start} --> {end}"
new_duration_line.setText(duration_str)
start_total_milliseconds, end_total_milliseconds = self.convert_to_ms(start, end)
self.scroll_layout.addLayout(new_button_layout)
self.scroll_layout.addWidget(new_duration_line)
self.scroll_layout.addWidget(new_text_edit)
text_and_duration.append([new_duration_line, new_text_edit, start_total_milliseconds, end_total_milliseconds, new_button_layout])
self.create_preview_text(text_and_duration)
# CREATES THE QGRAPHICSTEXTITEM FOR MY ACTUAL APP TO SHOW
def create_preview_text(self, text_and_duration):
try:
for duration_line, text_edit, start_total_milliseconds, end_total_milliseconds, button_layout in text_and_duration:
text_preview = QGraphicsTextItem()
self.text_data.append([text_preview, duration_line, text_edit, start_total_milliseconds, end_total_milliseconds, button_layout])
text_preview.hide()
self.text_data_updated.emit(self.text_data)
except Exception as e:
print("Error creating: ", e)
def convert_to_ms(self, start_time, end_time):
start_parts = start_time.split(':')
start_hours = int(start_parts[0])
start_minutes = int(start_parts[1])
start_seconds, start_milliseconds = map(int, start_parts[2].split(','))
start_total_milliseconds = (start_hours * 3600 + start_minutes * 60 + start_seconds) * 1000 + start_milliseconds
end_parts = end_time.split(':')
end_hours = int(end_parts[0])
end_minutes = int(end_parts[1])
end_seconds, end_milliseconds = map(int, end_parts[2].split(','))
end_total_milliseconds = (end_hours * 3600 + end_minutes * 60 + end_seconds) * 1000 + end_milliseconds
return start_total_milliseconds, end_total_milliseconds
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('SRT Preview Tool')
self.setGeometry(100, 100, 800, 600)
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
self.layout = QVBoxLayout()
self.central_widget.setLayout(self.layout)
# Setup scroll layout
scroll_container = QWidget()
scroll_container_layout = QVBoxLayout(scroll_container)
scroll_area = QScrollArea()
scroll_area.setWidgetResizable(True)
scroll_container_layout.addWidget(scroll_area)
scroll_content = QWidget(scroll_area)
scroll_area.setWidget(scroll_content)
self.scroll_layout = QVBoxLayout(scroll_content)
self.layout.addWidget(scroll_container)
# Setup buttons
self.load_button = QPushButton('Load SRT File')
self.load_button.clicked.connect(self.load_srt_file)
self.layout.addWidget(self.load_button)
# Setup CreatePreviewText
self.create_preview_text = CreatePreviewText(self.scroll_layout)
def load_srt_file(self):
file_path, _ = QFileDialog.getOpenFileName(self, 'Open SRT File', '', 'SRT Files (*.srt)')
if file_path:
self.create_preview_text.load_srt_file(file_path)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
1