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中使用CasperJS获取JS渲染生成的HTML内容的教程
Apr 09 Python
Python中一些自然语言工具的使用的入门教程
Apr 13 Python
Python模拟鼠标点击实现方法(将通过实例自动化模拟在360浏览器中自动搜索python)
Aug 23 Python
Python输入二维数组方法
Apr 13 Python
Django框架实现的简单分页功能示例
Dec 04 Python
django如何通过类视图使用装饰器
Jul 24 Python
Pytorch反向求导更新网络参数的方法
Aug 17 Python
Golang GBK转UTF-8的例子
Aug 26 Python
python numpy中cumsum的用法详解
Oct 17 Python
win10从零安装配置pytorch全过程图文详解
May 08 Python
python中查看.db文件中表格的名字及表格中的字段操作
Jul 07 Python
Python包资源下载路径报404解决方案
Nov 05 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/11/28 PHP
不支持fsockopen但支持culr环境下下ucenter与modoer通讯问题
2011/08/12 PHP
php中常用字符串处理代码片段整理
2011/11/07 PHP
Apache服务器下防止图片盗链的办法
2015/07/06 PHP
Thinkphp5 如何隐藏入口文件index.php(URL重写)
2019/10/16 PHP
javascript同步Import,同步调用外部js的方法
2008/07/08 Javascript
javascript 禁止复制网页
2009/06/11 Javascript
javascript Object与Function使用
2010/01/11 Javascript
jquery对象和DOM对象的任意相互转换
2016/02/21 Javascript
js控制TR的显示隐藏
2016/03/04 Javascript
分享javascript实现的冒泡排序代码并优化
2016/06/05 Javascript
多功能jQuery树插件zTree实现权限列表简单实例
2016/07/12 Javascript
移动端翻页插件dropload.js(支持Zepto和jQuery)
2016/07/27 Javascript
angular基于路由控制ui-router实现系统权限控制
2016/09/27 Javascript
浅谈Vue.js中ref ($refs)用法举例总结
2017/12/19 Javascript
Python反射用法实例简析
2017/12/22 Python
Python实现的rsa加密算法详解
2018/01/24 Python
利用python将图片版PDF转文字版PDF
2019/05/03 Python
Python利用matplotlib绘制约数个数统计图示例
2019/11/26 Python
使用TFRecord存取多个数据案例
2020/02/17 Python
Pycharm导入anaconda环境的教程图解
2020/07/31 Python
python利用xpath爬取网上数据并存储到django模型中
2021/02/26 Python
阿迪达斯印度官方商城:adidas India
2017/03/26 全球购物
写好自荐信要注意的问题
2013/11/10 职场文书
《寓言两则》教学反思
2014/02/27 职场文书
材料员岗位职责
2014/03/13 职场文书
报关报检委托书
2014/04/08 职场文书
产品陈列协议书(标准版)
2014/09/17 职场文书
六查六看自检自查剖析材料
2014/10/14 职场文书
债务授权委托书范本
2014/10/17 职场文书
社会实践心得体会范文
2016/01/14 职场文书
志愿者工作心得体会
2016/01/15 职场文书
如何判断微信付款码和支付宝付款码
2021/04/01 PHP
一篇文章看懂MySQL主从复制与读写分离
2021/11/07 MySQL
交互式可视化js库gojs使用介绍及技巧
2022/02/18 Javascript
Python开发五子棋小游戏
2022/04/28 Python