Всплывающее окно в дополнительном потоке
Сделал лаунчер для ТЛ. Написан на PyQt5.
Разрабатываю если ТЛ не установлен, выдаю всплывающее окно,
а т.к. у меня включен поток threading либо ошибка, либо белый экран ошибки и все виснет.
Помогите исправить.
import unit
from PyQt5 import QtWidgets
import time
import mouse
import os
from PyQt5.QtWidgets import QMessageBox
from PyQt5 import QtGui
import threading
class Window(QtWidgets.QMainWindow, unit.Ui_MainWindow):
def __init__(self):
super(Window, self).__init__()
self.setupUi(self)
self.setWindowIcon(QtGui.QIcon('icon.jpg'))
self.pushButton.clicked.connect(self.start_thread)
def go(self):
try:
ff = os.getcwd()
ff += '\\TLauncher.exe'
os.startfile(ff)
time.sleep(20)
mouse.move(1102, 827)
time.sleep(1)
mouse.click('left')
except:
error = QMessageBox()
error.setWindowTitle("Установите TL!")
error.setText("Установите TLauncher. Без него майнкрафт не будет запускаться!")
error.setIcon(QMessageBox.Information)
error.exec()
def start_thread(self):
thread = threading.Thread(target=self.go, args=())
thread.start()
App = QtWidgets.QApplication([])
window = Window()
window.show()
App.exec()
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Нельзя взаимодействовать с виджетами в дополнительных потоках.
Я предложу вам попробовать использовать класс QProcess.
Класс QProcess используется для запуска внешних программ и взаимодействия с ними.
exePath = "C:/Windows/system32/calc.exe"
exePath = "C:/Windows/system32/calc123.exe"
import sys
import os
import time
# ??? import mouse
import threading
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.Qt import *
# ??? import unit # from unit import Ui_MainWindow
class Window(QtWidgets.QMainWindow): # ??? , unit.Ui_MainWindow):
def __init__(self):
super(Window, self).__init__()
# ??? self.setupUi(self)
self.centralwidget = QtWidgets.QWidget()
self.setCentralWidget(self.centralwidget)
self.pushButton = QtWidgets.QPushButton('Start', self.centralwidget)
self.pushButton.clicked.connect(self.start_thread)
self.layout = QtWidgets.QVBoxLayout(self.centralwidget)
self.layout.addWidget(self.pushButton, alignment = Qt.AlignBottom)
def start_thread(self):
try:
# exePath = "C:/Windows/system32/calc.exe"
# нет такого exe`ка ------------> vvv
exePath = "C:/Windows/system32/calc123.exe"
ret = QProcess.startDetached(exePath)
if not ret:
raise
except Exception as e:
error = QMessageBox()
error.setWindowTitle("Установите TL!")
error.setText(
"Установите TLauncher. Без него майнкрафт не будет запускаться!"
)
error.setIcon(QMessageBox.Information)
error.exec()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.setWindowIcon(QtGui.QIcon('icon.jpg'))
window.resize(640, 480)
window.show()
sys.exit(app.exec_())

