无法使用pyqt5显示秒表

Posted

技术标签:

【中文标题】无法使用pyqt5显示秒表【英文标题】:Can't display stop watch using pyqt5 【发布时间】:2019-12-18 08:26:51 【问题描述】:

我正在构建一个非常简单的 GUI,但我无法计算出我的秒表。 我有两个不同的文件 - 一个用于 GUI 的变量,一个用于具有所有功能的主文件。 我的目标是,一旦用户按下“运行”按钮,计时器将出现在小文本框中,每秒以“hh:mm:ss”的形式变化。 我所做的基本上是收集一些我认为适合我的脚本的信息和一些代码行,但我认为我有一些错误并且它不起作用(我试图改变一些东西,但我明白了每次都有不同的错误..)

这些是我的文件:

图形用户界面文件:

from PyQt5 import QtCore, QtGui, QtWidgets



 class Ui_MainWindow(object):
  def setupUi(self, MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(895, 422)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")


    self.timer=QtCore.QTimer(self.centralwidget)
    self.timer.timeout.connect(self.showTime)
    self.timer.start(1000)
    self.showTime(MainWindow)

    ###txtoutput = big window for text###
    self.txtoutput = QtWidgets.QTextBrowser(self.centralwidget)
    self.txtoutput.setGeometry(QtCore.QRect(150, 200, 471, 192))
    self.txtoutput.setObjectName("txtoutput")

    ###modeltype = droplist###
    self.modeltype = QtWidgets.QComboBox(self.centralwidget)
    self.modeltype.setGeometry(QtCore.QRect(50, 90, 69, 22))
    self.modeltype.setObjectName("modeltype")
    self.modeltype.addItem("")
    self.modeltype.addItem("")
    self.modeltype.addItem("")

    ###seriallbl = label above input window###
    self.seriallbl = QtWidgets.QLabel(self.centralwidget)
    self.seriallbl.setGeometry(QtCore.QRect(90, 30, 81, 16))
    self.seriallbl.setObjectName("seriallbl")

    ###getserialbtn = "Enter" button###
    self.getserialbtn = QtWidgets.QPushButton(self.centralwidget)
    self.getserialbtn.setGeometry(QtCore.QRect(210, 50, 75, 23))
    self.getserialbtn.setObjectName("getserialbtn")

    ###chuckrealeasebtn = "Chuck"/"Release" button###
    self.chuckrealeasebtn = QtWidgets.QPushButton(self.centralwidget)
    self.chuckrealeasebtn.setEnabled(False)
    self.chuckrealeasebtn.setGeometry(QtCore.QRect(50, 120, 75, 23))
    self.chuckrealeasebtn.setObjectName("chuckrealeasebtn")
    self.chuckrealeasebtn.setCheckable(True)

    ###runbtn = "Run" button###
    self.runbtn = QtWidgets.QPushButton(self.centralwidget)
    self.runbtn.setEnabled(False)
    self.runbtn.setGeometry(QtCore.QRect(580, 70, 91, 61))
    font = QtGui.QFont()
    font.setPointSize(28)
    font.setBold(True)
    font.setWeight(75)
    self.runbtn.setFont(font)
    self.runbtn.setCheckable(False)
    self.runbtn.setObjectName("runbtn")

    ###serialinput = Text box for serial # input###
    self.serialinput = QtWidgets.QTextEdit(self.centralwidget)
    self.serialinput.setGeometry(QtCore.QRect(50, 50, 151, 31))
    self.serialinput.setObjectName("serialinput")

    ###stopbtn = "Stop" button###
    self.stopbtn = QtWidgets.QPushButton(self.centralwidget)
    self.stopbtn.setGeometry(QtCore.QRect(680, 70, 91, 61))
    font = QtGui.QFont()
    font.setPointSize(28)
    font.setBold(True)
    font.setWeight(75)
    self.stopbtn.setFont(font)
    self.stopbtn.setObjectName("stopbtn")

    ###exitbtn = "Exit" button###
    self.exitbtn = QtWidgets.QPushButton(self.centralwidget)
    self.exitbtn.setGeometry(QtCore.QRect(740, 370, 75, 23))
    self.exitbtn.setObjectName("exitbtn")
    self.timelbl = QtWidgets.QLabel(self.centralwidget)
    self.timelbl.setGeometry(QtCore.QRect(320, 140, 47, 13))
    self.timelbl.setObjectName("timelbl")

    ###timertxt = label above timer window###
    self.timertxt = QtWidgets.QTextBrowser(self.centralwidget)
    self.timertxt.setGeometry(QtCore.QRect(260, 160, 141, 31))
    self.timertxt.setObjectName("timertxt")


    MainWindow.setCentralWidget(self.centralwidget)
    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)



