I’m working on a PyQt5 project where I want to create a “glitch” window that displays images with a glitch effect. The window should stay on top of all other windows, including the taskbar and any windows that appear when I use Alt-Tab. However, I’m encountering an issue where the window doesn’t stay on top of the taskbar and Alt-Tab windows.
Here’s the code I’m using:
import sys
import random
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QPainter, QPixmap
from PyQt5.QtCore import Qt, QTimer
import time
import os
class GlitchWindow(QMainWindow):
def __init__(self, image_paths):
super().__init__()
self.setWindowFlags(Qt.Window | Qt.SplashScreen | Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setGeometry(0, 0, QApplication.desktop().width(), QApplication.desktop().height())
self.timer = QTimer(self)
self.timer.timeout.connect(self.update)
self.timer.start(50) # Update every 50ms
self.glitch_duration = 50000 # Glitch effect duration in milliseconds
# Load images
self.images = [QPixmap(image_path) for image_path in image_paths]
# Stop the glitch effect after a short duration
QTimer.singleShot(self.glitch_duration, self.close)
def paintEvent(self, event):
painter = QPainter(self)
for _ in range(2): # Adjust the number of images painted per frame
image = self.images[_]
middleW = (self.width()-image.width())//2+800
middleH = (self.height()-image.height())//2+400
if not _:
x = random.randint(middleW-2, middleW+2)
y = random.randint(middleH-2, middleH-2)
else:
x = random.choice([random.randint(middleW-400,middleW-200),random.randint(middleW+200,middleW+400)])
y = random.randint(middleH-400,middleH-300)
time.sleep(0.1)
painter.drawPixmap(x, y, image)
def show_glitch(image_paths):
app = QApplication(sys.argv)
glitch = GlitchWindow(image_paths)
glitch.show()
app.exec_()
if __name__ == "__main__":
image_paths = [
"Downloads/glitch.png",
"Downloads/glitches.png",
]
show_glitch(image_paths)
Despite setting the Qt.WindowStaysOnTopHint flag, the window does not stay on top of the Windows taskbar and the windows that appear when using Alt-Tab.
I’ve tried setting various window flags, such as Qt.SplashScreen and Qt.FramelessWindowHint.
Ensuring the window is set to stay on top with Qt.WindowStaysOnTopHint.
The window should remain on top of all other windows, including the taskbar and Alt-Tab windows, for the duration specified (50,000 milliseconds).
The window does not stay on top of the taskbar or the Alt-Tab windows, but it does stay on top of everything else.
I’m running this on Windows 10.
Using Python 3.8 and PyQt5.
Any help or suggestions would be greatly appreciated! I really appreciate any help you can provide.
Charlie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.