PyQt5之如何設(shè)置QWidget窗口背景圖片問題
PyQt5設(shè)置QWidget窗口背景圖片
QWidget 添加背景圖片問題
QWidget 創(chuàng)建的窗口有時并不能直接用 setStyleSheet 設(shè)置窗口部分樣式
比如背景圖,在Qt Designer 設(shè)置好背景圖樣式了 QWidget#Form{ … } 并能看到效果
但轉(zhuǎn)為 python3 代碼后,運行程序顯示不了這個背景圖
如果樣式使用的是 background-image 就好辦了,
直接使用下面代碼替換,即使用 QPalette 控件重新畫背景圖
palette = QPalette()
palette.setBrush(QPalette.Background, QBrush(QPixmap(":/pic/images/sysBackground.jpg"))) ?
self.setPalette(palette)QSS 背景圖樣式區(qū)別
background-image: 背景圖,默認(rèn)原圖大小,窗口空余部分填充此背景圖border-image: 默認(rèn)跟隨窗口大小進行拉伸image: 默認(rèn)原圖大小,窗口空余部分不補充
PyQt設(shè)置窗口背景圖像,以及圖像自適應(yīng)窗口大小變化
第一次用PyQt, 由于之前已經(jīng)用了一段時間的Python,種種原因需要做界面,搜了網(wǎng)上很多攻略,選擇了最簡單的一個方法,下載PyQt5和pyqt5_tools。具體的配置這里不詳細(xì)說了。
配置好之后通過如下界面點擊Qt Design(自己起的名)調(diào)用QT:
點擊之后創(chuàng)建QtWidgets的界面(test是自己起的名):

此時點擊保存,選擇當(dāng)前工程的路徑,工程目錄下會多一個.ui文件,此時右擊該ui文件:

利用pyuic把ui文件轉(zhuǎn)換成Python代碼:
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): ? ? ? ? def setupUi(self, Form): ? ? ? ? ? ? ? ? Form.setObjectName(“Form”) ? ? ? ? ? ? ? ? Form.resize(400, 300) ? ? ? ? ? ? ? ? palette = QtGui.QPalette() ? ? ? ? ? ? ? ? brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) ? ? ? ? ? ? ? ? brush.setStyle(QtCore.Qt.SolidPattern) ? ? ? ? ? ? ? ? palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Link, brush) ? ? ? ? ? ? ? ? brush = QtGui.QBrush(QtGui.QColor(255, 0, 0)) ? ? ? ? ? ? ? ? brush.setStyle(QtCore.Qt.SolidPattern) ? ? ? ? ? ? ? ? palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.LinkVisited, brush) ? ? ? ? ? ? ? ? brush = QtGui.QBrush(QtGui.QColor(255, 85, 0)) ? ? ? ? ? ? ? ? brush.setStyle(QtCore.Qt.SolidPattern) ? ? ? ? ? ? ? ? palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Link, brush) ? ? ? ? ? ? ? ? brush = QtGui.QBrush(QtGui.QColor(255, 0, 0)) ? ? ? ? ? ? ? ? brush.setStyle(QtCore.Qt.SolidPattern) ? ? ? ? ? ? ? ? palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.LinkVisited, brush) ? ? ? ? ? ? ? ? brush = QtGui.QBrush(QtGui.QColor(0, 0, 255)) ? ? ? ? ? ? ? ? brush.setStyle(QtCore.Qt.SolidPattern) ? ? ? ? ? ? ? ? palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Link, brush) ? ? ? ? ? ? ? ? brush = QtGui.QBrush(QtGui.QColor(255, 0, 0)) ? ? ? ? ? ? ? ? brush.setStyle(QtCore.Qt.SolidPattern) ? ? ? ? ? ? ? ? palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.LinkVisited, brush) ? ? ? ? ? ? ? ? Form.setPalette(palette) ? ? ? ? ? ? ? ? self.retranslateUi(Form) ? ? ? ? ? ? ? ? QtCore.QMetaObject.connectSlotsByName(Form) ? ? ? ? def retranslateUi(self, Form): ? ? ? ? ? ? ? ? _translate = QtCore.QCoreApplication.translate ? ? ? ? ? ? ? ? Form.setWindowTitle(_translate(“Form”, “Form”))
此時新建文件login.py:
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPixmap,QPainter
from test import Ui_Form
import numpy as np
import sys
class mywindow(Ui_Form, QWidget):
? ? ? ? def init(self):
? ? ? ? ? ? ? ? super(mywindow, self).init()
? ? ? ? ? ? ? ? self.setupUi(self)
? ? ? ? ? ? ? ? self.num = np.random.randint(10)
? ? ? ? ? ? ? ? self.setWindowTitle(‘行人檢測')
? ? ? ? ? ? ? ? print(self.num)
? ? ? ? def paintEvent(self, event):# set background_img
? ? ? ? ? ? ? ? painter = QPainter(self)
? ? ? ? ? ? ? ? painter.drawRect(self.rect())
? ? ? ? ? ? ? ? pixmap = QPixmap("./img/1.jpg")#換成自己的圖片的相對路徑
? ? ? ? ? ? ? ? painter.drawPixmap(self.rect(), pixmap)
if name == ‘main':
app = QApplication(sys.argv)
w = mywindow()
w.paintEngine()
w.show()
sys.exit(app.exec_())結(jié)果:

全屏:

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python網(wǎng)絡(luò)爬蟲精解之XPath的使用說明
XPath 是一門在 XML 文檔中查找信息的語言。XPath 可用來在 XML 文檔中對元素和屬性進行遍歷。XPath 是 W3C XSLT 標(biāo)準(zhǔn)的主要元素,并且 XQuery 和 XPointer 都構(gòu)建于 XPath 表達(dá)之上2021-09-09
Pyorch之numpy與torch之間相互轉(zhuǎn)換方式
今天小編就為大家分享一篇Pyorch之numpy與torch之間相互轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
python TK庫簡單應(yīng)用(實時顯示子進程輸出)
這篇文章主要介紹了python TK庫簡單應(yīng)用(實時顯示子進程輸出),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
詳解OpenCV中直方圖,掩膜和直方圖均衡化的實現(xiàn)
這篇文章主要為大家詳細(xì)介紹了OpenCV中直方圖、掩膜、直方圖均衡化詳細(xì)介紹及代碼的實現(xiàn),文中的示例代碼講解詳細(xì),需要的可以參考一下2022-11-11
Deepsort + Yolo 實現(xiàn)行人檢測和軌跡追蹤的方法
這篇文章主要介紹了Deepsort + Yolo 實現(xiàn)行人檢測和軌跡追蹤,本項目通過采用深度學(xué)習(xí)方法實現(xiàn)YOLO算法行人檢測和deepsort算法對人員定位的和軌跡跟蹤,需要的朋友可以參考下2021-09-09
Python pip安裝依賴redis被自動降級的問題及解決
安裝Redis 7.1.0時因版本過舊與Python3.10不兼容,通過pip安裝依賴時遵循“版本約束”,導(dǎo)致Redis降級到<3.0,Booktype 項目依賴Redis <3.0,與Python3.10不兼容,解決方法是刪除Booktype依賴,以避免版本沖突2025-12-12

