K
K
kramick2021-07-27 19:51:21
PyQt
kramick, 2021-07-27 19:51:21

How to update a PyQt application?

I'm starting to develop an application for VK on PyQt5. Created a simple window with the number of messages. I want the application to refresh every second and check if I have new messages. How can I do it? Here is the source code:

import sys

from PyQt5 import QtWidgets
from PyQt5.QtCore import QTimer
import playsound

from core import count_messages
from GUI import Ui_MainWindow


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.update_message

    def update_message(self):
        unread = count_messages()
        old = 0

        if unread - old > 0:
            playsound.playsound('new_message.mp3')
            old = unread
        else:
            old = unread

        self.messages.setText(f'У вас {unread} непрочитанных сообщений!')

def start_app(name_class):
    app = QtWidgets.QApplication(sys.argv)
    window = name_class()
    window.show()
    app.exec_()


if __name__ == '__main__':
    start_app(MainWindow)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bbkmzzzz, 2021-07-28
@kramick

QTimer to the rescue

U
UberPool, 2021-07-28
@UberPool

In a loop with N delay, access the VK API and, depending on the result, update the application.
What I found .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question