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各種語言間時間的轉(zhuǎn)化實現(xiàn)代碼
這篇文章主要介紹了python各種語言間時間的轉(zhuǎn)化,需要的朋友可以參考下2016-03-03使用C#配合ArcGIS Engine進行地理信息系統(tǒng)開發(fā)
這篇文章主要介紹了使用C#配合ArcGIS Engine進行地理信息系統(tǒng)開發(fā),ArcGIS Engine是Windows系統(tǒng)上可以讓程序員創(chuàng)建自定義的GIS桌面程序,需要的朋友可以參考下2016-02-02python小例子-縮進式編碼+算術(shù)運算符+定義與賦值
這篇文章主要給大家分享一些python學(xué)習(xí)小例子,內(nèi)容包括縮進式編碼風(fēng)格、算術(shù)運算符、定義與賦值,需要的小伙伴可以參考一下2022-04-04