PyQt5中的Label设置背景setAutoFillBackground

25 篇文章 6 订阅

由于PyQt5的资料比较少,这里主要是记录下我的学习经历

lable1.setAutoFillBackground(True)


很早之前就开始遇到这个问题,就是在给label设置背景的时候,总感觉这个属性没有用,今天才发现这个属性是需要和调色板结合起来一起使用才行,下面先看下这段代码

        #初始化标签控件
        lable1.setText("文本标签")
        lable1.setAutoFillBackground(True)
        palette = QPalette()
        palette.setColor(QPalette.Window, Qt.red)
        lable1.setPalette(palette)
        lable1.setAlignment(Qt.AlignCenter)

上面的显示结果如下图
这里写图片描述

解释下上面的代码,setAutoFillBackground(True)表示的是自动填充背景,如果想使用后面的代码。这里必须要设置为True
palette = QPalette(),这里是实例化一个调色板对象,大家可以这样考虑,你想给label设置背景那么肯定需要一个颜色器来生成,这里的QPalette就是这个颜色器
palette.setColor(QPalette.Window, Qt.red)这里就是颜色器设置颜色
lable1.setPalette(palette)这里就是想颜色器上的颜色整合到label上去

当然这样的话是可以设置label的背景颜色,但是只能设置红绿蓝这些基本的颜色,在项目开发的时候肯定是设置自定义的详细颜色,那么我们可以采取下面的方法设置

        background_color = QColor()
        background_color.setNamedColor('#282821')

        #初始化标签控件
        lable1.setText("文本标签")
        lable1.setAutoFillBackground(True)
        palette = QPalette()
        palette.setColor(QPalette.Window, background_color)
        lable1.setPalette(palette)
        lable1.setAlignment(Qt.AlignCenter)

其实大家也可以看到
background_color = QColor()
background_color.setNamedColor(‘#282821’)
这两句就是在设置我们的自定义颜色值
palette.setColor(QPalette.Window, background_color)这个就是应用我们的颜色值,具体效果如下图这里写图片描述

下面是完整的代码

import PyQt5
import sys

import requests
from PyQt5.QtGui import QPalette, QPixmap, QColor
from PyQt5.QtWidgets import QWidget, QLabel, QVBoxLayout, QApplication
from PyQt5.QtCore import Qt
# http://imgs.technews.cn/wp-content/uploads/2014/10/Baidu.jpg
class WindowDemo(QWidget):
    def __init__(self):
        super().__init__()
        lable1 = QLabel(self)
        lable2 = QLabel(self)
        lable3 = QLabel(self)
        lable4 = QLabel(self)

        url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1516814788757&di=b719cdd79b84b6f563a36617911e2cf8&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20180112%2Ff309b61eb3bd44e6956948de77f78ec3.jpg'
        req = requests.get(url)

        photo = QPixmap()
        photo.loadFromData(req.content)


        background_color = QColor()
        background_color.setNamedColor('#282821')

        #初始化标签控件
        lable1.setText("文本标签")
        lable1.setAutoFillBackground(True)
        palette = QPalette()
        palette.setColor(QPalette.Window, background_color)
        lable1.setPalette(palette)
        lable1.setAlignment(Qt.AlignCenter)

        lable2.setText("<a href='#'>欢迎使用PythonGUI应用</a>")
        lable3.setAlignment(Qt.AlignCenter)
        lable3.setToolTip("这个一个图片标签")
        lable3.setPixmap(photo)

        lable4.setText("<a href='http://www.baidu.com/'>WelCome to Baidu</a>")
        lable4.setAlignment(Qt.AlignRight)
        lable4.setToolTip("这是一个超链接标签")
        lable4.setAutoFillBackground(True)
        palette = QPalette()
        palette.setColor(QPalette.Window, Qt.red)
        lable4.setPalette(palette)
        lable4.setAlignment(Qt.AlignCenter)

        vbox = QVBoxLayout()
        vbox.addWidget(lable1)
        vbox.setStretch(1,1)
        vbox.addWidget(lable2)
        vbox.setStretch(1, 1)
        # vbox.addStretch()
        vbox.addWidget(lable3)
        vbox.setStretch(1, 1)
        # vbox.addStretch()
        # vbox.addStretch()
        vbox.addWidget(lable4)



        #允许label1访问超链接
        lable1.setOpenExternalLinks(True)

        #允许label4访问超链接并且打开浏览器访问,设置了False则不会访问网页
        lable4.setOpenExternalLinks(True)

        lable4.linkActivated.connect(link_clicked)

        lable2.linkHovered.connect(link_hovered)

        lable1.setTextInteractionFlags(Qt.TextSelectableByMouse)

        self.setLayout(vbox)

        self.setWindowTitle("例子")


def link_clicked():
        print("鼠标点击了label标签")

def link_hovered():
        print("鼠标画过了lable2标签")


if __name__=="__main__":
    app = QApplication(sys.argv)
    win = WindowDemo()
    win.show()
    app.exit(app.exec_())

欢迎留言

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值