PyQt5实现仿QQ贴边隐藏功能的实例代码


Posted in Python onMay 24, 2020

此程序大致功能为:可变换颜色,贴边隐藏。

变换颜色思路

QPalette( [ˈpælət] 调色板)类相当于对话框或控件的调色板,它管理着控件或窗体的所有颜色信息,每个窗体或控件都包含一个QPalette对象,在显示时按照它的QPalette对象中对各部分各状态下的颜色的描述来进行绘制。

实现代码

def Painting(self):
 color = random.choice(["CCFFFF","CC6699","CC99FF","99CCFF"])
 palette1 = QPalette()
 palette1.setColor(self.backgroundRole(),
    QColor("#{}".format(color))) # 改变窗体颜色
 self.setPalette(palette1)

贴边隐藏思路

可以判断窗口的位置,当与边缘的距离小于某值时,再判断鼠标是否在窗口,判断是否隐藏窗口;
根据隐藏窗口的隐藏位置,获得某块区域,当鼠标在这个位置时,显示窗口。

实现代码

鼠标进入事件,调用hide_or_show判断是否该显示

def enterEvent(self, event):
 self.hide_or_show('show', event)

鼠标离开事件,调用hide_or_show判断是否该隐藏

def leaveEvent(self, event):
 self.hide_or_show('hide', event)

鼠标点击事件

def mousePressEvent(self, event):
 if event.button() == Qt.LeftButton:
  self.dragPosition = event.globalPos() - self.frameGeometry(
  ).topLeft()
  QApplication.postEvent(self, QEvent(174))
  event.accept()

捕捉鼠标移动事件

def mouseMoveEvent(self, event):
 if event.buttons() == Qt.LeftButton:
  try:
  self.move(event.globalPos() - self.dragPosition)
  event.accept()
  except:pass

判断是否该隐藏

def hide_or_show(self, mode, event):
 pos = self.frameGeometry().topLeft()
 if mode == 'show' and self.moved:
  if pos.x() + WINDOW_WEIGHT >= SCREEN_WEIGHT: # 右侧显示
  self.startAnimation(SCREEN_WEIGHT - WINDOW_WEIGHT + 2, pos.y())
  event.accept()
  self.moved = False
  elif pos.x() <= 0: # 左侧显示
  self.startAnimation(0,pos.y())
  event.accept()
  self.moved = False
  elif pos.y() <= 0: # 顶层显示
  self.startAnimation(pos.x(),0)
  event.accept()
  self.moved = False
 elif mode == 'hide':
  if pos.x() + WINDOW_WEIGHT >= SCREEN_WEIGHT: # 右侧隐藏
  self.startAnimation(SCREEN_WEIGHT - 2,pos.y())
  event.accept()
  self.moved = True
  elif pos.x() <= 2: # 左侧隐藏
  self.startAnimation(2 - WINDOW_WEIGHT,pos.y())
  event.accept()
  self.moved = True
  elif pos.y() <= 2: # 顶层隐藏
  self.startAnimation(pos.x(),2 - WINDOW_HEIGHT)
  event.accept()
  self.moved = True

将划入划出作为属性动画

def startAnimation(self,width,height):
 animation = QPropertyAnimation(self,b"geometry",self)
 startpos = self.geometry()
 animation.setDuration(200)
 newpos = QRect(width,height,startpos.width(),startpos.height())
 animation.setEndValue(newpos)
 animation.start()

完整代码

import sys,random
from PyQt5.QtGui import QPalette,QColor
from PyQt5.QtWidgets import QWidget,QVBoxLayout,QPushButton,\
 QDesktopWidget,QApplication
from PyQt5.QtCore import Qt,QRect,QEvent,QPoint
from PyQt5.Qt import QCursor,QPropertyAnimation

