In PyQt5, new parent is in a different Qthread after pop up QMessageBox









up vote
0
down vote

favorite












This is PyQt5 code. I want to count down 5 seconds and update every 1 second. Then application will pop up QMessageBox.
But it will closed after click button on QMessageBox due to QObject::setParent: Cannot set parent, new parent is in a different thread,
detail code below:



from PyQt5 import QtCore, QtGui, QtWidgets
import sys, time
from _thread import *

class ThreadClass(QtCore.QThread):
# Create the signal
sig = QtCore.pyqtSignal(int)

def __init__(self, mw, parent=None):
self.mw = mw
self.mbox = QtWidgets.QMessageBox()
super().__init__(parent)
self.sig.connect(self.showtime)

def showtime(self, t):
self.mw.label.setText(str(t))

def run(self):
for t in range(5):
self.sig.emit(t)
time.sleep(1)
self.mbox.about(QtWidgets.QMainWindow(), "Title", "Finished")

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(253, 181)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(90, 100, 75, 23))
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(20, 20, 211, 16))
self.label.setObjectName("label")
self.tc = ThreadClass(self)
self.pushButton.clicked.connect(lambda: self.tc.start())
#self.pushButton.clicked.connect(lambda: start_new_thread(showtime, (self.label, )))
MainWindow.setCentralWidget(self.centralwidget)


QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton.setText("Show")
self.label.setText("Time")

if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ex = Ui_MainWindow()
w = QtWidgets.QMainWindow()
ex.setupUi(w)
w.show()
sys.exit(app.exec_())


App will close after clicked button on QMessageBox and error message below.



QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
QObject::setParent: Cannot set parent, new parent is in a different thread
QObject::setParent: Cannot set parent, new parent is in a different thread









share|improve this question









New contributor




