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 相关文章推荐
python3序列化与反序列化用法实例
May 26 Python
Python开发的HTTP库requests详解
Aug 29 Python
Python 实现网页自动截图的示例讲解
May 17 Python
python高效过滤出文件夹下指定文件名结尾的文件实例
Oct 21 Python
Django ORM多对多查询方法(自定义第三张表&amp;ManyToManyField)
Aug 09 Python
python元组的概念知识点
Nov 19 Python
QML用PathView实现轮播图
Jun 03 Python
python3.6.8 + pycharm + PyQt5 环境搭建的图文教程
Jun 11 Python
python字典的值可以修改吗
Jun 29 Python
Python3 pyecharts生成Html文件柱状图及折线图代码实例
Sep 29 Python
用python写PDF转换器的实现
Oct 29 Python
python爬取youtube视频的示例代码
Mar 03 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 程式大小
2006/12/06 PHP
Godaddy空间Zend Optimizer升级方法
2010/05/10 PHP
PHP根据IP地址获取所在城市具体实现
2013/11/27 PHP
PHP pthreads v3使用中的一些坑和注意点分析
2020/02/21 PHP
javascript arguments 传递给函数的隐含参数
2009/08/21 Javascript
JavaScript 组件之旅(四):测试 JavaScript 组件
2009/10/28 Javascript
jQuery 扩展对input的一些操作方法
2009/10/30 Javascript
基于prototype扩展的JavaScript常用函数库
2010/11/30 Javascript
js取两个数组的交集|差集|并集|补集|去重示例代码
2013/08/07 Javascript
JS实现切换标签页效果实例代码
2013/11/01 Javascript
js实现网页倒计时、网站已运行时间功能的代码3例
2014/04/14 Javascript
JS实用的带停顿的逐行文本循环滚动效果实例
2016/11/23 Javascript
Bootstrap 轮播(Carousel)插件
2016/12/26 Javascript
JavaScript实现向select下拉框中添加和删除元素的方法
2017/03/07 Javascript
详细分析jsonp的原理和实现方式
2017/11/20 Javascript
webpack4与babel配合使es6代码可运行于低版本浏览器的方法
2018/10/12 Javascript
JS div匀速移动动画与变速移动动画代码实例
2019/03/26 Javascript
详解vue-element Tree树形控件填坑路
2019/03/26 Javascript
vue-element-admin 菜单标签失效的解决方式
2019/11/12 Javascript
JavaScript如何实现防止重复的网络请求的示例
2021/01/28 Javascript
Python3使用PyQt5制作简单的画板/手写板实例
2017/10/19 Python
Python实现的字典值比较功能示例
2018/01/08 Python
python的移位操作实现详解
2019/08/21 Python
Python pip 安装与使用(安装、更新、删除)
2019/10/06 Python
Python +Selenium解决图片验证码登录或注册问题(推荐)
2020/02/09 Python
python 多线程死锁问题的解决方案
2020/08/25 Python
Python如何获取文件路径/目录
2020/09/22 Python
求职信模板怎么做
2014/01/26 职场文书
社区文艺活动方案
2014/08/19 职场文书
班子四风对照检查材料思想汇报
2014/09/29 职场文书
毕业生就业推荐表导师评语
2014/12/31 职场文书
总经理检讨书范文
2015/02/16 职场文书
秋菊打官司观后感
2015/06/03 职场文书
学生会工作感言
2015/08/07 职场文书
幼儿园中班教育随笔
2015/08/14 职场文书
pandas中DataFrame数据合并连接(merge、join、concat)
2021/05/30 Python