def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    self.modeltype.setItemText(0, _translate("MainWindow", "Type 1"))
    self.modeltype.setItemText(1, _translate("MainWindow", "Type 2"))
    self.modeltype.setItemText(2, _translate("MainWindow", "Type 3"))
    self.seriallbl.setText(_translate("MainWindow", "Enter serial #"))
    self.getserialbtn.setText(_translate("MainWindow", "Enter"))
    self.chuckrealeasebtn.setText(_translate("MainWindow", "chuck"))
    self.runbtn.setText(_translate("MainWindow", "Run"))
    self.stopbtn.setText(_translate("MainWindow", "Stop"))
    self.exitbtn.setText(_translate("MainWindow", "Exit"))
    self.timelbl.setText(_translate("MainWindow", "Timer"))

还有我的主要:

from PyQt5 import QtWidgets, QtCore, QtGui
from mainwin import Ui_MainWindow


class ApplicationWindow(QtWidgets.QMainWindow):
  def __init__(self):
   super(ApplicationWindow, self).__init__()

   self.ui = Ui_MainWindow()
   self.ui.setupUi(self)   

   self.ui.exitbtn.clicked.connect(self.exitclicked)      
   self.ui.getserialbtn.clicked.connect(self.serialentered)
   self.ui.chuckrealeasebtn.clicked.connect(self.chuckclicked)
   self.ui.runbtn.clicked.connect(self.showTime)

###exitclicked = "Exit" button function###
def exitclicked(self):                                      
    self.close()


def showTime(self):
    time=self.ui.QTime.currentTime()
    text=self.ui.time.toString('hh:mm:ss')
    if(time.second()%2==0):
        text=text[:2]+ ' ' +text [3:]
    self.ui.txtoutput.append(text)


###chuckclicked = "Chuck"/"Release" button function###    
def chuckclicked(self):
    if self.ui.chuckrealeasebtn.isChecked():
        mytext = self.ui.serialinput.toPlainText()
        self.ui.chuckrealeasebtn.setText("Release")
        self.ui.getserialbtn.setEnabled(False)
        self.ui.runbtn.setEnabled(True)
        self.ui.txtoutput.append(mytext+" - is chucked, Ready to run")
    else:
        self.ui.chuckrealeasebtn.setText("Chuck")
        self.ui.txtoutput.clear()
        self.ui.chuckrealeasebtn.setEnabled(False)
        self.ui.getserialbtn.setEnabled(True)
        self.ui.runbtn.setEnabled(False)

