最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PyQt5實現(xiàn)無邊框窗口的標(biāo)題拖動和窗口縮放

 更新時間:2018年04月19日 16:50:16   作者:專注劃水  
這篇文章主要為大家詳細介紹了PyQt5實現(xiàn)無邊框窗口的標(biāo)題拖動和窗口縮放,具有一定的參考價值,感興趣的小伙伴們可以參考一下

網(wǎng)上找了半天都找不到好用的PyQt5無邊框窗口的實現(xiàn),借鑒部分前輩的窗口拖放代碼,自己實現(xiàn)了一下無邊框窗口,問題可能還有一點,慢慢改吧

先做個筆記

py文件

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt5.QtWidgets import QWidget, QLabel, QPushButton, QVBoxLayout
from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtGui import QFont, QCursor

class QTitleLabel(QLabel):
  """
  新建標(biāo)題欄標(biāo)簽類
  """
  def __init__(self, *args):
    super(QTitleLabel, self).__init__(*args)
    self.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
    self.setFixedHeight(30)

class QTitleButton(QPushButton):
  """
  新建標(biāo)題欄按鈕類
  """
  def __init__(self, *args):
    super(QTitleButton, self).__init__(*args)
    self.setFont(QFont("Webdings")) # 特殊字體以不借助圖片實現(xiàn)最小化最大化和關(guān)閉按鈕
    self.setFixedWidth(40)