SCREEN_WEIGHT = 1920
SCREEN_HEIGHT = 1080
WINDOW_WEIGHT = 300
WINDOW_HEIGHT = 600
class Ui_Form(QWidget):
 def __init__(self):
 self.moved = False
 super(Ui_Form,self).__init__()
 self.setupUi()
 self.resize(WINDOW_WEIGHT, WINDOW_HEIGHT)
 self.show()
 def setupUi(self):
 self.setWindowFlags(Qt.FramelessWindowHint
    | Qt.WindowStaysOnTopHint
    | Qt.Tool) # 去掉标题栏
 self.widget = QWidget()
 self.Layout = QVBoxLayout(self.widget)
 self.Layout.setContentsMargins(0,0,0,0)
 self.setLayout(self.Layout)
 self.setWindowFlag(Qt.Tool)
 self.main_widget = QWidget()
 self.Layout.addWidget(self.main_widget)
 self.paint = QPushButton(self.main_widget)
 self.paint.setText("改变颜色")
 self.paint.move(QPoint(120,200))
 self.paint.clicked.connect(self.Painting)
 self.exit = QPushButton(self.main_widget)
 self.exit.setText(" 退出 ")
 self.exit.move(QPoint(120,400))
 self.exit.clicked.connect(lambda:exit(0))
 self.setStyleSheet('''
  QPushButton {
  color: rgb(137, 221, 255);
  background-color: rgb(37, 121, 255);
  border-style:none;
  border:1px solid #3f3f3f;
  padding:5px;
  min-height:20px;
  border-radius:15px;
  }
  ''')
 def Painting(self):
 color = random.choice(["CCFFFF","CC6699","CC99FF","99CCFF"])
 palette1 = QPalette()
 palette1.setColor(self.backgroundRole(),
    QColor("#{}".format(color))) # 改变窗体颜色
 self.setPalette(palette1)
 def enterEvent(self, event):
 self.hide_or_show('show', event)
 def leaveEvent(self, event):
 self.hide_or_show('hide', event)
 def mousePressEvent(self, event):
 if event.button() == Qt.LeftButton:
  self.dragPosition = event.globalPos() - self.frameGeometry(
  ).topLeft()
  QApplication.postEvent(self, QEvent(174))
  event.accept()
 def mouseMoveEvent(self, event):
 if event.buttons() == Qt.LeftButton:
  try:
  self.move(event.globalPos() - self.dragPosition)
  event.accept()
  except:pass
 #def mouseReleaseEvent(self, event):
 #self.moved = True
 #self.hide_or_show('show', event)
 def hide_or_show(self, mode, event):
 pos = self.frameGeometry().topLeft()
 if mode == 'show' and self.moved:
  if pos.x() + WINDOW_WEIGHT >= SCREEN_WEIGHT: # 右侧显示
  self.startAnimation(SCREEN_WEIGHT - WINDOW_WEIGHT + 2, pos.y())
  event.accept()
  self.moved = False
  elif pos.x() <= 0: # 左侧显示
  self.startAnimation(0,pos.y())
  event.accept()
  self.moved = False
  elif pos.y() <= 0: # 顶层显示
  self.startAnimation(pos.x(),0)
  event.accept()
  self.moved = False
 elif mode == 'hide':
  if pos.x() + WINDOW_WEIGHT >= SCREEN_WEIGHT: # 右侧隐藏
  self.startAnimation(SCREEN_WEIGHT - 2,pos.y())
  event.accept()
  self.moved = True
  elif pos.x() <= 2: # 左侧隐藏
  self.startAnimation(2 - WINDOW_WEIGHT,pos.y())
  event.accept()
  self.moved = True
  elif pos.y() <= 2: # 顶层隐藏
  self.startAnimation(pos.x(),2 - WINDOW_HEIGHT)
  event.accept()
  self.moved = True
 def startAnimation(self,width,height):
 animation = QPropertyAnimation(self,b"geometry",self)
 startpos = self.geometry()
 animation.setDuration(200)
 newpos = QRect(width,height,startpos.width(),startpos.height())
 animation.setEndValue(newpos)
 animation.start()
if __name__ == "__main__":
 app = QApplication(sys.argv)
 ui = Ui_Form()
 sys.exit(app.exec_())

