-1

I'm making a stopwatch using Python and pyqt5 and I'm asking you a question because there's one problem.

(Set to 5 seconds for now) When I press the start button on the stopwatch, I want to make a code that runs 0 -> 1 -> 2 -> 3 -> 4 -> 5 in sequence on the GUI screen, but the code I squeeze is outputting 0 and a few seconds later to 5. Do you happen to have this error because there is a problem with thread? Or is there any other problem?

from PyQt5 import uic
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import datetime
import time
CalUI = '../poor_timer/timer_gui.ui'
class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self, None)
        uic.loadUi(CalUI, self)

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.timeout)
        self.lcdNumber.setDigitCount(8)
        # 처음 값 세팅은 45분 15분 쉬기
        self.lcdNumber.display('0')
        # 알람끄기/ 공부 시작/ 공부 멈춤
        self.pushButton.clicked.connect(self.onStartButtonClicked)

    def onStartButtonClicked(self):
        self.timer.start()

    def timeout(self):
        sender = self.sender()
        for i in range(5):
            currentTime = str(i)
            self.lcdNumber.display(currentTime)
            time.sleep(1)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_dialog = MainWindow()
    main_dialog.show()
    app.exec_()
권기원
  • 11
  • 1
  • 1
    please provide a [mre] – eyllanesc Mar 28 '21 at 08:06
  • "but the code I squeeze is outputting 0 and a few seconds later to 5" My guess is that this happens because there is something wrong with your code. We can only try to tell you what is wrong with the code, if we see the code. – Karl Knechtel Mar 28 '21 at 08:41

1 Answers1

0

Since you don't have any minimal reproducible example code on hand, I can try to help based off just my experience with QtTimers. Here's an example of what it should look like in the __init__ of your UIWindow:

self.timer = QtCore.QTimer()
self.timer.start(1000) # updates the timer display every second

Setting up the UI:

# Time Dialog LCD Display
self.timeDialog = QtWidgets.QLCDNumber(self.centralwidget)
    # <<< any other irrelevant QLDCNumber settings set up here >>>
self.timeDialog.setObjectName("timeDialog")

Example of formatting time for hour:minutes and minutes:seconds

# Format the time
timeformatHourMin = '{:02d}:{:02d}'.format(self.duration_hour, self.duration_minute)
timeformatMinSec = '{:02d}:{:02d}'.format(self.duration_minute, self.duration_seconds)

# Displaying the specified format
if self.duration_hour != 0:
    self.timeDialog.display(timeformatHourMin)
elif self.duration_hour == 0:
    self.timeDialog.display(timeformatMinSec)

Again, I apologize for the vagueness (hopefully you get the idea). But as mentioned above, please provide specifics (code & error log) if you would like further assistance. Also, feel free to comment if you have any questions.

ObjectJosh
  • 601
  • 3
  • 14