class QUnFrameWindow(QWidget):
  """
  無邊框窗口類
  """
  def __init__(self):
    super(QUnFrameWindow, self).__init__(None, Qt.FramelessWindowHint) # 設(shè)置為頂級窗口,無邊框
    self._padding = 5 # 設(shè)置邊界寬度為5
    self.initTitleLabel() # 安放標(biāo)題欄標(biāo)簽
    self.setWindowTitle = self._setTitleText(self.setWindowTitle) # 用裝飾器將設(shè)置WindowTitle名字函數(shù)共享到標(biāo)題欄標(biāo)簽上
    self.setWindowTitle("UnFrameWindow")
    self.initLayout() # 設(shè)置框架布局
    self.setMinimumWidth(250)
    self.setMouseTracking(True) # 設(shè)置widget鼠標(biāo)跟蹤
    self.initDrag() # 設(shè)置鼠標(biāo)跟蹤判斷默認值

  def initDrag(self):
    # 設(shè)置鼠標(biāo)跟蹤判斷扳機默認值
    self._move_drag = False
    self._corner_drag = False
    self._bottom_drag = False
    self._right_drag = False

  def initTitleLabel(self):
    # 安放標(biāo)題欄標(biāo)簽
    self._TitleLabel = QTitleLabel(self)
    self._TitleLabel.setMouseTracking(True) # 設(shè)置標(biāo)題欄標(biāo)簽鼠標(biāo)跟蹤(如不設(shè),則標(biāo)題欄內(nèi)在widget上層,無法實現(xiàn)跟蹤)
    self._TitleLabel.setIndent(10) # 設(shè)置標(biāo)題欄文本縮進
    self._TitleLabel.move(0, 0) # 標(biāo)題欄安放到左上角

  def initLayout(self):
    # 設(shè)置框架布局
    self._MainLayout = QVBoxLayout()
    self._MainLayout.setSpacing(0)
    self._MainLayout.addWidget(QLabel(), Qt.AlignLeft) # 頂一個QLabel在豎放框架第一行,以免正常內(nèi)容擠占到標(biāo)題范圍里
    self._MainLayout.addStretch()
    self.setLayout(self._MainLayout)

  def addLayout(self, QLayout):
    # 給widget定義一個addLayout函數(shù),以實現(xiàn)往豎放框架的正確內(nèi)容區(qū)內(nèi)嵌套Layout框架
    self._MainLayout.addLayout(QLayout)

  def _setTitleText(self, func):
    # 設(shè)置標(biāo)題欄標(biāo)簽的裝飾器函數(shù)
    def wrapper(*args):
      self._TitleLabel.setText(*args)
      return func(*args)
    return wrapper

  def setTitleAlignment(self, alignment):
    # 給widget定義一個setTitleAlignment函數(shù),以實現(xiàn)標(biāo)題欄標(biāo)簽的對齊方式設(shè)定
    self._TitleLabel.setAlignment(alignment | Qt.AlignVCenter)

  def setCloseButton(self, bool):
    # 給widget定義一個setCloseButton函數(shù),為True時設(shè)置一個關(guān)閉按鈕
    if bool == True:
      self._CloseButton = QTitleButton(b'\xef\x81\xb2'.decode("utf-8"), self)
      self._CloseButton.setObjectName("CloseButton") # 設(shè)置按鈕的ObjectName以在qss樣式表內(nèi)定義不同的按鈕樣式
      self._CloseButton.setToolTip("關(guān)閉窗口")
      self._CloseButton.setMouseTracking(True) # 設(shè)置按鈕鼠標(biāo)跟蹤(如不設(shè),則按鈕在widget上層,無法實現(xiàn)跟蹤)
      self._CloseButton.setFixedHeight(self._TitleLabel.height()) # 設(shè)置按鈕高度為標(biāo)題欄高度
      self._CloseButton.clicked.connect(self.close) # 按鈕信號連接到關(guān)閉窗口的槽函數(shù)


  def setMinMaxButtons(self, bool):
    # 給widget定義一個setMinMaxButtons函數(shù),為True時設(shè)置一組最小化最大化按鈕
    if bool == True:
      self._MinimumButton = QTitleButton(b'\xef\x80\xb0'.decode("utf-8"), self)
      self._MinimumButton.setObjectName("MinMaxButton") # 設(shè)置按鈕的ObjectName以在qss樣式表內(nèi)定義不同的按鈕樣式
      self._MinimumButton.setToolTip("最小化")
      self._MinimumButton.setMouseTracking(True) # 設(shè)置按鈕鼠標(biāo)跟蹤(如不設(shè),則按鈕在widget上層,無法實現(xiàn)跟蹤)
      self._MinimumButton.setFixedHeight(self._TitleLabel.height()) # 設(shè)置按鈕高度為標(biāo)題欄高度
      self._MinimumButton.clicked.connect(self.showMinimized) # 按鈕信號連接到最小化窗口的槽函數(shù)
      self._MaximumButton = QTitleButton(b'\xef\x80\xb1'.decode("utf-8"), self)
      self._MaximumButton.setObjectName("MinMaxButton") # 設(shè)置按鈕的ObjectName以在qss樣式表內(nèi)定義不同的按鈕樣式
      self._MaximumButton.setToolTip("最大化")
      self._MaximumButton.setMouseTracking(True) # 設(shè)置按鈕鼠標(biāo)跟蹤(如不設(shè),則按鈕在widget上層,無法實現(xiàn)跟蹤)
      self._MaximumButton.setFixedHeight(self._TitleLabel.height()) # 設(shè)置按鈕高度為標(biāo)題欄高度
      self._MaximumButton.clicked.connect(self._changeNormalButton) # 按鈕信號連接切換到恢復(fù)窗口大小按鈕函數(shù)

  def _changeNormalButton(self):
    # 切換到恢復(fù)窗口大小按鈕
    try:
      self.showMaximized() # 先實現(xiàn)窗口最大化
      self._MaximumButton.setText(b'\xef\x80\xb2'.decode("utf-8")) # 更改按鈕文本
      self._MaximumButton.setToolTip("恢復(fù)") # 更改按鈕提示
      self._MaximumButton.disconnect() # 斷開原本的信號槽連接
      self._MaximumButton.clicked.connect(self._changeMaxButton) # 重新連接信號和槽
    except:
      pass

  def _changeMaxButton(self):
    # 切換到最大化按鈕
    try:
      self.showNormal()
      self._MaximumButton.setText(b'\xef\x80\xb1'.decode("utf-8"))
      self._MaximumButton.setToolTip("最大化")
      self._MaximumButton.disconnect()
      self._MaximumButton.clicked.connect(self._changeNormalButton)
    except:
      pass

  def resizeEvent(self, QResizeEvent):
    # 自定義窗口調(diào)整大小事件
    self._TitleLabel.setFixedWidth(self.width()) # 將標(biāo)題標(biāo)簽始終設(shè)為窗口寬度
    # 分別移動三個按鈕到正確的位置
    try:
      self._CloseButton.move(self.width() - self._CloseButton.width(), 0)
    except:
      pass
    try:
      self._MinimumButton.move(self.width() - (self._CloseButton.width() + 1) * 3 + 1, 0)
    except:
      pass
    try:
      self._MaximumButton.move(self.width() - (self._CloseButton.width() + 1) * 2 + 1, 0)
    except:
      pass
    # 重新調(diào)整邊界范圍以備實現(xiàn)鼠標(biāo)拖放縮放窗口大小,采用三個列表生成式生成三個列表
    self._right_rect = [QPoint(x, y) for x in range(self.width() - self._padding, self.width() + 1)
              for y in range(1, self.height() - self._padding)]
    self._bottom_rect = [QPoint(x, y) for x in range(1, self.width() - self._padding)
             for y in range(self.height() - self._padding, self.height() + 1)]
    self._corner_rect = [QPoint(x, y) for x in range(self.width() - self._padding, self.width() + 1)
                  for y in range(self.height() - self._padding, self.height() + 1)]

  def mousePressEvent(self, event):
    # 重寫鼠標(biāo)點擊的事件
    if (event.button() == Qt.LeftButton) and (event.pos() in self._corner_rect):
      # 鼠標(biāo)左鍵點擊右下角邊界區(qū)域
      self._corner_drag = True
      event.accept()
    elif (event.button() == Qt.LeftButton) and (event.pos() in self._right_rect):
      # 鼠標(biāo)左鍵點擊右側(cè)邊界區(qū)域
      self._right_drag = True
      event.accept()
    elif (event.button() == Qt.LeftButton) and (event.pos() in self._bottom_rect):
      # 鼠標(biāo)左鍵點擊下側(cè)邊界區(qū)域
      self._bottom_drag = True
      event.accept()
    elif (event.button() == Qt.LeftButton) and (event.y() < self._TitleLabel.height()):
      # 鼠標(biāo)左鍵點擊標(biāo)題欄區(qū)域
      self._move_drag = True
      self.move_DragPosition = event.globalPos() - self.pos()
      event.accept()

  def mouseMoveEvent(self, QMouseEvent):
    # 判斷鼠標(biāo)位置切換鼠標(biāo)手勢
    if QMouseEvent.pos() in self._corner_rect:
      self.setCursor(Qt.SizeFDiagCursor)
    elif QMouseEvent.pos() in self._bottom_rect:
      self.setCursor(Qt.SizeVerCursor)
    elif QMouseEvent.pos() in self._right_rect:
      self.setCursor(Qt.SizeHorCursor)
    else:
      self.setCursor(Qt.ArrowCursor)
    # 當(dāng)鼠標(biāo)左鍵點擊不放及滿足點擊區(qū)域的要求后,分別實現(xiàn)不同的窗口調(diào)整
    # 沒有定義左方和上方相關(guān)的5個方向,主要是因為實現(xiàn)起來不難,但是效果很差,拖放的時候窗口閃爍,再研究研究是否有更好的實現(xiàn)
    if Qt.LeftButton and self._right_drag:
      # 右側(cè)調(diào)整窗口寬度
      self.resize(QMouseEvent.pos().x(), self.height())
      QMouseEvent.accept()
    elif Qt.LeftButton and self._bottom_drag:
      # 下側(cè)調(diào)整窗口高度
      self.resize(self.width(), QMouseEvent.pos().y())
      QMouseEvent.accept()
    elif Qt.LeftButton and self._corner_drag:
      # 右下角同時調(diào)整高度和寬度
      self.resize(QMouseEvent.pos().x(), QMouseEvent.pos().y())
      QMouseEvent.accept()
    elif Qt.LeftButton and self._move_drag:
      # 標(biāo)題欄拖放窗口位置
      self.move(QMouseEvent.globalPos() - self.move_DragPosition)
      QMouseEvent.accept()

  def mouseReleaseEvent(self, QMouseEvent):
    # 鼠標(biāo)釋放后,各扳機復(fù)位
    self._move_drag = False
    self._corner_drag = False
    self._bottom_drag = False
    self._right_drag = False


