0

so I want to update the label every second so it creates a simple countdown timer, I can see that the countdown timer works from the print statement but it just doesn't update the value in the label, I tried using QTimer but couldn't get to know how it works, how can I fix ? are there any better methods I can use ?

from  PyQt5.QtWidgets import *
from PyQt5.QtGui import QFont
from PyQt5.QtCore import *
import sys 
import time

class Window(QMainWindow) : 
    def __init__(self) : 
        super().__init__()
        self.setWindowTitle("Countdown Timer")
        self.setGeometry(0,0,500,500)
        self.Widgets()
        
    def Widgets(self) : 
        timer = 0
        #timer countdown Label
        self.Tcountdown = QLabel(str(timer),self)
        self.Tcountdown.resize(50,30)
        self.Tcountdown.move(250,90)
        self.Tcountdown.setFont(QFont("Arial",20))
        #Buttons : 
        self.startB = QPushButton(self)
        self.startB.resize(100,100)
        self.startB.move(250,200)
        self.startB.setText("Start")
        self.startB.clicked.connect(self.StartTimer)
        self.show()
        #the Pyqt Timer
    def StartTimer(self) : 
        for i in range(15,0,-1) : 
            timer = i 
            print(i)
            self.Tcountdown.setText(str(timer))
            time.sleep(1)
        
    

        
app = QApplication(sys.argv) 
window = Window()
app.exit(app.exec_()) 
Madjid
  • 85
  • 1
  • 1
  • 5

0 Answers0