我目前正在尝试使用PyQt创建一个线程计时器应用程序。很简单,对吧?我也是这么想。然而,花了一整天的时间想弄清楚出了什么问题,我仍然绝对不知道。在我所有的巨大固执之中,我拒绝放弃本应该是15分钟的项目。QThread神秘错误
继承人麻将codez:
__author__ = 'Darth Vader'
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox, QApplication, QDialog
from PyQt5.QtCore import QThread
from timerui import Ui_Form
import sys
import ctypes
import time
import threading
class Timer(QThread):
def makeNoise(self):
pass
def run(self):
self.ui.startButton.setStyleSheet('''QPushButton {color: red;font: bold 15px;}''')
self.ui.startButton.setEnabled(False)
self.hour = int(self.ui.spinBoxHrs.value())
self.min = int(self.ui.spinBoxMin.value())
self.sec = int(self.ui.spinBoxSec.value())
if self.sec:
self.countSec = self.sec
elif self.min and not self.sec:
self.countSec = 60
self.min -= 1
elif self.hour and not self.min and not self.sec:
self.min = 59
self.countSec = 60
print(self.countSec)
while self.countSec or self.hour or self.min:
if not self.countSec and self.min:
self.min -= 1
self.countSec = 60
elif not self.countSec and not self.min and self.hour:
self.hour -= 1
self.min = 59
self.sec = 60
elif not self.countSec and not self.min and not self.hour:
self.makeNoise()
break
time.sleep(1)
self.countSec -= 1
self.ui.startButton.setText("%dh %dm %ds" % (self.hour, self.min, self.sec))
self.ui.startButton.setEnabled(True)
self.ui.startButton.setText("Start")
self.ui.startButton.setStyleSheet('QPushButton{}')
def setup(self, gui):
self.ui = gui
def __init__(self):
QThread.__init__(self)
def start():
t = Timer()
t.start()
if __name__ == '__main__':
myappid = u'org.ayoung.timer'
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
app = QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon('res/favicon.png'))
window = QDialog()
ui = Ui_Form()
ui.setupUi(window)
ui.startButton.clicked.connect(start)
window.show()
sys.exit(app.exec_())
和错误:
QThread: Destroyed while thread is still running
QMutex: destroying locked mutex
从我读过,这两个错误是与垃圾收集,但我完全不知道如何解决它们。
谢谢!
除此之外,QThread.start()返回无,并且线程没有被分配给变量,这就是为什么它被销毁。老实说,这个代码有很多错误,很难知道从哪里开始。 – 2015-02-24 09:02:42
给我一个休息时间,这是我第一次使用PyQT。此外,当我有两个单独的陈述时,也发生了同样的事情。 @warvariuc试过了,现在我刚刚得到“QThread:线程仍在运行时被销毁”的错误。 – 2015-02-24 09:25:07
@DarthVader我知道你有这个代码的其他问题,但我回答你的主要问题。 Stackoverflow试图回答一个问题,否则你最终会讨论代码而不是回答小而明确的问题。你可以讨论你的代码[这里](http://chat.stackoverflow.com/rooms/6/python) – warvariuc 2015-02-24 10:10:44