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

python GUI庫圖形界面開發(fā)之PyQt5窗口背景與不規(guī)則窗口實(shí)例

 更新時(shí)間:2020年02月25日 15:09:29   作者:jia666666  
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5窗口背景與不規(guī)則窗口實(shí)例,需要的朋友可以參考下

窗口背景主要包括,背景色與背景圖片,設(shè)置窗口背景有三種方法

  • 使用QSS設(shè)置窗口背景
  • 使用QPalette設(shè)置窗口背景
  • 實(shí)現(xiàn)PainEvent,使用QPainter繪制背景

QSS設(shè)置窗口背景

在QSS中,我們可以使用Background或者background-color的方式來設(shè)置背景色,設(shè)置窗口背景色之后,子控件默認(rèn)會繼承父窗口的背景色,如果想要為控件設(shè)置背景圖片或圖標(biāo),則可以使用setPixmap或則setIcon來完成。關(guān)于這兩個函數(shù)的用法,可以參考本博客下的PyQt5的基礎(chǔ)控件分欄

實(shí)例:QSS設(shè)置窗口背景

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication

app = QApplication(sys.argv)
win = QMainWindow()

#設(shè)置窗口標(biāo)題與初始大小
win.setWindowTitle("界面背景圖片設(shè)置")
win.resize(350, 250)
#設(shè)置對象名稱
win.setObjectName("MainWindow")

# #todo 1 設(shè)置窗口背景圖片
win.setStyleSheet("#MainWindow{border-image:url(./images/python.jpg);}")

#todo 2 設(shè)置窗口背景色
#win.setStyleSheet("#MainWindow{background-color: yellow}")

win.show()
sys.exit(app.exec_())

運(yùn)行效果圖如下

核心代碼如下

#設(shè)置對象名稱
win.setObjectName("MainWindow")

# #todo 1 設(shè)置窗口背景圖片
win.setStyleSheet("#MainWindow{border-image:url(./images/python.jpg);}")

優(yōu)化 使用setStyleSheet()設(shè)置窗口背景色,核心代碼和效果圖如下

#todo 2 設(shè)置窗口背景色
win.setStyleSheet("#MainWindow{background-color: yellow}")

QPalette設(shè)置窗口背景

當(dāng)使用QPalette(調(diào)試板)來設(shè)置背景圖片時(shí),需要考慮背景圖片的尺寸

圖片尺寸可以文件管理器打開,右鍵屬性查看

當(dāng)背景圖片的寬度高度大于窗口的寬度高度時(shí),背景圖片會平鋪整個背景

當(dāng)背景圖片寬度高度小于窗口的寬度高度時(shí),則會加載多個背景圖片

實(shí)例:QPalette設(shè)置窗口背景

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtGui import QPalette, QBrush, QPixmap

app = QApplication(sys.argv)
win = QMainWindow()

win.setWindowTitle("界面背景圖片設(shè)置")
palette = QPalette()
palette.setBrush(QPalette.Background, QBrush(QPixmap("./images/python.jpg")))
win.setPalette(palette)

# todo 1 當(dāng)背景圖片的寬度和高度大于窗口的寬度和高度時(shí)
win.resize(460, 255 )
#
# # todo 2 當(dāng)背景圖片的寬度和高度小于窗口的寬度和高度時(shí)
# win.resize(800, 600)
win.show()
sys.exit(app.exec_())

當(dāng)背景圖片的寬度高度大于窗口的寬度高度時(shí),背景圖片會平鋪整個背景

當(dāng)背景圖片寬度高度小于窗口的寬度高度時(shí),則會加載多個背景圖片

核心代碼如下

win.setWindowTitle("界面背景圖片設(shè)置")
palette = QPalette()
palette.setBrush(QPalette.Background, QBrush(QPixmap("./images/python.jpg")))
win.setPalette(palette)

# todo 1 當(dāng)背景圖片的寬度和高度大于窗口的寬度和高度時(shí)
win.resize(460, 255 )
#
# # todo 2 當(dāng)背景圖片的寬度和高度小于窗口的寬度和高度時(shí)
# win.resize(800, 600)

PaintEvent設(shè)置窗口背景

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPainter,QPixmap
from PyQt5.QtCore import Qt


class Winform(QWidget):
  def __init__(self, parent=None):
    super(Winform, self).__init__(parent)
    self.setWindowTitle("paintEvent設(shè)置背景顏色")

  def paintEvent(self, event):
    painter = QPainter(self)
    #todo 1 設(shè)置背景顏色
    painter.setBrush(Qt.green)
    painter.drawRect(self.rect())

    # #todo 2 設(shè)置背景圖片,平鋪到整個窗口,隨著窗口改變而改變
    # pixmap = QPixmap("./images/screen1.jpg")
    # painter.drawPixmap(self.rect(), pixmap)


if __name__ == "__main__":
  app = QApplication(sys.argv)
  form = Winform()
  form.show()
  sys.exit(app.exec_())

核心代碼:使用paintEvent設(shè)置窗口的背景色