###serialentered = function that checks if the serial # that was entered is found or not found in a certain list###    
def serialentered(self):
    serialnum = self.ui.serialinput.toPlainText()
    listnum=self.ui.modeltype.currentIndex()
    if (listnum==0):
        with open(r'C:\Users\hv_lab\Documents\python tests\Serial Numbers - Type 1.txt') as r1:
            if serialnum in r1.read():
                self.ui.txtoutput.append(serialnum+" - Serial was found in List 1")
                self.ui.chuckrealeasebtn.setEnabled(True)
            else:
                self.ui.txtoutput.append(serialnum+" - Serial was NOT found in List 1")
                self.ui.chuckrealeasebtn.setEnabled(False)
    elif (listnum==1):
        with open(r'C:\Users\hv_lab\Documents\python tests\Serial Numbers - Type 2.txt') as r2:
            if serialnum in r2.read():
                self.ui.txtoutput.append(serialnum+" - Serial was found in List 2")
                self.ui.chuckrealeasebtn.setEnabled(True)
            else:
                self.ui.txtoutput.append(serialnum+" - Serial was NOT found in List 2")
                self.ui.chuckrealeasebtn.setEnabled(False)
    elif (listnum==2):
        with open(r'C:\Users\hv_lab\Documents\python tests\Serial Numbers - Type 3.txt') as r3:
            if serialnum in r3.read():
                self.ui.txtoutput.append(serialnum+" - Serial was found in List 3")
                self.ui.chuckrealeasebtn.setEnabled(True)
            else:
                 self.ui.txtoutput.append(serialnum+" - Serial was NOT found in List 3")
                 self.ui.chuckrealeasebtn.setEnabled(False)



 def main():
  import sys
  app = QtWidgets.QApplication(sys.argv)
  application = ApplicationWindow()
  application.show()
  sys.exit(app.exec_())

if __name__ == "__main__":
 main()

所以我不确定这里到底出了什么问题 - 如果有人有任何我没有想到的想法,我会很高兴..

提前谢谢各位!

***我应该编辑我希望秒表从 00:00:00 开始,并在我点击“运行”按钮后每秒计数一次”

【问题讨论】:

【参考方案1】:

切勿修改在 QT Designer 中创建的模块。 试试看:

from PyQt5 import QtCore, QtGui, QtWidgets


# from mainwin import Ui_MainWindow
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(895, 422)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")


# -       self.timer=QtCore.QTimer(self.centralwidget)
# -       self.timer.timeout.connect(self.showTime)
# -       self.timer.start(1000)
# -       self.showTime(MainWindow)

        ###txtoutput = big window for text###
        self.txtoutput = QtWidgets.QTextBrowser(self.centralwidget)
        self.txtoutput.setGeometry(QtCore.QRect(150, 200, 471, 192))
        self.txtoutput.setObjectName("txtoutput")

        ###modeltype = droplist###
        self.modeltype = QtWidgets.QComboBox(self.centralwidget)
        self.modeltype.setGeometry(QtCore.QRect(50, 90, 69, 22))
        self.modeltype.setObjectName("modeltype")
        self.modeltype.addItem("")
        self.modeltype.addItem("")
        self.modeltype.addItem("")

        ###seriallbl = label above input window###
        self.seriallbl = QtWidgets.QLabel(self.centralwidget)
        self.seriallbl.setGeometry(QtCore.QRect(90, 30, 81, 16))
        self.seriallbl.setObjectName("seriallbl")

        ###getserialbtn = "Enter" button###
        self.getserialbtn = QtWidgets.QPushButton(self.centralwidget)
        self.getserialbtn.setGeometry(QtCore.QRect(210, 50, 75, 23))
        self.getserialbtn.setObjectName("getserialbtn")

        ###chuckrealeasebtn = "Chuck"/"Release" button###
        self.chuckrealeasebtn = QtWidgets.QPushButton(self.centralwidget)
        self.chuckrealeasebtn.setEnabled(False)
        self.chuckrealeasebtn.setGeometry(QtCore.QRect(50, 120, 75, 23))
        self.chuckrealeasebtn.setObjectName("chuckrealeasebtn")
        self.chuckrealeasebtn.setCheckable(True)

        ###runbtn = "Run" button###
        self.runbtn = QtWidgets.QPushButton(self.centralwidget)
