PyQt5實(shí)現(xiàn)仿QQ貼邊隱藏功能的實(shí)例代碼
此程序大致功能為:可變換顏色,貼邊隱藏。
變換顏色思路
QPalette( [ˈpælət] 調(diào)色板)類相當(dāng)于對話框或控件的調(diào)色板,它管理著控件或窗體的所有顏色信息,每個窗體或控件都包含一個QPalette對象,在顯示時按照它的QPalette對象中對各部分各狀態(tài)下的顏色的描述來進(jìn)行繪制。
實(shí)現(xiàn)代碼
def Painting(self): color = random.choice(["CCFFFF","CC6699","CC99FF","99CCFF"]) palette1 = QPalette() palette1.setColor(self.backgroundRole(), QColor("#{}".format(color))) # 改變窗體顏色 self.setPalette(palette1)
貼邊隱藏思路
可以判斷窗口的位置,當(dāng)與邊緣的距離小于某值時,再判斷鼠標(biāo)是否在窗口,判斷是否隱藏窗口;
根據(jù)隱藏窗口的隱藏位置,獲得某塊區(qū)域,當(dāng)鼠標(biāo)在這個位置時,顯示窗口。
實(shí)現(xiàn)代碼
鼠標(biāo)進(jìn)入事件,調(diào)用hide_or_show判斷是否該顯示
def enterEvent(self, event): self.hide_or_show('show', event)
鼠標(biāo)離開事件,調(diào)用hide_or_show判斷是否該隱藏
def leaveEvent(self, event): self.hide_or_show('hide', event)
鼠標(biāo)點(diǎn)擊事件
def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self.dragPosition = event.globalPos() - self.frameGeometry( ).topLeft() QApplication.postEvent(self, QEvent(174)) event.accept()
捕捉鼠標(biāo)移動事件
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: # 右側(cè)顯示 self.startAnimation(SCREEN_WEIGHT - WINDOW_WEIGHT + 2, pos.y()) event.accept() self.moved = False elif pos.x() <= 0: # 左側(cè)顯示 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: # 右側(cè)隱藏 self.startAnimation(SCREEN_WEIGHT - 2,pos.y()) event.accept() self.moved = True elif pos.x() <= 2: # 左側(cè)隱藏 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) # 去掉標(biāo)題欄 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: # 右側(cè)顯示 self.startAnimation(SCREEN_WEIGHT - WINDOW_WEIGHT + 2, pos.y()) event.accept() self.moved = False elif pos.x() <= 0: # 左側(cè)顯示 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: # 右側(cè)隱藏 self.startAnimation(SCREEN_WEIGHT - 2,pos.y()) event.accept() self.moved = True elif pos.x() <= 2: # 左側(cè)隱藏 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_())
總結(jié)
到此這篇關(guān)于PyQt5實(shí)現(xiàn)仿QQ貼邊隱藏功能的文章就介紹到這了,更多相關(guān)PyQt5實(shí)現(xiàn)隱藏內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)socket庫網(wǎng)絡(luò)通信套接字
socket又叫套接字,實(shí)現(xiàn)網(wǎng)絡(luò)通信的兩端就是套接字。分為服務(wù)器對應(yīng)的套接字和客戶端對應(yīng)的套接字,本文給大家介紹Python實(shí)現(xiàn)socket庫網(wǎng)絡(luò)通信套接字的相關(guān)知識,包括套接字的基本概念,感興趣的朋友跟隨小編一起看看吧2021-06-06Python語言的面相對象編程方式初步學(xué)習(xí)
這篇文章主要介紹Python語言的面相對象編程方式的初步學(xué)習(xí),包括類和對象以及繼承特性等知識,需要的朋友可以參考下2016-03-03小白入門篇使用Python搭建點(diǎn)擊率預(yù)估模型
本文將從零開始,僅僅利用基礎(chǔ)的numpy庫,使用Python實(shí)現(xiàn)一個最簡單的神經(jīng)網(wǎng)絡(luò)(或者說是簡易的LR,因?yàn)長R就是一個單層的神經(jīng)網(wǎng)絡(luò)),解決一個點(diǎn)擊率預(yù)估的問題。感興趣的朋友跟隨小白一起看看吧2018-10-10Flask如何獲取用戶的ip,查詢用戶的登錄次數(shù),并且封ip
這篇文章主要介紹了Flask如何獲取用戶的ip,查詢用戶的登錄次數(shù),并且封ip問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01在Python中使用MongoEngine操作數(shù)據(jù)庫教程實(shí)例
這篇文章主要介紹了在Python中使用MongoEngine操作數(shù)據(jù)庫教程實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12