質問するログイン新規登録

回答編集履歴

1

修正

2021/01/11 02:37

投稿

退会済みユーザー
answer CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  import sys #システムモジュール
8
8
  from PyQt5 import QtWidgets,QtCore
9
- from PyQt5.QtCore import QThread, QTimer, QEventLoop # 追加
9
+ from PyQt5.QtCore import QObject, QEventLoop, QThread, QTimer, pyqtSignal # 追加
10
10
  from PyQt5.QtWidgets import QMainWindow
11
11
  from PyQt5.QtWidgets import QMessageBox
12
12
  from AK003_V1001ui_test import Ui_MainWindow
@@ -14,14 +14,26 @@
14
14
  # import threading #delete
15
15
  import pyautogui as pg #全てのwindowを閉じるよう
16
16
 
17
+
18
+ # バックグラウンドで処理を行うためのクラス
19
+ class Worker(QObject):
20
+ start = pyqtSignal()
21
+ done = pyqtSignal()
22
+
23
+ def run(self):
24
+ self.start.emit()
25
+ self.done.emit()
26
+
27
+
17
28
  class AK003(QMainWindow, Ui_MainWindow):
18
29
  def __init__(self,parent=None):
19
30
  super(AK003, self).__init__(parent)
20
- # 下記行追加
31
+ # 下記5行追加
21
32
  self.thread = QThread()
33
+ self.worker = Worker()
22
- self.thread.started.connect(self.minimize)
34
+ self.worker.start.connect(self.minimize)
23
- self.thread.finished.connect(self.thread.quit)
35
+ self.worker.done.connect(self.thread.quit)
24
- self.moveToThread(self.thread)
36
+ self.thread.started.connect(self.worker.run)
25
37
 
26
38
  self.ui = Ui_MainWindow()
27
39
  self.ui.setupUi(self)
@@ -39,7 +51,8 @@
39
51
  msg_ret = QMessageBox.question(None,"確認","デスクトップ画面を表示しますがよろしいですか?", QMessageBox.Ok,QMessageBox.Cancel )
40
52
  if(msg_ret != QMessageBox.Cancel ):
41
53
  # デスクトップ表示スレッドの開始
54
+ self.worker.moveToThread(self.thread)
42
- self.thread.start()
55
+ self.thread.start()
43
56
  else:
44
57
  pass
45
58