# -       self.runbtn.setEnabled(False)
        self.runbtn.setGeometry(QtCore.QRect(580, 70, 91, 61))
        font = QtGui.QFont()
        font.setPointSize(28)
        font.setBold(True)
        font.setWeight(75)
        self.runbtn.setFont(font)
        self.runbtn.setCheckable(False)
        self.runbtn.setObjectName("runbtn")

        ###serialinput = Text box for serial # input###
        self.serialinput = QtWidgets.QTextEdit(self.centralwidget)
        self.serialinput.setGeometry(QtCore.QRect(50, 50, 151, 31))
        self.serialinput.setObjectName("serialinput")

        ###stopbtn = "Stop" button###
        self.stopbtn = QtWidgets.QPushButton(self.centralwidget)
        self.stopbtn.setGeometry(QtCore.QRect(680, 70, 91, 61))
        font = QtGui.QFont()
        font.setPointSize(28)
        font.setBold(True)
        font.setWeight(75)
        self.stopbtn.setFont(font)
        self.stopbtn.setObjectName("stopbtn")

        ###exitbtn = "Exit" button###
        self.exitbtn = QtWidgets.QPushButton(self.centralwidget)
        self.exitbtn.setGeometry(QtCore.QRect(740, 370, 75, 23))
        self.exitbtn.setObjectName("exitbtn")
        self.timelbl = QtWidgets.QLabel(self.centralwidget)
        self.timelbl.setGeometry(QtCore.QRect(320, 140, 47, 13))
        self.timelbl.setObjectName("timelbl")

        ###timertxt = label above timer window###
        self.timertxt = QtWidgets.QTextBrowser(self.centralwidget)
        self.timertxt.setGeometry(QtCore.QRect(260, 160, 141, 31))
        self.timertxt.setObjectName("timertxt")


        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)



    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.modeltype.setItemText(0, _translate("MainWindow", "Type 1"))
        self.modeltype.setItemText(1, _translate("MainWindow", "Type 2"))
        self.modeltype.setItemText(2, _translate("MainWindow", "Type 3"))
        self.seriallbl.setText(_translate("MainWindow", "Enter serial #"))
        self.getserialbtn.setText(_translate("MainWindow", "Enter"))
        self.chuckrealeasebtn.setText(_translate("MainWindow", "chuck"))
        self.runbtn.setText(_translate("MainWindow", "Run"))
        self.stopbtn.setText(_translate("MainWindow", "Stop"))
        self.exitbtn.setText(_translate("MainWindow", "Exit"))
        self.timelbl.setText(_translate("MainWindow", "Timer"))


class ApplicationWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(ApplicationWindow, self).__init__()

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)   

        self.ui.exitbtn.clicked.connect(self.exitclicked)      
        self.ui.getserialbtn.clicked.connect(self.serialentered)
        self.ui.chuckrealeasebtn.clicked.connect(self.chuckclicked)
        self.ui.runbtn.clicked.connect(self.onClick_runbtn)         # - (self.showTime)

        self.timer = QtCore.QTimer(self)                            # +++
        self.timer.timeout.connect(self.showTime)                   # +++

    ###exitclicked = "Exit" button function###
    def exitclicked(self):                                      
        self.close()

    def onClick_runbtn(self):                                       # +++
        self.timer.start(1000)                                      # +++

    def showTime(self):
        time = QtCore.QTime.currentTime()                           # - self.ui.QTime.currentTime()
        text = time.toString('hh:mm:ss')                            # - self.ui.time.toString('hh:mm:ss')
        if(time.second()%2==0):
            text = text[:2]+ ' ' +text [3:]
