Python Forum
Basic PyQt6 Example of a timer
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic PyQt6 Example of a timer
#1
from PyQt6.QtWidgets import (QApplication, QWidget, QLabel, QPushButton, QMessageBox, QGridLayout,
                             QMainWindow)
from PyQt6.QtCore import QTimer
import sys


class View(QMainWindow):
    def __init__(self):
        super().__init__()
        self.resize(300, 80)

        self.label = QLabel('Hi, I am a label')
        self.label.setStyleSheet('border: 1px solid black;')
        self.label.setMaximumHeight(25)
        self.label.setMinimumHeight(25)

        self.label2 = QLabel()
        self.label2.setStyleSheet('border: 1px solid black; background-color: lightyellow')
        self.label2.setMaximumHeight(25)
        self.label2.setMinimumHeight(25)
        self.label2.hide()

        self.button = QPushButton('Click Me')

        layout = QGridLayout()
        layout.addWidget(self.label, 0, 0, 1, 1)
        layout.addWidget(self.label2, 1, 0, 1, 1)
        layout.addWidget(self.button, 2, 0, 1, 1)

        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)


class Controller:
    def __init__(self, view):
        self.view = view
        self.duration = 5

        self.view.button.pressed.connect(self.show_msg)

    def show_msg(self):
        self.view.label2.setText(f'I am going to close in {self.duration} seconds.')
        self.view.label2.show()
        self.timer = QTimer()
        self.timer.timeout.connect(self.update)
        self.timer.start(1000)

        self.msg = QMessageBox()
        self.msg.setText(f'I am going to close in {self.duration} seconds.')
        self.msg.setIcon(QMessageBox.Icon.Information)
        self.msg.exec()

    def update(self):
        self.view.label2.setText(f'I am going to close in {self.duration} seconds.')
        self.msg.setText(f'I am going to close in {self.duration - 2} seconds.')
        self.duration -= 1

        box_dur = self.duration - 2
        if box_dur < 0:
            self.msg.close()

        if self.duration < 0:
            self.timer.stop()
            self.view.label2.hide()
            self.duration = 5

    def hideit(self, arg):
        arg.hide()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    controller = Controller(View())
    controller.view.show()
    sys.exit(app.exec())
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
Basic PyQt6 Example of a timer - by menator01 - May-27-2022, 05:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pyqt6 and random emoji menator01 0 2,396 Sep-19-2022, 06:46 PM
Last Post: menator01
  PyQt6 Version of weather app menator01 3 3,311 Jan-30-2022, 12:32 AM
Last Post: menator01

Forum Jump:

User Panel Messages

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