class Winform(QWidget):
  def __init__(self, parent=None):
    super(Winform, self).__init__(parent)
    self.setWindowTitle("paintEvent設(shè)置背景顏色")

  def paintEvent(self, event):
    painter = QPainter(self)
    #todo 1 設(shè)置背景顏色
    painter.setBrush(Qt.green)
    painter.drawRect(self.rect())

效果如圖

核心代碼:設(shè)置窗口背景圖片

# #todo 2 設(shè)置背景圖片,平鋪到整個窗口,隨著窗口改變而改變
pixmap = QPixmap("./images/screen1.jpg")
painter.drawPixmap(self.rect(), pixmap)

QWidget類中比較重要的繪圖函數(shù)如表所示

函數(shù) 描述
setMask(self,QBitmap)setMask(self,QRegion) setMask()的作用是為調(diào)用它的控件增加一個遮罩,遮住所選區(qū)域以外的部分,使之看起來是透明的,它的參數(shù)可以為QBitmap或QRegion對象,此處調(diào)用QPixmap的mask()函數(shù)獲得圖片自身的遮罩,是一個QBitmap對象,在實(shí)例中使用的是PNG格式的圖片,它的透明部分就是一個遮罩
paintEvent(self,QPaintEvent) 通過重載paintEvent()函數(shù)繪制窗口背景

不規(guī)則窗口實(shí)例 1

實(shí)現(xiàn)不規(guī)則窗口的最簡單方式就是圖片素材不僅當(dāng)遮罩層,還當(dāng)背景圖片,通過重載paintEvent()函數(shù)繪制窗口背景

import sys
from PyQt5.QtWidgets import QApplication,QWidget
from PyQt5.QtGui import QPixmap,QPainter,QBitmap

class MyForm(QWidget):
  def __init__(self,parent=None):
    super(MyForm, self).__init__(parent)
    #設(shè)置標(biāo)題與初始窗口大小
    self.setWindowTitle('不規(guī)則窗口的實(shí)現(xiàn)例子')
    self.resize(560,390)

  def paintEvent(self, QPaintEvent):
    painter=QPainter(self)
    #在指定位置繪制圖片
    painter.drawPixmap(0,0,280,390,QPixmap(r'./images/dog.jpg'))
    painter.drawPixmap(280,0,280,390,QBitmap(r'./images/dog.jpg'))
if __name__ == '__main__':
  app=QApplication(sys.argv)
  form=MyForm()
  form.show()
  sys.exit(app.exec_())

運(yùn)行效果如下

不規(guī)則窗口實(shí)例 2

使用兩張圖片,一張用來做遮罩來控制窗口的大小,然后在利用paintEvent()函數(shù)重繪另一張為窗口的背景圖。

import sys
from PyQt5.QtWidgets import QApplication,QWidget
from PyQt5.QtGui import QPixmap,QPainter,QBitmap

class MyForm(QWidget):
  def __init__(self,parent=None):
    super(MyForm, self).__init__(parent)
    #設(shè)置標(biāo)題與初始窗口大小
    self.setWindowTitle('不規(guī)則窗口的實(shí)現(xiàn)例子')

    self.pix=QBitmap('./images/mask.png')
    self.resize(self.pix.size())
    self.setMask(self.pix)

  def paintEvent(self, QPaintEvent):
    painter=QPainter(self)
    #在指定位置繪制圖片
    painter.drawPixmap(0,0,self.pix.width(),self.pix.height(),QPixmap(r'./images/screen1.jpg'))

if __name__ == '__main__':
  app=QApplication(sys.argv)
  form=MyForm()
  form.show()
  sys.exit(app.exec_())

運(yùn)行效果如下

可以拖動的不規(guī)則窗口實(shí)例

第二個窗口的實(shí)例是不可以拖動的,這里實(shí)現(xiàn)可以拖動的功能

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPixmap, QPainter, QCursor, QBitmap
from PyQt5.QtCore import Qt


class ShapeWidget(QWidget):
  def __init__(self, parent=None):
    super(ShapeWidget, self).__init__(parent)
    self.setWindowTitle("不規(guī)則的,可以拖動的窗體實(shí)現(xiàn)例子")
    self.mypix()

  # 顯示不規(guī)則 pix
  def mypix(self):
    #獲得圖片自身的遮罩
    self.pix = QBitmap("./images/mask.png")
    #將獲得的圖片的大小作為窗口的大小
    self.resize(self.pix.size())
    #增加一個遮罩
    self.setMask(self.pix)
    #print(self.pix.size())
    self.dragPosition = None

  # 重定義鼠標(biāo)按下響應(yīng)函數(shù)mousePressEvent(QMouseEvent)
  # 鼠標(biāo)移動響應(yīng)函數(shù)mouseMoveEvent(QMouseEvent),使不規(guī)則窗體能響應(yīng)鼠標(biāo)事件,隨意拖動。
  def mousePressEvent(self, event):
    #鼠標(biāo)左鍵按下
    if event.button() == Qt.LeftButton:
      self.m_drag = True
      self.m_DragPosition = event.globalPos() - self.pos()
      event.accept()
      self.setCursor(QCursor(Qt.OpenHandCursor))
    if event.button() == Qt.RightButton:
      self.close()

  def mouseMoveEvent(self, QMouseEvent):
    if Qt.LeftButton and self.m_drag:
      # 當(dāng)左鍵移動窗體修改偏移值
      self.move(QMouseEvent.globalPos() - self.m_DragPosition)
      QMouseEvent.accept()

  def mouseReleaseEvent(self, QMouseEvent):
    self.m_drag = False
    self.setCursor(QCursor(Qt.ArrowCursor))

  # 一般 paintEvent 在窗體首次繪制加載, 要重新加載paintEvent
  # 需要重新加載窗口使用 self.update() or self.repaint()
  def paintEvent(self, event):
    painter = QPainter(self)
    #在指定位置繪制圖片
    painter.drawPixmap(0, 0, self.width(), self.height(), QPixmap("./images/boy.png"))


