highlight1()- 第二次就不行了,我该如何解决?
import sys
from time import sleep
from PySide6.QtWidgets import QMainWindow, QApplication
from PySide6.QtGui import QPainter, QColor, QPen
from PySide6.QtCore import Qt, QTimer
class Highlight(QMainWindow):
def __new__(cls, *args, **kwargs):
return super().__new__(cls)
def __init__(self, x=0, y=0, w=500, h=500, color=Qt.red, thickness=2):
super(Highlight, self).__init__()
self.x = x
self.y = y
self.w = w
self.h = h
self.reg = self.x, self.y, self.w, self.h
self.setAttribute(Qt.WA_NoSystemBackground, True)
self.setAttribute(Qt.WA_TranslucentBackground, True)
self.setStyleSheet("background:transparent;")
self.setWindowFlags(Qt.FramelessWindowHint)
self.outside_color = color
self.thickness = thickness
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
painter.setPen(QPen(self.outside_color, self.thickness, Qt.SolidLine))
trans = QColor(225, 100, 233)
trans.setAlphaF(0.1)
painter.setBrush(trans)
painter.drawRect(*self.reg)
painter.end()
def highlight1(reg, dur=2000, color=Qt.red):
app1 = QApplication(sys.argv)
window = Highlight(*reg, color=color)
window.showFullScreen()
QTimer.singleShot(dur, window.close)
sys.exit(app1.exec())
if __name__ == '__main__':
highlight1((10, 100, 600, 500), 3000, Qt.green)
sleep(1)
highlight1((50, 200, 500, 400), 3000, Qt.black)
sleep(5)
