PyQt5 - приложение 'не отвечает', при запуске иного процесса из другого файла
часть кода:
class AutoAdditionProfilesWindow(QWidget):
def __init__(self, main_window=None):
super().__init__()
self.setWindowTitle("Create Profile")
self.setGeometry(200, 200, 500, 100)
self.layout = QFormLayout()
self.setLayout(self.layout)
self.cookies_input = QPushButton("Choose Files")
self.cookies_input.clicked.connect(self.choose_cookies_files)
self.layout.addRow(QLabel("Choose Files:"), self.cookies_input)
self.save_button = QPushButton("start")
self.save_button.clicked.connect(self.start)
self.layout.addWidget(self.save_button)
self.file_path = "./files/profiles_info.json"
def choose_cookies_files(self):
options = QFileDialog.Options()
self.files, _ = QFileDialog.getOpenFileNames(self, "Select Cookies Files", "", "Text Files (*.txt);;All Files (*)", options=options)
if self.files:
self.cookies_input.setText(f"{len(self.files)}")
def start(self):
##=============================================##
##=============================================##
from find_auth_keys import find_keys
self.list_of_tokens = ["a375a9d14d00431052b4d668d7501d7fa243fce3"]
text = QLabel("finding keys.")
self.layout.addWidget(text)
self.progress = QProgressBar()
self.progress.setVisible(True)
self.progress.setMaximum(len(self.files))
self.layout.addWidget(self.progress)
self.progress.setValue(self.progress.value() + 1)
for index, path in enumerate(self.files):
self.progress.setValue(self.progress.value() + 1)
keys = find_keys(path, self)
for key in keys:
self.list_of_tokens.append(key)
##=============================================##
##=============================================##
text.setText("checking profiles.")
self.layout.removeWidget(self.progress)
self.progress.deleteLater()
self.progress = QProgressBar()
self.progress.setVisible(True)
self.progress.setMaximum(len(self.list_of_tokens))
self.layout.addWidget(self.progress)
self.progress.setValue(self.progress.value() + 1)
chunk_size = 6
profiles_queue = [self.list_of_tokens[i:i + chunk_size] for i in range(0, len(self.list_of_tokens), chunk_size)]
try:
for profile in profiles_queue:
from main_module import Application
Application([], {}).check_cookies([profile], self)
except Exception as e:
print(e)
print(self.list_of_tokens)
##=============================================##
##=============================================##
text.setText("adding profiles.")
self.layout.removeWidget(self.progress)
self.progress.deleteLater()
self.progress = QProgressBar()
self.progress.setVisible(True)
self.progress.setMaximum(len(self.list_of_tokens))
self.layout.addWidget(self.progress)
self.progress.setValue(self.progress.value() + 1)
for key in self.list_of_tokens:
unique_code = self.generate_unique_code()
profile_key = f"profile_{unique_code}"
profile_data = {
"name": profile_key,
"cookies": {"name": "auth_token", "value": f"{key}", "path": "/", "domain": ".x.com"},
"proxy": "",
"notes": "",
"tags": ""
}
with open(self.file_path, 'r') as file:
data = json.load(file)
data[profile_key] = profile_data
with open(self.file_path, 'w', encoding="utf-8") as file:
json.dump(data, file, indent=4)
self.progress.setValue(self.progress.value() + 1)
def generate_unique_code(self):
return str(uuid.uuid4())
как сказал ранее, программа "не отвечает", (как я понимаю) когда запускается процесс с другого файла. А мне важно, как минимум, сделать прогрес бар.
пробовал оптимизировать - не помогло
Ответы (1 шт):
Автор решения: Duxtaa
→ Ссылка
Решение нашлось и очень даже простое. Спасибо Justiks.
добавил QtWidgets.qApp.processEvents()
в for
-циклы, и проблема пропала.
пример:
for profile in profiles_queue:
from main_module import Application
Application([], {}).check_cookies([profile], self)
self.progress.setValue(self.progress.value() + 1)
QtWidgets.qApp.processEvents()
импорт:
from PyQt5 import QtWidgets
ещё раз спасибо автору Justiks!☺️