x = self.x() + random.choice([-1, 1]) * 100 # 在 x 方向上移动 10 个像素(移动速度更快)
y = self.y() + random.choice([-1, 1]) * 100 # 在 y 方向上移动 10 个像素(移动速度更快)
# 边界检测和反弹处理
if x < desktop_rect.left():
x = desktop_rect.left()
elif x + window_rect.width() > desktop_rect.right():
x = desktop_rect.right() - window_rect.width()
if y < desktop_rect.top():
y = desktop_rect.top()
elif y + window_rect.height() > desktop_rect.bottom():
y = desktop_rect.bottom() - window_rect.height()
self.move(x, y)
if __name__ == "__main__":
app = QApplication(sys.argv)
windows = []
for _ in range(300):
window = CustomWindow()
windows.append(window)
window.show()
使用道具 举报
点评
使用道具 举报
使用道具 举报
只要不学C++ 别的都好学 Java也不好整
使用道具 举报
使用道具 举报
import random
from PyQt5.QtCore import Qt, QRect, QTimer
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QDesktopWidget
class CustomWindow(QWidget):
def __init__(self):
super().__init__()
# 设置窗口属性为无法最小化和没有关闭按钮
self.setWindowFlags(
Qt.Window | Qt.CustomizeWindowHint | Qt.WindowCloseButtonHint)
self.setWindowFlags(
Qt.Window | Qt.CustomizeWindowHint | Qt.WindowCloseButtonHint)
self.setWindowTitle("Windows危险管家")
self.resize(1600, 315)
content_label = QLabel(self)
X = "你的电脑已经报废,重启也没用,不要说我们没有警告过你!别试了,关不掉的\n" * 100
content_label.setText(X)
font = content_label.font()
font.setPointSize(20)
content_label.setFont(font)
content_label.setAlignment(Qt.AlignCenter)
# 随机设置窗口的位置
desktop_rect = QDesktopWidget().availableGeometry()
window_rect = self.frameGeometry()
max_x = desktop_rect.width() - window_rect.width()
max_y = desktop_rect.height() - window_rect.height()
x = random.randint(0, max_x)
y = random.randint(0, max_y)
self.move(x, y)
# 设置定时器,每隔一段时间移动窗口
self.timer = QTimer()
self.timer.timeout.connect(self.moveWindow)
self.timer.start(1) # 修改定时器间隔为 50 毫秒(移动速度更快)
def moveWindow(self):
desktop_rect = QDesktopWidget().availableGeometry()
window_rect = self.frameGeometry()
x = self.x() + random.choice([-1, 1]) * 100 # 在 x 方向上移动 10 个像素(移动速度更快)
y = self.y() + random.choice([-1, 1]) * 100 # 在 y 方向上移动 10 个像素(移动速度更快)
# 边界检测和反弹处理
if x < desktop_rect.left():
x = desktop_rect.left()
elif x + window_rect.width() > desktop_rect.right():
x = desktop_rect.right() - window_rect.width()
if y < desktop_rect.top():
y = desktop_rect.top()
elif y + window_rect.height() > desktop_rect.bottom():
y = desktop_rect.bottom() - window_rect.height()
self.move(x, y)
if __name__ == "__main__":
app = QApplication(sys.argv)
windows = []
for _ in range(300):
window = CustomWindow()
windows.append(window)
window.show()
sys.exit(app.exec())
这是个小病毒,中配电脑就撑得住,不要把弹窗数量搞到1000以上!不然电脑会报废
使用道具 举报