qt设计中如何更新按钮文本标签中的定时器值

2024-04-19 08:19:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我是pyqt新手,想知道如何更新窗口中按钮文本标签中的计时器值。我只想,当我运行脚本时,它将打开包含按钮的新窗口,在该按钮文本中,计时器将显示计时器值,我们将计时器值作为输入。 显示我的代码下图:-你知道吗

import time
from PyQt4 import QtCore, QtGui
import sys

timeData=0

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'))
        #while 1:
        self.home()

    def home(self):
        btn = QtGui.QPushButton(self)
        btn.setText(self.timer())
        btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
        btn.resize(100,100)
        btn.move(100,100)
        self.show()

    #def updateTimer(self):

    def timer(self):
      uin = input("enter the time : ")
      when_to_stop = abs(int(uin))
      while when_to_stop > -1:
     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)
     when_to_stop -= 1
         global timeData
         timeData=timeData+2
         time.sleep(0.9)    
         return(str(time_left+'\r'))

def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())

run()

请帮我找出我的问题


Tags: toimportselftimedefsyswindow按钮