实战PyQt5: 107-设置窗口背景
文章插图
【实战PyQt5: 107-设置窗口背景】在一些特定情况下 , 我们需要设置窗口的背景 , 来实现某种特别的显示效果 , 比如如果是一个视频窗口 , 常常将背景设置为黑色 , 如果是一个要显示带透明度的图像的窗口 , 常常将窗口的背景设置为国际象棋棋盘的样式等等 。
设置窗口的背景 , 就是设置窗口的背景色或者修改窗口的背景图片 , 在Qt中 , 设置窗口背景有三种方法:
- 使用QPalette设置窗口背景;
- 使用QSS设置窗口背景;
- 重新实现PaintEvent,使用QPainter绘制背景 。
palette = self.palette()palette.setColor(QPalette.Background, color)self.setPalette(palette)self.setAutoFillBackground(True)使用QBrush设置背景图片时 , 图像会使用平铺模式来填充背景 。 因此需要注意背景图片的尺寸大小 , 当背景图片的宽度和高度大于窗口的宽度或者高度时 , 背景图像会剪裁掉大于窗口尺寸的部分 , 当图像尺寸小于窗口尺寸时 , 图像重复填充窗口 。 使用QPalette设置背景图片的核心代码如下:palette = self.palette()palette.setColor(QPalette.Background, QBrush(QPixmap(image_filename)))self.setPalette(palette)self.setAutoFillBackground(True)使用QSS设置窗口背景使用QWidget.setStyleSheet()函数可以使用QSS方式为窗口设置背景 , 设置背景的颜色的方式如下( 假设背景色设置为绿色):self.setStyleSheet('background-color: green;')设置背景图像 , 如果使用拉伸填充方式:self.setStyleSheet('border-image:url(image_filename)')如果使用平铺模式:self.setStyleSheet('background -image:url(image_filename)')在paintEvent函数中绘制窗口背景重载部件的paintEvent()函数 , 在其中使用QPainter来绘制窗口背景 , 记住 , 既然是背景 , 就得首先绘制 , 不然会覆盖掉绘制背景之前的那些绘制元素 。创建绘图工具:
painter = QPainter(self)绘制背景颜色的代码(假设背景颜色为蓝色):painter.fillRect(self.rect(), Qt.blue)拉伸效果绘制背景painter.drawPixmap(self.rect(), QPixmap(self.imageFilename)))平铺效果绘制背景painter.fillRect(self.rect(), QBrush(QPixmap(image_filename)))测试代码测试代码使用了六个QFrame部件对象 , 演示了使用上述三种方法设置窗口背景 , 每一种方式演示了颜色和平铺图像方式 。 图像拉伸方式设置背景的代码也在其中(相关代码被注释 , 有兴趣的您可以打开注释看运行效果) 。完整代码如下:import sys,osfrom PyQt5.QtCore import Qtfrom PyQt5.QtGui import QPalette, QPainter, QBrush, QPixmap, QPenfrom PyQt5.QtWidgets import (QApplication, QWidget, QFrame, QGridLayout) class MyWindow(QFrame):def __init__(self, type, parent = None):super(MyWindow, self).__init__(parent)self.setObjectName('MyWindow')self.type = typeself.imageFilename = os.path.dirname(__file__) + "/background.png"#palette Backgroundif type == 'Palette-Color':palette = self.palette()palette.setColor(QPalette.Background, Qt.red)self.setPalette(palette)self.setAutoFillBackground(True)self.info = 'Palette Background Color'elif type == 'Palette-Image':palette = self.palette()palette.setBrush(QPalette.Background, QBrush(QPixmap(self.imageFilename)))self.setPalette(palette)self.setAutoFillBackground(True)self.info = 'Palette Background Image'elif type == 'Sheet-Color':self.setStyleSheet('#MyWindow{background-color: green;}')self.info = 'Style Sheet Color'elif type == 'Sheet-Image':#必须将\\ 替换成 / 才能正确显示urlName = self.imageFilename.replace('\\', '/')#平铺:background:url(),background-image:url();#拉伸填充:border-image:url();sheet = 'background-image:url(%s);'%(urlName)self.setStyleSheet(sheet)self.info = 'Style Sheet Image'elif type == 'Paint-Color':self.info='Paint Background Color'elif type == 'Paint-Image':self.info='Paint Background Image'else:print('不支持')def paintEvent(self, event):painter = QPainter(self)painter.setRenderHint(QPainter.TextAntialiasing)#如果是Paint模式则绘制背景if self.type == 'Paint-Color':painter.fillRect(self.rect(), Qt.blue)elif self.type == 'Paint-Image':#平铺效果painter.fillRect(self.rect(), QBrush(QPixmap(self.imageFilename)))#拉伸效果#painter.drawPixmap(self.rect(), QPixmap(self.imageFilename))#居中绘制文本信息painter.drawText(self.rect(), Qt.AlignCenter, self.info)class WindowBackgroundDemo(QWidget):def __init__(self, parent = None):super(WindowBackgroundDemo, self).__init__(parent)# 设置窗口标题self.setWindowTitle('实战PyQt5: 设置窗口背景')#设置窗口尺寸self.resize(640, 480)self.initUi()def initUi(self):layout = QGridLayout()wid1 = MyWindow('Palette-Color')wid2 = MyWindow('Palette-Image')wid3 = MyWindow('Sheet-Image')wid4 = MyWindow('Sheet-Color')wid5 = MyWindow('Paint-Color')wid6 = MyWindow('Paint-Image')layout.addWidget(wid1, 0, 0)layout.addWidget(wid2, 1, 0)layout.addWidget(wid3, 0, 1)layout.addWidget(wid4, 1, 1)layout.addWidget(wid5, 0, 2)layout.addWidget(wid6, 1, 2)self.setLayout(layout)if __name__ == '__main__':app = QApplication(sys.argv)window = WindowBackgroundDemo()window.show()sys.exit(app.exec())
- Wireshark数据包分析实战:TCP报文段重组
- Python爬虫采集网易云音乐热评实战
- Django实战016:django中使用redis详解
- HTTP实战之Wireshark抓包分析
- Wireshark数据包分析实战:网卡卸载
- Python数据分析:数据可视化实战教程
- 实战经验:电商平台遭遇CC攻击,我们是如何应对的?
- Tencent IN对话 | 八位互联网实战家,实战智慧营销商学院
- HLS实战之Wireshark抓包分析
- Vue实战091:Vue项目部署到nginx服务器