if __name__ == "__main__":
  from PyQt5.QtWidgets import QApplication
  import sys
  app = QApplication(sys.argv)
  app.setStyleSheet(open("./UnFrameStyle.qss").read())
  window = QUnFrameWindow()
  window.setCloseButton(True)
  window.setMinMaxButtons(True)
  window.show()
  sys.exit(app.exec_())

qss文件

/**********Title**********/
QTitleLabel{
    background-color: Gainsboro;
    font: 100 10pt;
}


/**********Button**********/
QTitleButton{
    background-color: rgba(255, 255, 255, 0);
    color: black;
    border: 0px;
    font: 100 10pt;
}
QTitleButton#MinMaxButton:hover{
    background-color: #D0D0D1;
    border: 0px;
    font: 100 10pt;
}
QTitleButton#CloseButton:hover{
    background-color: #D32424;
    color: white;
    border: 0px;
    font: 100 10pt;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java byte數(shù)組操縱方式代碼實例解析

    Java byte數(shù)組操縱方式代碼實例解析

    這篇文章主要介紹了Java byte數(shù)組操縱方式代碼實例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • Python?Flask?模型介紹和配置方法

    Python?Flask?模型介紹和配置方法

    flask是基于MTV的結(jié)構(gòu),其中M指的就是模型,即數(shù)據(jù)模型,在項目中對應(yīng)的是數(shù)據(jù)庫,下面紀(jì)錄以mysql和orm方式連接數(shù)據(jù)庫的方法,對Python?Flask?模型介紹和配置方法感興趣的朋友跟隨小編一起看看吧
    2022-12-12
  • windows10系統(tǒng)中安裝python3.x+scrapy教程

    windows10系統(tǒng)中安裝python3.x+scrapy教程

    本文給大家主要介紹了在windows10系統(tǒng)中安裝python3以及scrapy框架的教程以及有可能會遇到的問題的解決辦法,希望大家能夠喜歡
    2016-11-11
  • Python進階:生成器 懶人版本的迭代器詳解

    Python進階:生成器 懶人版本的迭代器詳解

    這篇文章主要介紹了Python進階:生成器 懶人版本的迭代器詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-06-06
  • Python爬蟲實現(xiàn)簡單的爬取有道翻譯功能示例

    Python爬蟲實現(xiàn)簡單的爬取有道翻譯功能示例

    這篇文章主要介紹了Python爬蟲實現(xiàn)簡單的爬取有道翻譯功能,結(jié)合實例形式分析了Python基于urllib庫實現(xiàn)的爬蟲爬取有道翻譯相關(guān)定義與數(shù)據(jù)交互功能操作技巧,需要的朋友可以參考下
    2018-07-07
  • Python3.10?Generator生成器Coroutine原生協(xié)程詳解

    Python3.10?Generator生成器Coroutine原生協(xié)程詳解

    這篇文章主要為大家介紹了Python3.10?Generator生成器Coroutine原生協(xié)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • python re模塊常見用法例舉

    python re模塊常見用法例舉

    在本篇文章里小編給大家整理的是一篇關(guān)于python re模塊常見用法例舉內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-03-03
  • Python threading Local()函數(shù)用法案例詳解

    Python threading Local()函數(shù)用法案例詳解

    這篇文章主要介紹了Python threading Local()函數(shù)用法案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • numpy降維方法

    numpy降維方法

    本文主要介紹了numpy降維方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Python能干什么、Python主要應(yīng)用于哪些方面

    Python能干什么、Python主要應(yīng)用于哪些方面

    無論是從入門級選手到專業(yè)級選手都在做的爬蟲,還是Web程序開發(fā)、桌面程序開發(fā)還是科學(xué)計算、圖像處理, Python都可以勝任。Python為我們提供了非常完善的基礎(chǔ)代碼庫,覆蓋了網(wǎng)絡(luò)、文件、GUI、 數(shù)據(jù)庫、文本等大量內(nèi)容。用Python開發(fā),許多功能不必從零編寫
    2023-06-06

最新評論

张掖市| 江城| 景德镇市| 民乐县| 竹溪县| 西和县| 买车| 博乐市| 文安县| 六安市| 勃利县| 宜章县| 大余县| 乐平市| 岳阳县| 衡南县| 涡阳县| 康平县| 中西区| 德保县| 江山市| 阳高县| 汝州市| 鹿泉市| 始兴县| 克拉玛依市| 陵水| 随州市| 庄浪县| 永平县| 杭锦后旗| 万源市| 连山| 平远县| 昭苏县| 临清市| 西宁市| 宁乡县| 察隅县| 沈阳市| 温州市|