#        self.ui.txtoutput.append(text)
        self.ui.timelbl.setText(text)

    ###chuckclicked = "Chuck"/"Release" button function###    
    def chuckclicked(self):
        if self.ui.chuckrealeasebtn.isChecked():
            mytext = self.ui.serialinput.toPlainText()
            self.ui.chuckrealeasebtn.setText("Release")
            self.ui.getserialbtn.setEnabled(False)
            self.ui.runbtn.setEnabled(True)
            self.ui.txtoutput.append(mytext+" - is chucked, Ready to run")
        else:
            self.ui.chuckrealeasebtn.setText("Chuck")
            self.ui.txtoutput.clear()
            self.ui.chuckrealeasebtn.setEnabled(False)
            self.ui.getserialbtn.setEnabled(True)
            self.ui.runbtn.setEnabled(False)

    ###serialentered = function that checks if the serial # that was entered is found or not found in a certain list###    
    def serialentered(self):
        serialnum = self.ui.serialinput.toPlainText()
        listnum=self.ui.modeltype.currentIndex()
        if (listnum==0):
            with open(r'C:\Users\hv_lab\Documents\python tests\Serial Numbers - Type 1.txt') as r1:
                if serialnum in r1.read():
                    self.ui.txtoutput.append(serialnum+" - Serial was found in List 1")
                    self.ui.chuckrealeasebtn.setEnabled(True)
                else:
                    self.ui.txtoutput.append(serialnum+" - Serial was NOT found in List 1")
                    self.ui.chuckrealeasebtn.setEnabled(False)
        elif (listnum==1):
            with open(r'C:\Users\hv_lab\Documents\python tests\Serial Numbers - Type 2.txt') as r2:
                if serialnum in r2.read():
                    self.ui.txtoutput.append(serialnum+" - Serial was found in List 2")
                    self.ui.chuckrealeasebtn.setEnabled(True)
                else:
                    self.ui.txtoutput.append(serialnum+" - Serial was NOT found in List 2")
                    self.ui.chuckrealeasebtn.setEnabled(False)
        elif (listnum==2):
            with open(r'C:\Users\hv_lab\Documents\python tests\Serial Numbers - Type 3.txt') as r3:
                if serialnum in r3.read():
                    self.ui.txtoutput.append(serialnum+" - Serial was found in List 3")
                    self.ui.chuckrealeasebtn.setEnabled(True)
                else:
                     self.ui.txtoutput.append(serialnum+" - Serial was NOT found in List 3")
                     self.ui.chuckrealeasebtn.setEnabled(False)



if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    application = ApplicationWindow()
    application.show()
    sys.exit(app.exec_())


更新

为什么我不应该编辑在 QT 设计器中创建的模块?

想一想如果您想更改设计会发生什么。你会记住很长一段时间你需要做哪些改变......

我希望我的秒表从 0 开始,然后每秒计数。有没有办法做到这一点,或者我只能显示当前时间?

from PyQt5 import QtCore, QtGui, QtWidgets
import time                                                            # +++

# from mainwin import Ui_MainWindow
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(895, 422)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        ###txtoutput = big window for text###
        self.txtoutput = QtWidgets.QTextBrowser(self.centralwidget)
        self.txtoutput.setGeometry(QtCore.QRect(150, 200, 471, 192))
        self.txtoutput.setObjectName("txtoutput")
        ###modeltype = droplist###
        self.modeltype = QtWidgets.QComboBox(self.centralwidget)
        self.modeltype.setGeometry(QtCore.QRect(50, 90, 69, 22))
        self.modeltype.setObjectName("modeltype")
        self.modeltype.addItem("")
        self.modeltype.addItem("")
        self.modeltype.addItem("")
        ###seriallbl = label above input window###
        self.seriallbl = QtWidgets.QLabel(self.centralwidget)
        self.seriallbl.setGeometry(QtCore.QRect(90, 30, 81, 16))
        self.seriallbl.setObjectName("seriallbl")
        ###getserialbtn = "Enter" button###
        self.getserialbtn = QtWidgets.QPushButton(self.centralwidget)
        self.getserialbtn.setGeometry(QtCore.QRect(210, 50, 75, 23))
        self.getserialbtn.setObjectName("getserialbtn")
        ###chuckrealeasebtn = "Chuck"/"Release" button###
        self.chuckrealeasebtn = QtWidgets.QPushButton(self.centralwidget)
        self.chuckrealeasebtn.setEnabled(False)
        self.chuckrealeasebtn.setGeometry(QtCore.QRect(50, 120, 75, 23))
        self.chuckrealeasebtn.setObjectName("chuckrealeasebtn")
        self.chuckrealeasebtn.setCheckable(True)
        ###runbtn = "Run" button###
        self.runbtn = QtWidgets.QPushButton(self.centralwidget)
