【发布时间】:2018-08-01 05:53:29
【问题描述】:
我在一个接一个地同时运行计时器时遇到问题。下一个计时器正在运行,前一个计时器停止,到达前一个计时器开始的时间后。下面列出了有问题的代码。
import time
from PyQt4 import QtCore, QtGui
import sys
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50, 50, 500, 300)
self.setWindowTitle("PyQT tuts!")
self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))
self.home()
def home(self):
self.btn = QtGui.QPushButton(self)
self.btn.setObjectName(_fromUtf8("pb"))
self.btn.clicked.connect(self.timer)
self.btn.setText("Timer1")
self.btn.resize(65,25)
self.btn.move(100,100)
self.btn2 = QtGui.QPushButton(self)
self.btn2.setObjectName(_fromUtf8("pb2"))
self.btn2.clicked.connect(self.timer2)
self.btn2.setText("Timer2")
self.btn2.resize(65,25)
self.btn2.move(100,150)
self.btn3 = QtGui.QPushButton(self)
self.btn3.setObjectName(_fromUtf8("pb3"))
self.btn3.clicked.connect(self.timer3)
self.btn3.setText("Timer3")
self.btn3.resize(65,25)
self.btn3.move(100,200)
self.btn4 = QtGui.QPushButton(self)
self.btn4.setObjectName(_fromUtf8("pb4"))
self.btn4.clicked.connect(self.timer4)
self.btn4.setText("Timer4")
self.btn4.resize(65,25)
self.btn4.move(100,250)
self.show()
def timer(self):
# uin = input("enter the time : ")
when_to_stop = 10
# abs(int(uin))
while when_to_stop > 0:
m, s = divmod(when_to_stop, 60)
h, m = divmod(m, 60)
time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2)
# print(time_left+'\r')
time.sleep(1.0)
when_to_stop -= 1
self.btn.setText(str(time_left))
QtGui.qApp.processEvents()
def timer2(self):
# uin = input("enter the time : ")
when_to_stop = 10
# abs(int(uin))
while when_to_stop > 0:
m, s = divmod(when_to_stop, 60)
h, m = divmod(m, 60)
time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2)
# print(time_left+'\r')
time.sleep(1.0)
when_to_stop -= 1
self.btn2.setText(str(time_left))
QtGui.qApp.processEvents()
def timer3(self):
# uin = input("enter the time : ")
when_to_stop = 10
# abs(int(uin))
while when_to_stop > 0:
m, s = divmod(when_to_stop, 60)
h, m = divmod(m, 60)
time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2)
# print(time_left+'\r')
time.sleep(1.0)
when_to_stop -= 1
self.btn3.setText(str(time_left))
QtGui.qApp.processEvents()
def timer4(self):
# uin = input("enter the time : ")
when_to_stop = 10
# abs(int(uin))
while when_to_stop > 0:
m, s = divmod(when_to_stop, 60)
h, m = divmod(m, 60)
time_left = str(h).zfill(2) + ":" + str(m).zfill(2) + ":" + str(s).zfill(2)
# print(time_left+'\r')
time.sleep(1.0)
when_to_stop -= 1
self.btn4.setText(str(time_left))
QtGui.qApp.processEvents()
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
run()
【问题讨论】:
标签: python python-2.7 pyqt pyqt4