直接上代码,copy过去就能用
思路:
后台300ms检测一次窗口的位置
窗口Y方向坐标移到屏幕上界限附近,且鼠标移出窗口时。将窗口移出屏幕。
反过来检测窗口是否在屏幕外+鼠标是否在窗口坐标范围内,再将窗口移回。
同时获取电脑屏幕的长宽,将移出左右两侧的窗口拉回
#!/bin/env python # -*- coding:utf-8 -*- ''' Class: my_hide_or_show.py Programmer: tangzhiying Date: 2021-12-12 Purpose: demo for hide window ''' import numpy as np import sys import os import re import PyWinMouse as mouse #捕捉鼠标坐标 from PyQt5 import QtWidgets, QtGui, QtCore from PyQt5.QtCore import Qt, QTimer from PyQt5.QtWidgets import qApp from PyQt5.QtWidgets import QApplication, QMainWindow import time class Demo(QtWidgets.QMainWindow): def __init__(self, parent=None): super().__init__(parent) self.setFixedSize(self.width(), self.height()) self._init_timer() #定时器 self.mouse = mouse.Mouse() #捕捉 self._get_screen_rect() def _get_screen_rect(self): self.desktop = QApplication.desktop() self.screen_rect = self.desktop.screenGeometry() self.height = self.screen_rect.height() self.width = self.screen_rect.width() def _init_timer(self): self.timer = QTimer() self.timer.setInterval(300) #0.3秒触发一次 self.timer.start() self.timer.timeout.connect(self._hide_or_show) def _hide_or_show(self): start_x = self.x() start_y = self.y() current_height = self.frameGeometry().height() current_width = self.frameGeometry().width() end_x = start_x + current_width end_y = start_y + current_height mouse_x = self.mouse.get_mouse_pos()[0] mouse_y = self.mouse.get_mouse_pos()[1] screen_x = self.width screen_y = self.height if mouse_x >= start_x and mouse_x <= end_x and mouse_y >= start_y and mouse_y <= end_y: #判断鼠标是否在框架内 mouse_in = 1 else: mouse_in = 0 if start_x <= 0: #靠边时将窗口拉回 self.move(0, start_y) elif start_x >= screen_x - current_width: self.move(screen_x - current_width, start_y) if start_y >= -5 and start_y <= 5 and mouse_in == 0: #满足条件则将窗口移出当前屏幕 self.move(start_x, -current_height + 10) elif start_y == -current_height + 10 and mouse_in == 1: self.move(start_x, 0) def main(): app = QApplication(sys.argv) window = Demo() window.show() app.exec_() if __name__ == "__main__": main()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)