我目前正在嘗試使用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