时间:2021-05-22
此程序大致功能为:可变换颜色,贴边隐藏。
变换颜色思路
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,randomfrom PyQt5.QtGui import QPalette,QColorfrom PyQt5.QtWidgets import QWidget,QVBoxLayout,QPushButton,\ QDesktopWidget,QApplicationfrom PyQt5.QtCore import Qt,QRect,QEvent,QPointfrom PyQt5.Qt import QCursor,QPropertyAnimationSCREEN_WEIGHT = 1920SCREEN_HEIGHT = 1080WINDOW_WEIGHT = 300WINDOW_HEIGHT = 600class 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实现隐藏内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了PyQt5实现简单计算器的具体代码,供大家参考,具体内容如下下面我们将介绍使用python的PyQt5图形界面来编写一个简易的计算器,实现“
本文实例为大家分享了PyQt5实现登录页面的具体代码,供大家参考,具体内容如下效果图:python代码:importsysfromPyQt5.QtCoreimp
PyQt5不规则窗口实现动画效果实例importsysfromPyQt5.QtCoreimport*fromPyQt5.QtGuiimport*fromPyQt
在pyqt5中要做到自适应布局,必须应用Layout类下面列出类似于html中float功能的布局方法:实现原理:PyQt5中的布局中,stretch属性类似于
PyCharm不识别PyQt5的问题如图所示,引用PyQt5的时候显示错误“ModuleNotFoundError:Nomodulenamed'pyqt5'”首