总结

到此这篇关于PyQt5实现仿QQ贴边隐藏功能的文章就介绍到这了,更多相关PyQt5实现隐藏内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python Sql数据库增删改查操作简单封装
Apr 18 Python
详解Python中 __get__和__getattr__和__getattribute__的区别
Jun 16 Python
Django使用httpresponse返回用户头像实例代码
Jan 26 Python
对numpy.append()里的axis的用法详解
Jun 28 Python
python3.6使用tkinter实现弹跳小球游戏
May 09 Python
pyqt5 comboBox获得下标、文本和事件选中函数的方法
Jun 14 Python
python @classmethod 的使用场合详解
Aug 23 Python
python sqlite的Row对象操作示例
Sep 11 Python
python3实现用turtle模块画一棵随机樱花树
Nov 21 Python
Python异步编程之协程任务的调度操作实例分析
Feb 01 Python
Python Selenium模块安装使用教程详解
Jul 09 Python
Python实现位图分割的效果
Nov 20 Python
通过Python扫描代码关键字并进行预警的实现方法
May 24 #Python
关于keras中keras.layers.merge的用法说明
May 23 #Python
使用keras2.0 将Merge层改为函数式
May 23 #Python
使用keras实现densenet和Xception的模型融合
May 23 #Python
在keras下实现多个模型的融合方式
May 23 #Python
Keras使用ImageNet上预训练的模型方式
May 23 #Python
使用Keras预训练模型ResNet50进行图像分类方式
May 23 #Python
You might like
php的XML文件解释类应用实例
2014/09/22 PHP
thinkphp实现上一篇与下一篇的方法
2014/12/08 PHP
PHP 实现的将图片转换为TXT
2015/10/21 PHP
PHP异常处理定义与使用方法分析
2017/07/25 PHP
php和nginx交互实例讲解
2019/09/24 PHP
jquery中EasyUI使用技巧小结
2015/02/10 Javascript
全面解析Bootstrap布局组件应用
2016/02/22 Javascript
js select实现省市区联动选择
2020/04/17 Javascript
BooStrap对导航条的改造实践小结
2016/09/21 Javascript
使用JS正则表达式 替换括号,尖括号等
2016/11/29 Javascript
JavaScript数组去重的6个方法
2017/01/21 Javascript
微信小程序手势操作之单触摸点与多触摸点
2017/03/10 Javascript
基于Vue实现页面切换左右滑动效果
2020/06/29 Javascript
基于vue 开发中出现警告问题去除方法
2018/01/25 Javascript
Electron + vue 打包桌面操作流程详解
2019/06/24 Javascript
turn.js异步加载实现翻书效果
2019/07/25 Javascript
vue全局使用axios的操作
2020/09/08 Javascript
Python运行报错UnicodeDecodeError的解决方法
2016/06/07 Python
11月编程语言排行榜 Python逆袭C#上升到第4
2017/11/15 Python
python flask实现分页的示例代码
2018/08/02 Python
python:接口间数据传递与调用方法
2018/12/17 Python
Python csv模块使用方法代码实例
2019/08/29 Python
python程序中的线程操作 concurrent模块使用详解
2019/09/23 Python
PyCharm永久激活方式(推荐)
2020/09/22 Python
python的flask框架难学吗
2020/07/31 Python
python爬虫使用正则爬取网站的实现
2020/08/03 Python
python的链表基础知识点
2020/09/13 Python
RIP版本1跟版本2的区别
2013/12/30 面试题
初中英语演讲稿
2014/04/29 职场文书
信用卡工资证明格式
2014/09/13 职场文书
税务职业生涯规划书范文
2014/09/16 职场文书
教师党员个人自我剖析材料
2014/09/29 职场文书
成绩单评语
2015/01/04 职场文书
2015年学校少先队工作总结
2015/07/20 职场文书
心理健康教育主题班会
2015/08/13 职场文书
python for循环赋值问题
2021/06/03 Python