movep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.























    up vote
    0
    down vote

    favorite












    This is PyQt5 code. I want to count down 5 seconds and update every 1 second. Then application will pop up QMessageBox.
    But it will closed after click button on QMessageBox due to QObject::setParent: Cannot set parent, new parent is in a different thread,
    detail code below:



    from PyQt5 import QtCore, QtGui, QtWidgets
    import sys, time
    from _thread import *

    class ThreadClass(QtCore.QThread):
    # Create the signal
    sig = QtCore.pyqtSignal(int)

    def __init__(self, mw, parent=None):
    self.mw = mw
    self.mbox = QtWidgets.QMessageBox()
    super().__init__(parent)
    self.sig.connect(self.showtime)

    def showtime(self, t):
    self.mw.label.setText(str(t))

    def run(self):
    for t in range(5):
    self.sig.emit(t)
    time.sleep(1)
    self.mbox.about(QtWidgets.QMainWindow(), "Title", "Finished")

    class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(253, 181)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.pushButton = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton.setGeometry(QtCore.QRect(90, 100, 75, 23))
    self.pushButton.setObjectName("pushButton")
    self.label = QtWidgets.QLabel(self.centralwidget)
    self.label.setGeometry(QtCore.QRect(20, 20, 211, 16))
    self.label.setObjectName("label")
    self.tc = ThreadClass(self)
    self.pushButton.clicked.connect(lambda: self.tc.start())
    #self.pushButton.clicked.connect(lambda: start_new_thread(showtime, (self.label, )))
    MainWindow.setCentralWidget(self.centralwidget)


    QtCore.QMetaObject.connectSlotsByName(MainWindow)
    self.pushButton.setText("Show")
    self.label.setText("Time")

    if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = Ui_MainWindow()
    w = QtWidgets.QMainWindow()
    ex.setupUi(w)
    w.show()
    sys.exit(app.exec_())


    App will close after clicked button on QMessageBox and error message below.



    QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
    QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
    QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
    QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
    QObject::setParent: Cannot set parent, new parent is in a different thread
    QObject::setParent: Cannot set parent, new parent is in a different thread









    share|improve this question









    New contributor




    movep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      This is PyQt5 code. I want to count down 5 seconds and update every 1 second. Then application will pop up QMessageBox.
      But it will closed after click button on QMessageBox due to QObject::setParent: Cannot set parent, new parent is in a different thread,
      detail code below:



      from PyQt5 import QtCore, QtGui, QtWidgets
      import sys, time
      from _thread import *

      class ThreadClass(QtCore.QThread):
      # Create the signal
      sig = QtCore.pyqtSignal(int)

      def __init__(self, mw, parent=None):
      self.mw = mw
      self.mbox = QtWidgets.QMessageBox()
      super().__init__(parent)
      self.sig.connect(self.showtime)

      def showtime(self, t):
      self.mw.label.setText(str(t))

      def run(self):
      for t in range(5):
      self.sig.emit(t)
      time.sleep(1)
      self.mbox.about(QtWidgets.QMainWindow(), "Title", "Finished")

      class Ui_MainWindow(object):
      def setupUi(self, MainWindow):
      MainWindow.setObjectName("MainWindow")
      MainWindow.resize(253, 181)
      self.centralwidget = QtWidgets.QWidget(MainWindow)
      self.centralwidget.setObjectName("centralwidget")
      self.pushButton = QtWidgets.QPushButton(self.centralwidget)
      self.pushButton.setGeometry(QtCore.QRect(90, 100, 75, 23))
      self.pushButton.setObjectName("pushButton")
      self.label = QtWidgets.QLabel(self.centralwidget)
      self.label.setGeometry(QtCore.QRect(20, 20, 211, 16))
      self.label.setObjectName("label")
      self.tc = ThreadClass(self)
      self.pushButton.clicked.connect(lambda: self.tc.start())
      #self.pushButton.clicked.connect(lambda: start_new_thread(showtime, (self.label, )))
      MainWindow.setCentralWidget(self.centralwidget)


      QtCore.QMetaObject.connectSlotsByName(MainWindow)
      self.pushButton.setText("Show")
      self.label.setText("Time")

      if __name__ == '__main__':
      app = QtWidgets.QApplication(sys.argv)
      ex = Ui_MainWindow()
      w = QtWidgets.QMainWindow()
      ex.setupUi(w)
      w.show()
      sys.exit(app.exec_())


      App will close after clicked button on QMessageBox and error message below.



      QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
      QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
      QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
      QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
      QObject::setParent: Cannot set parent, new parent is in a different thread
      QObject::setParent: Cannot set parent, new parent is in a different thread









      share|improve this question









      New contributor




      movep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      This is PyQt5 code. I want to count down 5 seconds and update every 1 second. Then application will pop up QMessageBox.
      But it will closed after click button on QMessageBox due to QObject::setParent: Cannot set parent, new parent is in a different thread,
      detail code below:



      from PyQt5 import QtCore, QtGui, QtWidgets
      import sys, time
      from _thread import *

      class ThreadClass(QtCore.QThread):
      # Create the signal
      sig = QtCore.pyqtSignal(int)

      def __init__(self, mw, parent=None):
      self.mw = mw
      self.mbox = QtWidgets.QMessageBox()
      super().__init__(parent)
      self.sig.connect(self.showtime)

      def showtime(self, t):
      self.mw.label.setText(str(t))

      def run(self):
      for t in range(5):
      self.sig.emit(t)
      time.sleep(1)
      self.mbox.about(QtWidgets.QMainWindow(), "Title", "Finished")

      class Ui_MainWindow(object):
      def setupUi(self, MainWindow):
      MainWindow.setObjectName("MainWindow")
      MainWindow.resize(253, 181)
      self.centralwidget = QtWidgets.QWidget(MainWindow)
      self.centralwidget.setObjectName("centralwidget")
      self.pushButton = QtWidgets.QPushButton(self.centralwidget)
      self.pushButton.setGeometry(QtCore.QRect(90, 100, 75, 23))
      self.pushButton.setObjectName("pushButton")
      self.label = QtWidgets.QLabel(self.centralwidget)
      self.label.setGeometry(QtCore.QRect(20, 20, 211, 16))
      self.label.setObjectName("label")
      self.tc = ThreadClass(self)
      self.pushButton.clicked.connect(lambda: self.tc.start())
      #self.pushButton.clicked.connect(lambda: start_new_thread(showtime, (self.label, )))
      MainWindow.setCentralWidget(self.centralwidget)


      QtCore.QMetaObject.connectSlotsByName(MainWindow)
      self.pushButton.setText("Show")
      self.label.setText("Time")

      if __name__ == '__main__':
      app = QtWidgets.QApplication(sys.argv)
      ex = Ui_MainWindow()
      w = QtWidgets.QMainWindow()
      ex.setupUi(w)
      w.show()
      sys.exit(app.exec_())


      App will close after clicked button on QMessageBox and error message below.



      QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
      QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
      QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
      QBasicTimer::stop: Failed. Possibly trying to stop from a different thread
      QObject::setParent: Cannot set parent, new parent is in a different thread
      QObject::setParent: Cannot set parent, new parent is in a different thread






      python python-3.x pyqt5 qthread qmessagebox






      share|improve this question









      New contributor




      movep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      movep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Nov 10 at 15:04









      Raoslaw Szamszur

      60114




      60114






      New contributor




      movep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 10 at 11:55









      movep

      63




      63




      New contributor




      movep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      movep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      movep is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Try it:



          import sys #, time
          from PyQt5 import QtCore, QtGui, QtWidgets
          from _thread import *

          class ThreadClass(QtCore.QThread):
          # Create the signal
          sig = QtCore.pyqtSignal(int)
          finish = QtCore.pyqtSignal() # +++

          def __init__(self, mw, parent=None):
          # self.mw = mw
          # self.mbox = QtWidgets.QMessageBox()
          super().__init__(parent)
          # self.sig.connect(self.showtime)

          # def showtime(self, t):
          # self.mw.label.setText(str(t))

          def run(self):
          for t in range(5):
          self.sig.emit(t)
          #time.sleep(1)
          QtCore.QThread.msleep(1000)

          self.finish.emit() # +++
          # self.mbox.about(QtWidgets.QMainWindow(), "Title", "Finished")

          class Ui_MainWindow(object):
          def setupUi(self, MainWindow):
          MainWindow.setObjectName("MainWindow")
          MainWindow.resize(253, 181)
          self.centralwidget = QtWidgets.QWidget(MainWindow)
          self.centralwidget.setObjectName("centralwidget")

          self.pushButton = QtWidgets.QPushButton(self.centralwidget)
          self.pushButton.setGeometry(QtCore.QRect(90, 100, 75, 23))
          self.pushButton.setObjectName("pushButton")

          self.label = QtWidgets.QLabel(self.centralwidget)
          self.label.setGeometry(QtCore.QRect(20, 20, 211, 16))
          self.label.setObjectName("label")

          self.tc = ThreadClass(self)
          self.pushButton.clicked.connect(lambda: self.tc.start())
          #self.pushButton.clicked.connect(lambda: start_new_thread(showtime, (self.label, )))

          self.tc.sig.connect(self.showtime) # +++
          self.tc.finish.connect(self.finishTime) # +++


          MainWindow.setCentralWidget(self.centralwidget)


          QtCore.QMetaObject.connectSlotsByName(MainWindow)
          self.pushButton.setText("Show")
          self.label.setText("Time")

          # +++
          def showtime(self, t):
          self.label.setText(str(t))

          # +++
          def finishTime(self):
          self.mbox = QtWidgets.QMessageBox()
          self.mbox.about(QtWidgets.QMainWindow(), "Title", "Finished")


          if __name__ == '__main__':
          app = QtWidgets.QApplication(sys.argv)
          w = QtWidgets.QMainWindow()
          ex = Ui_MainWindow()
          ex.setupUi(w)
          w.show()
          sys.exit(app.exec_())


          enter image description here






          share|improve this answer




















          • Adding some explanation would make this answer more useful.
            – mx0
            Nov 10 at 15:24










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          movep is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238682%2fin-pyqt5-new-parent-is-in-a-different-qthread-after-pop-up-qmessagebox%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          Try it:



          import sys #, time
          from PyQt5 import QtCore, QtGui, QtWidgets
          from _thread import *

          class ThreadClass(QtCore.QThread):
          # Create the signal
          sig = QtCore.pyqtSignal(int)
          finish = QtCore.pyqtSignal() # +++

          def __init__(self, mw, parent=None):
          # self.mw = mw
          # self.mbox = QtWidgets.QMessageBox()
          super().__init__(parent)
          # self.sig.connect(self.showtime)

          # def showtime(self, t):
          # self.mw.label.setText(str(t))

          def run(self):
          for t in range(5):
          self.sig.emit(t)
          #time.sleep(1)
          QtCore.QThread.msleep(1000)

          self.finish.emit() # +++
          # self.mbox.about(QtWidgets.QMainWindow(), "Title", "Finished")

          class Ui_MainWindow(object):
          def setupUi(self, MainWindow):
          MainWindow.setObjectName("MainWindow")
          MainWindow.resize(253, 181)
          self.centralwidget = QtWidgets.QWidget(MainWindow)
          self.centralwidget.setObjectName("centralwidget")

          self.pushButton = QtWidgets.QPushButton(self.centralwidget)
          self.pushButton.setGeometry(QtCore.QRect(90, 100, 75, 23))
          self.pushButton.setObjectName("pushButton")

          self.label = QtWidgets.QLabel(self.centralwidget)
          self.label.setGeometry(QtCore.QRect(20, 20, 211, 16))
          self.label.setObjectName("label")

          self.tc = ThreadClass(self)
          self.pushButton.clicked.connect(lambda: self.tc.start())
          #self.pushButton.clicked.connect(lambda: start_new_thread(showtime, (self.label, )))

          self.tc.sig.connect(self.showtime) # +++
          self.tc.finish.connect(self.finishTime) # +++


          MainWindow.setCentralWidget(self.centralwidget)


          QtCore.QMetaObject.connectSlotsByName(MainWindow)
          self.pushButton.setText("Show")
          self.label.setText("Time")

          # +++
          def showtime(self, t):
          self.label.setText(str(t))

          # +++
          def finishTime(self):
          self.mbox = QtWidgets.QMessageBox()
          self.mbox.about(QtWidgets.QMainWindow(), "Title", "Finished")


          if __name__ == '__main__':
          app = QtWidgets.QApplication(sys.argv)
          w = QtWidgets.QMainWindow()
          ex = Ui_MainWindow()
          ex.setupUi(w)
          w.show()
          sys.exit(app.exec_())


          enter image description here






          share|improve this answer




















          • Adding some explanation would make this answer more useful.
            – mx0
            Nov 10 at 15:24














          up vote
          1
          down vote



          accepted










          Try it:



          import sys #, time
          from PyQt5 import QtCore, QtGui, QtWidgets
          from _thread import *

          class ThreadClass(QtCore.QThread):
          # Create the signal
          sig = QtCore.pyqtSignal(int)
          finish = QtCore.pyqtSignal() # +++

          def __init__(self, mw, parent=None):
          # self.mw = mw
          # self.mbox = QtWidgets.QMessageBox()
          super().__init__(parent)
          # self.sig.connect(self.showtime)

          # def showtime(self, t):
          # self.mw.label.setText(str(t))

          def run(self):
          for t in range(5):
          self.sig.emit(t)
          #time.sleep(1)
          QtCore.QThread.msleep(1000)

          self.finish.emit() # +++
          # self.mbox.about(QtWidgets.QMainWindow(), "Title", "Finished")

          class Ui_MainWindow(object):
          def setupUi(self, MainWindow):
          MainWindow.setObjectName("MainWindow")
          MainWindow.resize(253, 181)
          self.centralwidget = QtWidgets.QWidget(MainWindow)
          self.centralwidget.setObjectName("centralwidget")

          self.pushButton = QtWidgets.QPushButton(self.centralwidget)
          self.pushButton.setGeometry(QtCore.QRect(90, 100, 75, 23))
          self.pushButton.setObjectName("pushButton")

          self.label = QtWidgets.QLabel(self.centralwidget)
          self.label.setGeometry(QtCore.QRect(20, 20, 211, 16))
          self.label.setObjectName("label")

          self.tc = ThreadClass(self)
          self.pushButton.clicked.connect(lambda: self.tc.start())
          #self.pushButton.clicked.connect(lambda: start_new_thread(showtime, (self.label, )))

          self.tc.sig.connect(self.showtime) # +++
          self.tc.finish.connect(self.finishTime) # +++


          MainWindow.setCentralWidget(self.centralwidget)


          QtCore.QMetaObject.connectSlotsByName(MainWindow)
          self.pushButton.setText("Show")
          self.label.setText("Time")

          # +++
          def showtime(self, t):
          self.label.setText(str(t))

          # +++
          def finishTime(self):
          self.mbox = QtWidgets.QMessageBox()
          self.mbox.about(QtWidgets.QMainWindow(), "Title", "Finished")


          if __name__ == '__main__':
          app = QtWidgets.QApplication(sys.argv)
          w = QtWidgets.QMainWindow()
          ex = Ui_MainWindow()
          ex.setupUi(w)
          w.show()
          sys.exit(app.exec_())


          enter image description here






          share|improve this answer




















          • Adding some explanation would make this answer more useful.
            – mx0
            Nov 10 at 15:24












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Try it:



          import sys #, time
          from PyQt5 import QtCore, QtGui, QtWidgets
          from _thread import *

          class ThreadClass(QtCore.QThread):
          # Create the signal
          sig = QtCore.pyqtSignal(int)
          finish = QtCore.pyqtSignal() # +++

          def __init__(self, mw, parent=None):
          # self.mw = mw
          # self.mbox = QtWidgets.QMessageBox()
          super().__init__(parent)
          # self.sig.connect(self.showtime)

          # def showtime(self, t):
          # self.mw.label.setText(str(t))

          def run(self):
          for t in range(5):
          self.sig.emit(t)
          #time.sleep(1)
          QtCore.QThread.msleep(1000)

          self.finish.emit() # +++
          # self.mbox.about(QtWidgets.QMainWindow(), "Title", "Finished")

          class Ui_MainWindow(object):
          def setupUi(self, MainWindow):
          MainWindow.setObjectName("MainWindow")
          MainWindow.resize(253, 181)
          self.centralwidget = QtWidgets.QWidget(MainWindow)
          self.centralwidget.setObjectName("centralwidget")

          self.pushButton = QtWidgets.QPushButton(self.centralwidget)
          self.pushButton.setGeometry(QtCore.QRect(90, 100, 75, 23))
          self.pushButton.setObjectName("pushButton")

          self.label = QtWidgets.QLabel(self.centralwidget)
          self.label.setGeometry(QtCore.QRect(20, 20, 211, 16))
          self.label.setObjectName("label")

          self.tc = ThreadClass(self)
          self.pushButton.clicked.connect(lambda: self.tc.start())
          #self.pushButton.clicked.connect(lambda: start_new_thread(showtime, (self.label, )))

          self.tc.sig.connect(self.showtime) # +++
          self.tc.finish.connect(self.finishTime) # +++


          MainWindow.setCentralWidget(self.centralwidget)


          QtCore.QMetaObject.connectSlotsByName(MainWindow)
          self.pushButton.setText("Show")
          self.label.setText("Time")

          # +++
          def showtime(self, t):
          self.label.setText(str(t))

          # +++
          def finishTime(self):
          self.mbox = QtWidgets.QMessageBox()
          self.mbox.about(QtWidgets.QMainWindow(), "Title", "Finished")


          if __name__ == '__main__':
          app = QtWidgets.QApplication(sys.argv)
          w = QtWidgets.QMainWindow()
          ex = Ui_MainWindow()
          ex.setupUi(w)
          w.show()
          sys.exit(app.exec_())


          enter image description here






          share|improve this answer












          Try it:



          import sys #, time
          from PyQt5 import QtCore, QtGui, QtWidgets
          from _thread import *

          class ThreadClass(QtCore.QThread):
          # Create the signal
          sig = QtCore.pyqtSignal(int)
          finish = QtCore.pyqtSignal() # +++

          def __init__(self, mw, parent=None):
          # self.mw = mw
          # self.mbox = QtWidgets.QMessageBox()
          super().__init__(parent)
          # self.sig.connect(self.showtime)

          # def showtime(self, t):
          # self.mw.label.setText(str(t))

          def run(self):
          for t in range(5):
          self.sig.emit(t)
          #time.sleep(1)
          QtCore.QThread.msleep(1000)

          self.finish.emit() # +++
          # self.mbox.about(QtWidgets.QMainWindow(), "Title", "Finished")

          class Ui_MainWindow(object):
          def setupUi(self, MainWindow):
          MainWindow.setObjectName("MainWindow")
          MainWindow.resize(253, 181)
          self.centralwidget = QtWidgets.QWidget(MainWindow)
          self.centralwidget.setObjectName("centralwidget")

          self.pushButton = QtWidgets.QPushButton(self.centralwidget)
          self.pushButton.setGeometry(QtCore.QRect(90, 100, 75, 23))
          self.pushButton.setObjectName("pushButton")

          self.label = QtWidgets.QLabel(self.centralwidget)
          self.label.setGeometry(QtCore.QRect(20, 20, 211, 16))
          self.label.setObjectName("label")

          self.tc = ThreadClass(self)
          self.pushButton.clicked.connect(lambda: self.tc.start())
          #self.pushButton.clicked.connect(lambda: start_new_thread(showtime, (self.label, )))

          self.tc.sig.connect(self.showtime) # +++
          self.tc.finish.connect(self.finishTime) # +++


          MainWindow.setCentralWidget(self.centralwidget)


          QtCore.QMetaObject.connectSlotsByName(MainWindow)
          self.pushButton.setText("Show")
          self.label.setText("Time")

          # +++
          def showtime(self, t):
          self.label.setText(str(t))

          # +++
          def finishTime(self):
          self.mbox = QtWidgets.QMessageBox()
          self.mbox.about(QtWidgets.QMainWindow(), "Title", "Finished")


          if __name__ == '__main__':
          app = QtWidgets.QApplication(sys.argv)
          w = QtWidgets.QMainWindow()
          ex = Ui_MainWindow()
          ex.setupUi(w)
          w.show()
          sys.exit(app.exec_())


          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 12:26









          S. Nick

          2,156237




          2,156237











          • Adding some explanation would make this answer more useful.
            – mx0
            Nov 10 at 15:24
















          • Adding some explanation would make this answer more useful.
            – mx0
            Nov 10 at 15:24















          Adding some explanation would make this answer more useful.
          – mx0
          Nov 10 at 15:24




          Adding some explanation would make this answer more useful.
          – mx0
          Nov 10 at 15:24










          movep is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          movep is a new contributor. Be nice, and check out our Code of Conduct.












          movep is a new contributor. Be nice, and check out our Code of Conduct.











          movep is a new contributor. Be nice, and check out our Code of Conduct.













           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238682%2fin-pyqt5-new-parent-is-in-a-different-qthread-after-pop-up-qmessagebox%23new-answer', 'question_page');

          );

          Post as a guest














































































          這個網誌中的熱門文章

          What does pagestruct do in Eviews?

          Dutch intervention in Lombok and Karangasem

          Channel Islands