小编Ser*_*tin的帖子

QThread:线程仍在运行时被销毁

我在python中遇到QThreads的问题.我想改变标签的背景颜色.但我的应用程序在启动时崩溃."QThread:在线程仍在运行时被破坏"

   class MainWindow(QMainWindow):
      def __init__(self):
          QMainWindow.__init__(self)
          self.ui = Ui_MainWindow()
          self.ui.setupUi(self)

          statusTh = statusThread(self)
          self.connect(statusTh, SIGNAL('setStatus'), self.st, Qt.QueuedConnection)
          statusTh.start()

      def st(self):
          if self.status == 'ON':
              self.ui.label.setStyleSheet('background-color:green')
          else:
              self.ui.label.setStyleSheet('background-color:red')

  class statusThread(QThread):
      def __init__(self, mw):
          super(statusThread, self).__init__()

      def run(self):
          while True:
              time.sleep(1)
              self.emit(SIGNAL('setStatus'))

  if __name__ == "__main__":
      app = QApplication(sys.argv)
      main_window = MainWindow()
      main_window.show()
      sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)

python pyqt qthread

16
推荐指数
1
解决办法
1万
查看次数

标签 统计

pyqt ×1

python ×1

qthread ×1