import sys
import random
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QGuiApplication, QPixmap, QPainter
from PyQt5.QtWidgets import QApplication, QWidget
STRIPE_WIDTH = 10
SHIFT = 10
INTERVAL = 1000 # 1 секунда
class FloatingScreen(QWidget):
def __init__(self):
super().__init__()
# Получаем основной экран
screen = QGuiApplication.primaryScreen()
geometry = screen.geometry()
self.screen_width = geometry.width()
self.screen_height = geometry.height()
# Прозрачное окно без рамки
self.setGeometry(geometry)
self.setWindowFlags(
Qt.FramelessWindowHint |
Qt.WindowStaysOnTopHint |
Qt.Tool
)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setAttribute(Qt.WA_TransparentForMouseEvents)
# Таймер — один раз в секунду
self.timer = QTimer(self)
self.timer.timeout.connect(self.move_random_stripe)
self.timer.start(INTERVAL)
self.current_image = None
def move_random_stripe(self):
screen = QGuiApplication.primaryScreen()
# Делаем снимок экрана
screenshot = screen.grabWindow(
0,
0,
0,
self.screen_width,
self.screen_height
)
# Случайная вертикальная полоса
max_x = self.screen_width - STRIPE_WIDTH
x = random.randrange(0, max_x + 1, STRIPE_WIDTH)
# Случайное направление
direction = random.choice([-1, 1])
shift = direction * SHIFT
# Создаём новый кадр
result = QPixmap(screenshot.size())
result.fill(Qt.transparent)
painter = QPainter(result)
# Рисуем весь экран как есть
painter.drawPixmap(0, 0, screenshot)
# Вырезаем выбранную полосу
stripe = screenshot.copy(
x,
0,
STRIPE_WIDTH,
self.screen_height
)
# Закрываем старую полосу
# и вставляем её со смещением
painter.fillRect(
x,
0,
STRIPE_WIDTH,
self.screen_height,
Qt.transparent
)
painter.drawPixmap(
x,
shift,
stripe
)
painter.end()
self.current_image = result
self.update()
def paintEvent(self, event):
if self.current_image is None:
return
painter = QPainter(self)
painter.drawPixmap(0, 0, self.current_image)
painter.end()
if name == "__main__":
app = QApplication(sys.argv)
window = FloatingScreen()
window.show()
sys.exit(app.exec_())