此程序大致功能为:可变换颜色,贴边隐藏。
变换颜色思路
QPalette( ["htmlcode">
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_())
总结
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 黄乙玲1988-无稳定的爱心肝乱糟糟[日本东芝1M版][WAV+CUE]
- 群星《我们的歌第六季 第3期》[320K/MP3][70.68MB]
- 群星《我们的歌第六季 第3期》[FLAC/分轨][369.48MB]
- 群星《燃!沙排少女 影视原声带》[320K/MP3][175.61MB]
- 乱斗海盗瞎6胜卡组推荐一览 深暗领域乱斗海盗瞎卡组分享
- 炉石传说乱斗6胜卡组分享一览 深暗领域乱斗6胜卡组代码推荐
- 炉石传说乱斗本周卡组合集 乱斗模式卡组最新推荐
- 佟妍.2015-七窍玲珑心【万马旦】【WAV+CUE】
- 叶振棠陈晓慧.1986-龙的心·俘虏你(2006复黑限量版)【永恒】【WAV+CUE】
- 陈慧琳.1998-爱我不爱(国)【福茂】【WAV+CUE】
- 咪咕快游豪礼放送,百元京东卡、海量欢乐豆就在咪咕咪粉节!
- 双11百吋大屏焕新“热”,海信AI画质电视成最大赢家
- 海信电视E8N Ultra:真正的百吋,不止是大!
- 曾庆瑜1990-曾庆瑜历年精选[派森][WAV+CUE]
- 叶玉卿1999-深情之选[飞图][WAV+CUE]