if __name__ == '__main__':
  app = QApplication(sys.argv)
  form = ShapeWidget()
  form.show()
  app.exec_()

運(yùn)行效果如下

本文主要介紹了python GUI庫PyQt5窗口背景與不規(guī)則窗口實(shí)例,大家可以參考下,更多關(guān)于這方面的文章大家可以點(diǎn)擊下面的相關(guān)鏈接

相關(guān)文章

  • python繪制云雨圖raincloud?plot

    python繪制云雨圖raincloud?plot

    這篇文章主要介紹了python繪制云雨圖raincloud?plot,Raincloud的Python實(shí)現(xiàn)是一個名為PtitPrince的包,它寫在seaborn之上,這是一個Python繪圖庫,用于從pandas數(shù)據(jù)幀中獲取漂亮的繪圖
    2022-08-08
  • python如何實(shí)現(xiàn)完全數(shù)

    python如何實(shí)現(xiàn)完全數(shù)

    這篇文章主要介紹了python如何實(shí)現(xiàn)完全數(shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • python實(shí)現(xiàn)簡易聊天對話框

    python實(shí)現(xiàn)簡易聊天對話框

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡易聊天對話框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Python接口傳輸url與flask數(shù)據(jù)詳解

    Python接口傳輸url與flask數(shù)據(jù)詳解

    這篇文章主要介紹了Python通過接口傳輸url與flask數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-09-09
  • PyQt+socket實(shí)現(xiàn)遠(yuǎn)程操作服務(wù)器的方法示例

    PyQt+socket實(shí)現(xiàn)遠(yuǎn)程操作服務(wù)器的方法示例

    這篇文章主要介紹了PyQt+socket實(shí)現(xiàn)遠(yuǎn)程操作服務(wù)器的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 如何給pip更換國內(nèi)源并配置Python的國內(nèi)鏡像詳解

    如何給pip更換國內(nèi)源并配置Python的國內(nèi)鏡像詳解

    pip安裝的包都存在于外國的服務(wù)器上,速度會非常慢,可以給pip配置國內(nèi)鏡像,直接從國內(nèi)服務(wù)器安裝依賴,這篇文章主要介紹了如何給pip更換國內(nèi)源并配置Python的國內(nèi)鏡像的相關(guān)資料,需要的朋友可以參考下
    2025-04-04
  • python實(shí)現(xiàn)識別手寫數(shù)字 python圖像識別算法

    python實(shí)現(xiàn)識別手寫數(shù)字 python圖像識別算法

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)識別手寫數(shù)字,python圖像識別算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 詳解OpenMV圖像處理的基本方法

    詳解OpenMV圖像處理的基本方法

    這篇文章主要介紹了OpenMV圖像處理的基本方法,包括感光元件的相關(guān)知識介紹,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-11-11
  • python 中 __init__的意義以及作用

    python 中 __init__的意義以及作用

    python中的__init__是一個私有函數(shù)(方法),訪問私有函數(shù)中的變量在python中用self,在PHP中用$this,這篇文章主要介紹了python 中 __init__的意義以及作用,需要的朋友可以參考下
    2023-02-02
  • python實(shí)現(xiàn)經(jīng)緯度采樣的示例代碼

    python實(shí)現(xiàn)經(jīng)緯度采樣的示例代碼

    這篇文章主要介紹了python實(shí)現(xiàn)經(jīng)緯度采樣的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12

最新評論

安阳市| 古蔺县| 巫山县| 亳州市| 建湖县| 岳池县| 垦利县| 通渭县| 清远市| 鄂托克前旗| 齐河县| 岳阳县| 莎车县| 望城县| 台南县| 长治县| 正定县| 青冈县| 佳木斯市| 中山市| 南华县| 东源县| 高要市| 漳平市| 伊吾县| 哈巴河县| 惠来县| 新闻| 噶尔县| 平安县| 大方县| 宜城市| 行唐县| 都昌县| 内江市| 浦东新区| 宁陕县| 达尔| 枣强县| 苏尼特左旗| 金湖县|