PyQT5 Python 3.12 gif overlay

56 views Asked by At

There are problems using the QTimer function. In the class initialization after the timer ran, the window is hidden successfuly, but if I try to call it inside the show_window function, the window is not hidden.

import sys
import keyboard
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QMovie, QCursor
from PyQt5.Qt import QLabel, QSize

 
class Example(QWidget):
    def __init__(self):
        super().__init__()
        cursor_pos = QCursor().pos()
        screen_number = QApplication.desktop().screenNumber(cursor_pos)
        screen_rect = QApplication.desktop().availableGeometry(screen_number)
        w_width = screen_rect.width()
        w_height = screen_rect.height()
        print(w_width, w_height)

        keyboard.on_press(self.show_window)
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.hide)
        self.timer.start(5000)

        self.setWindowTitle('Gif_overlay')
        self.setGeometry(screen_rect.right() - w_height//4, screen_rect.bottom() - w_height//4,
                         w_height//4, w_height//4)
        self.setAttribute(Qt.WA_TranslucentBackground, True)
        # self.setWindowFlags(Qt.FramelessWindowHint)
        self.setWindowFlag(Qt.WindowStaysOnTopHint)

        self.label = QLabel(self)
        movie = QMovie("1.gif")
        gif_size = (w_height//4, w_height//4)
        movie.setScaledSize(QSize(*gif_size))
        self.label.resize(*gif_size)
        self.label.setMovie(movie)
        self.label.move(0, 0)
        movie.start()

    def show_window(self, key):
        if not (self.isVisible()):
            self.show()
            self.timer.start(5000)
        else:
            self.timer.setInterval(5000) 


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Example()
    w.show()
    #w.hide()
    sys.exit(app.exec_())

I use python 3.12, the library version below

Package            Version
------------------ ----------
certifi            2023.11.17
charset-normalizer 3.3.2
colorama           0.4.6
decorator          4.4.2
idna               3.6
imageio            2.33.0
imageio-ffmpeg     0.4.9
keyboard           0.13.5
moviepy            1.0.3
numpy              1.26.2
Pillow             9.5.0
pip                23.2.1
proglog            0.1.10
pygame             2.5.2
PyQt5              5.15.10
PyQt5-Qt5          5.15.2
PyQt5-sip          12.13.0
requests           2.31.0
setuptools         69.0.2
tqdm               4.66.1
urllib3            2.1.0

I want to put the gif on the screen when the user types on the keyboard and a minute after the stop, even outside the app window.

0

There are 0 answers