目录

PyQt5设置QWidget窗口背景图片

QWidget 添加背景图片问题

QWidget 创建窗口有时并不能直接用 setStyleSheet 设置窗口部分样式

比如背景图,在Qt Designer 设置好背景图样式了 QWidget#Form{ … } 并能看到效果

但转为 python3 代码后,运行程序显示不了这个背景图

如果样式使用的是 backgroundimage 就好办了,

直接使用下面代码替换,即使用 QPalette 控件重新画背景图

1

2

3

palette = QPalette()

palette.setBrush(QPalette.Background, QBrush(QPixmap(":/pic/images/sysBackground.jpg")))  

self.setPalette(palette)

QSS 背景图样式区别

PyQt设置窗口背景图像,以及图像适应窗口大小变化

一次用PyQt, 由于之前已经用了一段时间的Python,种种原因需要界面,搜了网上很多攻略,选择了最简单一个方法下载PyQt5和pyqt5_tools。具体的配置这里不详细说了。

配置好之后通过如下界面点击Qt Design(自己起的名)调用QT:

点击之后创建QtWidgets界面test自己起的名):

此时点击保存,选择当前工程路径工程目录下会多一个.ui文件,此时右击该ui文件

利用pyuicui文件转换成Python代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

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: 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

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_())

结果

全屏

原文地址:https://blog.csdn.net/FL1623863129/article/details/134627576

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_17385.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注