Pyqt 设置 背景颜色和背景图片、 QPalette 调色板 与QPainter 画板区别 、 不规则图片

2023-05-16,,

设置 背景颜色和背景图片

首先设置autoFillBackground属性为真
然后定义一个QPalette对象
设置QPalette对象的背景属性(颜色或图片)
最后设置QWidget对象的Palette

实例:

 # -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import * class Icon(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
palette1 = QtGui.QPalette(self)
palette1.setColor(self.backgroundRole(), QColor(192,253,123)) # 设置背景颜色
# palette1.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap('../../../Document/images/17_big.jpg'))) # 设置背景图片
self.setPalette(palette1)
self.setAutoFillBackground(True) # 不设置也可以
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Icon')
self.setWindowIcon(QtGui.QIcon('../../../Document/images/firefox.png'))
mylayout = QVBoxLayout()
self.setLayout(mylayout) app = QtGui.QApplication(sys.argv)
icon = Icon()
icon.show()
sys.exit(app.exec_())

效果:

---------------------------------------------------------------------------------------

QPalette 调色板  与QPainter 画板区别

 # -*- coding: UTF8 -*-
from PyQt4 import QtGui
from PyQt4.QtGui import *
import sys '''
调色板: palette 铺平整个背景 (小于窗体有多个图片)
png 如果有图层,背景为黑色,可图层覆盖
'''
class Icon(QtGui.QWidget):
def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent)
self.resize(256, 256)
self.setWindowTitle('Icon')
mylayout = QVBoxLayout()
self.setLayout(mylayout) def paintEvent(self, event):
palette1 = QtGui.QPalette(self)
palette1.setColor(self.backgroundRole(), QColor(192, 253, 123)) # 设置背景颜色
palette1.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap('1.png'))) # 设置背景图片
self.setPalette(palette1) app = QtGui.QApplication(sys.argv)
icon = Icon()
icon.show()
sys.exit(app.exec_())

效果:

 # -*- coding: UTF8 -*-
from PyQt4.QtGui import *
from PyQt4.QtCore import * '''
绘制QPainter 窗体 显示一个
QPainter默认只能在paintEvent里面调用 否则:QPainter::begin: Paint device returned engine == 0, type: 1
'''
class MyForm(QWidget):
def __init__(self,parent=None):
super(MyForm, self).__init__(parent)
self.resize(256, 256) def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setPen(Qt.NoPen)
painter.drawPixmap(0, 0, 256, 256, QPixmap("1.png"))
painter.end() app = QApplication([])
form = MyForm()
form.show()
app.exec_()

效果:

--------------------------------------------------------------------------------------------------------------------------

不规则窗体的显示

pyqt 的显示不规则图片主要注意两点
1. 图片的遮盖物mask
pixmap.setMask() etMask()的作用是为调用它的控件增加一个遮罩,遮住所选区域以外的部分使之看起来是透明的, 它的参数可为一个QBitmap对象或一个QRegion对象,此处调用QPixmap的mask()函数获得图片自身的遮罩,为一个QBitmap对象
2. 如何激活paintEvent
paintEvent 每次初始化窗体只调用一次,导致chang不规则图片的时候遮盖物不修改,所以要每修改次图片就激活一次paintEvent事件
激活的方法是更新窗体或重新绘制

通过Qtimer 每隔*/秒  self.connect(self.timer,SIGNAL("timeout()"),self.timeChang)   触发update

 self.update()
self.repaint()

代码如下:

 # -*- coding: UTF8 -*-
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys class ShapeWidget(QWidget):
def __init__(self,parent=None):
super(ShapeWidget,self).__init__(parent)
self.i = 1
self.mypix()
self.timer = QTimer()
self.timer.setInterval(500) # 500秒
self.timer.timeout.connect(self.timeChange)
self.timer.start() # 显示不规则 pic
def mypix(self):
self.update()
if self.i == 5:
self.i = 1
self.mypic = {1: 'Left_Button.png', 2: "Up_Button.png", 3: 'Right_Button.png', 4: 'Down_Button.png'}
self.pix = QPixmap(self.mypic[self.i], "", Qt.AvoidDither | Qt.ThresholdDither | Qt.ThresholdAlphaDither) #
self.resize(self.pix.size())
self.setMask(self.pix.mask()) #setMask()的作用是为调用它的控件增加一个遮罩,遮住所选区域以外的部分使之看起来是透明的, 它的参数可为一个QBitmap对象或一个QRegion对象,此处调用QPixmap的mask()函数获得图片自身的遮罩,为一个QBitmap对象。实例中使用的是png格式的图片,它的透明部分实际上即是一个遮罩。
self.dragPosition=None #重定义鼠标按下响应函数mousePressEvent(QMouseEvent)和鼠标移动响应函数mouseMoveEvent(QMouseEvent),使不规则窗体能响应鼠标事件,随意拖动。
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.dragPosition = event.globalPos()-self.frameGeometry().topLeft() #保存当前鼠标点所在的位置相对于窗体左上角的偏移值dragPosition,
event.accept()
if event.button() == Qt.RightButton:
self.close() def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton:
self.move(event.globalPos()-self.dragPosition) # 当左键移动窗体修改偏移值
event.accept()
# 如何激活 paintEvent 呢? 一般 paintEvent 在窗体首次绘制加载, 要重新加载paintEvent 需要重新加载窗口使用 self.update() or self.repaint()
def paintEvent(self, event):
painter = QPainter(self)
painter.drawPixmap(0, 0, self.pix.width(),self.pix.height(),self.pix)
# 鼠标双击事件
def mouseDoubleClickEvent(self, event):
if event.button() == 1:
self.i+=1
self.mypix() #每**秒修改paint
def timeChange(self):
self.i+=1
self.mypix() if __name__ == '__main__':
app=QApplication(sys.argv)
form=ShapeWidget()
form.show()
app.exec_()

不规则资源图片:

效果:

---------------------------------------------------------------------------------------------

加载Gif动画效果

 # -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore,QtGui
class loadingGif(QtGui.QWidget):
def __init__(self,parent=None):
super(loadingGif, self).__init__(parent)
self.label = QtGui.QLabel('', self)
self.setFixedSize(200,200)
self.setWindowFlags(QtCore.Qt.Dialog|QtCore.Qt.CustomizeWindowHint)
self.movie = QtGui.QMovie("loading.gif")
self.label.setMovie(self.movie)
self.movie.start() if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mw = loadingGif()
mw.show()
sys.exit(app.exec_())

效果:

Pyqt 设置 背景颜色和背景图片、 QPalette 调色板 与QPainter 画板区别 、 不规则图片的相关教程结束。

《Pyqt 设置 背景颜色和背景图片、 QPalette 调色板 与QPainter 画板区别 、 不规则图片.doc》

下载本文的Word格式文档,以方便收藏与打印。