PyQt5 实现给无边框widget窗口添加背景图片
作者:ros_tyler 时间:2021-01-15 17:07:44
我就废话不多说了,大家还是直接看代码吧~
#! /usr/bin/env python
# -*- coding:utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPalette, QBrush, QPixmap
class NoBorderWindow(QWidget):
def __init__(self):
super().__init__()
self.window_UI()
self.drawn()
def window_UI(self):
self.resize(950, 200)
self.setWindowFlags(Qt.FramelessWindowHint)
def drawn(self):
self.palette = QPalette()
self.palette.setBrush(QPalette.Background, QBrush(QPixmap("./images/bg.gif")))
self.setPalette(self.palette)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = NoBorderWindow()
win.show()
sys.exit(app.exec_())
补充:PyQt5 设置QWidget窗口背景图片问题
QWidget 添加背景图片问题
QWidget 创建的窗口有时并不能直接用 setStyleSheet 设置窗口部分样式
比如背景图,在Qt Designer 设置好背景图样式了 QWidget#Form{ … } 并能看到效果
但转为 python3 代码后,运行程序显示不了这个背景图
如果样式使用的是 background-image 就好办了,
直接使用下面代码替换,即使用 QPalette 控件重新画背景图
palette = QPalette()
palette.setBrush(QPalette.Background, QBrush(QPixmap(":/pic/images/sysBackground.jpg")))
self.setPalette(palette)
QSS 背景图样式区别:
background-image: 背景图,默认原图大小,窗口空余部分填充此背景图
border-image: 默认跟随窗口大小进行拉伸
image: 默认原图大小,窗口空余部分不补充
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/qq_42701005/article/details/105881365
标签:PyQt5,widget,窗口,背景图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
php打印输出棋盘的实现方法
2023-10-09 04:38:10
Python os和os.path模块详情
2022-12-08 12:53:45
![](https://img.aspxhome.com/file/2023/2/89332_0s.png)
text-overflow实现标题省略号功能
2009-05-19 12:13:00
python常用排序算法的实现代码
2022-08-21 08:50:00
Python爬取当网书籍数据并数据可视化展示
2023-11-20 11:31:14
![](https://img.aspxhome.com/file/2023/7/76907_0s.png)
SQL Server 对表的主键设计问题及解决办法
2010-06-07 13:29:00
Python中类型检查的详细介绍
2022-08-18 16:51:47
![](https://img.aspxhome.com/file/2023/7/70017_0s.jpg)
python logging模块的使用总结
2021-11-15 06:46:53
Python进程的通信Queue、Pipe实例分析
2021-11-22 13:50:44
![](https://img.aspxhome.com/file/2023/8/65078_0s.png)
Web开发的改良
2009-06-25 14:34:00
十行Python代码实现文字识别功能
2024-01-01 14:55:09
![](https://img.aspxhome.com/file/2023/1/80611_0s.png)
Python3如何在服务器打印资产信息
2023-11-15 08:03:45
![](https://img.aspxhome.com/file/2023/5/63545_0s.png)
Javascript Closures (2)
2009-03-18 12:22:00
什么是python的自省
2022-04-24 19:06:50
python 将md5转为16字节的方法
2023-09-17 18:46:37
python中requests爬去网页内容出现乱码问题解决方法介绍
2023-09-14 01:00:11
three.js中文文档学习之创建场景
2023-08-22 21:02:00
在oracle 数据库中查看一个sql语句的执行时间和SP2-0027错误
2009-10-09 13:04:00
PhpStorm配置debug环境的详细过程
2023-05-26 20:04:32
PHP代码加密和扩展解密实战
2023-11-17 02:58:00