Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pyqt5
#1
is there any way to use timer object to set text in pyqt5 with out creating another function to get the time to upsate or is there any alternative way to update the text set in a label

 
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
from PyQt5.QtWidgets import QVBoxLayout, QLabel
from PyQt5.QtGui import QFont
from PyQt5.QtCore import QTimer, QTime, Qt
from PyQt5 import QtCore


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.build_time()
        self.show()
        self.showFullScreen()
        self.setStyleSheet("background-color: black;")

    def keyPressEvent(self, e):
        if e.key() == QtCore.Qt.Key_Escape:
            self.close()
        if e.key() == QtCore.Qt.Key_F11:
            if self.isMaximized():
                self.showNormal()
            else:
                self.showMaximized()

    font = "Arial"
    color = "antiquewhite"
    style_sheet = f"color: {color} ;"
   


    def build_time(self):
        self.time = QLabel(self)
        self.time.setGeometry(0, 85, 400, 75)
        self.time.setStyleSheet(self.style_sheet)
        self.time.setFont(QFont(self.font, 60))

        timer = QTimer(self)
        # adding action to timer
        timer.timeout.connect(self.showTime)
        # update the timer every second
        timer.start(1000)

    def showTime(self):
        # getting current time
        current_time = QTime.currentTime()
        # converting QTime object to string
        label_time = current_time.toString('hh:mm:ss')
        # showing it to the label
        self.time.setText(label_time)

    # create pyqt5 app


App = QApplication(sys.argv)

# create the instance of our Window
window = Window()

# showing all the widgets
window.show()

# start the app
App.exit(App.exec_())
buran write Jan-12-2021, 05:00 AM:
Please, post in correct sub-forum. This time I moved it to GUI.
Reply
#2
 
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
from PyQt5.QtWidgets import QVBoxLayout, QLabel
from PyQt5.QtGui import QFont
from PyQt5.QtCore import QTimer, QTime, Qt
from PyQt5 import QtCore


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.timerId = -1    
        self.build_time()
        self.show()
        self.showFullScreen()
        self.setStyleSheet("background-color: black;")

    def keyPressEvent(self, e):
        if e.key() == QtCore.Qt.Key_Escape:
            self.close()
        if e.key() == QtCore.Qt.Key_F11:
            if self.isMaximized():
                self.showNormal()
            else:
                self.showMaximized()

    font = "Arial"
    color = "antiquewhite"
    style_sheet = f"color: {color} ;"
   


    def build_time(self):
        self.time = QLabel(self)
        self.time.setGeometry(0, 85, 400, 75)
        self.time.setStyleSheet(self.style_sheet)
        self.time.setFont(QFont(self.font, 60))
        self.timerId = self.startTimer(100)

    def timerEvent(self, te):
        if te.timerId() != self.timerId:
            return
        # getting current time
        current_time = QTime.currentTime()
        # converting QTime object to string
        label_time = current_time.toString('hh:mm:ss')
        # showing it to the label
        self.time.setText(label_time)

    # create pyqt5 app


App = QApplication(sys.argv)

# create the instance of our Window
window = Window()

# showing all the widgets
window.show()

# start the app
App.exit(App.exec_())
Reply
#3
There are always alternatives, but in PyQt is probably the easiest. In this example I use a timer to update the stopwatch display.
from PySide2.QtWidgets import QWidget, QLabel, QPushButton, QGridLayout, QApplication
 
class Stopwatch(QWidget):
 
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
 
        self.timer = QTimer()
        self.timer.timeout.connect(self.update_time)
        
        self.time_display = QLabel(self, text='00:00:00.00')
        self.time_display.setStyleSheet('QLabel {background-color: white; font-size: 50pt;}')
        
        self.start_button = QPushButton(self, text='Start')
        self.start_button.setStyleSheet('QPushButton {background-color: yellow; font-size: 20pt;}')
        self.start_button.clicked.connect(self.start_timer)
        
        self.layout = QGridLayout(self)
        self.layout.addWidget(self.time_display, 0, 0)
        self.layout.addWidget(self.start_button, 1, 0)
          
    def update_time(self):
        """Increment counter and update time display"""
        # Calculate hours:minutes:seconds
        rem = time.time() - self.start_time
        rem, h = math.modf(rem / 3600)
        rem, m = math.modf(rem * 3600 / 60)
        s = rem * 60
        self.time_display.setText(f'{int(h):02d}:{int(m):02d}:{s:05.2f}')
 
    def start_timer(self):
        """Start/Stop the timer"""
        if self.start_button.text() == 'Start':
            self.start_time = time.time()
            self.start_button.setText('Stop')
            self.timer.start(10)
        else:
            self.timer.stop()
            self.start_button.setText('Start')
  
app = QApplication(sys.argv)
win = Stopwatch()
win.setWindowTitle('Stopwatch')
win.show()
sys.exit(app.exec_())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,823 Apr-06-2019, 11:15 PM
Last Post: ZenWoR

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020