# -       self.runbtn.setEnabled(False)
        self.runbtn.setGeometry(QtCore.QRect(580, 70, 91, 61))
        font = QtGui.QFont()
        font.setPointSize(28)
        font.setBold(True)
        font.setWeight(75)
        self.runbtn.setFont(font)
        self.runbtn.setCheckable(False)
        self.runbtn.setObjectName("runbtn")
        ###serialinput = Text box for serial # input###
        self.serialinput = QtWidgets.QTextEdit(self.centralwidget)
        self.serialinput.setGeometry(QtCore.QRect(50, 50, 151, 31))
        self.serialinput.setObjectName("serialinput")
        ###stopbtn = "Stop" button###
        self.stopbtn = QtWidgets.QPushButton(self.centralwidget)
        self.stopbtn.setGeometry(QtCore.QRect(680, 70, 91, 61))
        font = QtGui.QFont()
        font.setPointSize(28)
        font.setBold(True)
        font.setWeight(75)
        self.stopbtn.setFont(font)
        self.stopbtn.setObjectName("stopbtn")
        ###exitbtn = "Exit" button###
        self.exitbtn = QtWidgets.QPushButton(self.centralwidget)
        self.exitbtn.setGeometry(QtCore.QRect(740, 370, 75, 23))
        self.exitbtn.setObjectName("exitbtn")
        self.timelbl = QtWidgets.QLabel(self.centralwidget)
        self.timelbl.setGeometry(QtCore.QRect(260, 140, 147, 23))         # +
        self.timelbl.setObjectName("timelbl")
        ###timertxt = label above timer window###
        self.timertxt = QtWidgets.QTextBrowser(self.centralwidget)
        self.timertxt.setGeometry(QtCore.QRect(260, 160, 141, 31))
        self.timertxt.setObjectName("timertxt")

        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.modeltype.setItemText(0, _translate("MainWindow", "Type 1"))
        self.modeltype.setItemText(1, _translate("MainWindow", "Type 2"))
        self.modeltype.setItemText(2, _translate("MainWindow", "Type 3"))
        self.seriallbl.setText(_translate("MainWindow", "Enter serial #"))
        self.getserialbtn.setText(_translate("MainWindow", "Enter"))
        self.chuckrealeasebtn.setText(_translate("MainWindow", "chuck"))
        self.runbtn.setText(_translate("MainWindow", "Run"))
        self.stopbtn.setText(_translate("MainWindow", "Stop"))
        self.exitbtn.setText(_translate("MainWindow", "Exit"))
        self.timelbl.setText(_translate("MainWindow", "Timer"))


class ApplicationWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(ApplicationWindow, self).__init__()

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)   

        self.ui.exitbtn.clicked.connect(self.exitclicked)      
        self.ui.getserialbtn.clicked.connect(self.serialentered)
        self.ui.chuckrealeasebtn.clicked.connect(self.chuckclicked)
        self.ui.runbtn.clicked.connect(self.onClick_runbtn)            # + self.onClick_runbtn 

### VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV

        self.timer = QtCore.QTimer(self)                            
        self.timer.timeout.connect(self.showTime)                   
        self.time = 0
        self.timeInterval = 1000

    def onClick_runbtn(self):                                       
        self.timer.start(self.timeInterval)                                      
        self.timeViewer = QtWidgets.QLCDNumber(self)
        self.timeViewer.setGeometry(QtCore.QRect(280, 80, 100, 45))
        self.timeViewer.show()

    def showTime(self):  
        self.time += 1
        self.settimer(self.time)
        ##print(self.time)

    def settimer(self, int):
        self.time = int
        self.timeViewer.display(self.time)
        if self.timeInterval == 1000:
            self.ui.timelbl.setText(time.strftime('%H hour %M minute %S second',time.gmtime(self.time)))

### ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^       

    ###exitclicked = "Exit" button function###
    def exitclicked(self):                                      
        self.close()        

    ###chuckclicked = "Chuck"/"Release" button function###    
    def chuckclicked(self):
        if self.ui.chuckrealeasebtn.isChecked():
            mytext = self.ui.serialinput.toPlainText()
            self.ui.chuckrealeasebtn.setText("Release")
            self.ui.getserialbtn.setEnabled(False)
            self.ui.runbtn.setEnabled(True)
            self.ui.txtoutput.append(mytext+" - is chucked, Ready to run")
        else:
            self.ui.chuckrealeasebtn.setText("Chuck")
            self.ui.txtoutput.clear()
            self.ui.chuckrealeasebtn.setEnabled(False)
            self.ui.getserialbtn.setEnabled(True)
            self.ui.runbtn.setEnabled(False)

    ###serialentered = function that checks if the serial # that was entered is found or not found in a certain list###    
    def serialentered(self):
        serialnum = self.ui.serialinput.toPlainText()
        listnum=self.ui.modeltype.currentIndex()
        if (listnum==0):
            with open(r'C:\Users\hv_lab\Documents\python tests\Serial Numbers - Type 1.txt') as r1:
                if serialnum in r1.read():
                    self.ui.txtoutput.append(serialnum+" - Serial was found in List 1")
                    self.ui.chuckrealeasebtn.setEnabled(True)
                else:
                    self.ui.txtoutput.append(serialnum+" - Serial was NOT found in List 1")
                    self.ui.chuckrealeasebtn.setEnabled(False)
        elif (listnum==1):
            with open(r'C:\Users\hv_lab\Documents\python tests\Serial Numbers - Type 2.txt') as r2:
                if serialnum in r2.read():
                    self.ui.txtoutput.append(serialnum+" - Serial was found in List 2")
                    self.ui.chuckrealeasebtn.setEnabled(True)
                else:
                    self.ui.txtoutput.append(serialnum+" - Serial was NOT found in List 2")
                    self.ui.chuckrealeasebtn.setEnabled(False)
        elif (listnum==2):
            with open(r'C:\Users\hv_lab\Documents\python tests\Serial Numbers - Type 3.txt') as r3:
                if serialnum in r3.read():
                    self.ui.txtoutput.append(serialnum+" - Serial was found in List 3")
                    self.ui.chuckrealeasebtn.setEnabled(True)
                else:
                     self.ui.txtoutput.append(serialnum+" - Serial was NOT found in List 3")
                     self.ui.chuckrealeasebtn.setEnabled(False)


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    application = ApplicationWindow()
    application.show()
    sys.exit(app.exec_())

【讨论】:

嗨尼克!首先 - 非常感谢你。我有几个问题:为什么我不应该从不编辑在 QT 设计器中创建的模块?我编辑了我希望我的秒表从 0 开始,并且每秒计数。有没有办法做到这一点,或者我只能显示当前时间?非常感谢! 嘿尼克,很抱歉仍然打扰你:我现在正试图让秒表在我按下停止按钮后再次变为 0,但基本上下一次手表的速度会加倍。 .. @Alon123 提出一个新问题,附上最新版本的例子。写下你想做的和不能做的。

以上是关于无法使用pyqt5显示秒表的主要内容,如果未能解决你的问题,请参考以下文章

PyQt5 应用在 TeamViewer 下无法使用全屏模式

无法在 Qt 设计器(macos)中显示自定义 PyQt5 小部件插件

PyQt5 - 即使使用 QThread,MainWindow 中的 QMovie 也无法播放

秒表与计时器 - 何时使用

PyQt5 无法连接到显示器

我无法使用 MatplotLib 生成图表以在 PyQt5 App 上绘图