简体   繁体   English

PyQt5中堆积小部件的透明背景

[英]Transparent Background for Stacked Widgets in PyQt5

I've been trying to create a basic clock using PyQt5. 我一直在尝试使用PyQt5创建一个基本时钟。 The clock part works and the background image behind the clock loads but I am unable to get the background of the text to be transparent. 时钟部分工作,时钟后面的背景图像加载,但我无法使文本的背景透明。

I have tried both the way listed below and also with self.lbl1.setStyleSheet("background-color: rgba(255, 255, 255, 10);") but I get the same result in both cases. 我已经尝试了下面列出的方式以及self.lbl1.setStyleSheet("background-color: rgba(255, 255, 255, 10);")但在两种情况下都得到了相同的结果。

If I set the layout.setCurrentIndex(0) I see that the background jpeg is there so that is loading just fine. 如果我设置layout.setCurrentIndex(0)我看到背景jpeg在那里,所以加载就好了。

Any thoughts on what I would need to do to have layered widgets with transparency? 关于我需要做些什么来构建具有透明度的分层小部件的想法?

To try to clear it up a bit 试着清理一下

Here is what I get 这是我得到的

here is what I want 这就是我想要的

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        layout = QStackedLayout()


        #Background area test
        self.background = QLabel(self)
        self.background.setPixmap(QPixmap("image.jpeg"))
        self.background.show()
        #self.background.setAutoFillBackground(True)

        #Setup text area for clock
        newfont = QFont("Consolas",120, QFont.Bold)
        self.lbl1 = QLabel()
        self.lbl1.setAlignment(Qt.AlignCenter)
        self.lbl1.setFont(newfont)
        self.lbl1.setWindowFlags(Qt.FramelessWindowHint)
        self.lbl1.setAttribute(Qt.WA_TranslucentBackground)

        #Timer to refresh clock
        timer = QTimer(self)
        timer.timeout.connect(self.showTime)
        timer.start(1000)
        self.showTime()



        #layout area for widgets

        layout.addWidget(self.background)
        layout.addWidget(self.lbl1)
        layout.setCurrentIndex(1)
        self.setLayout(layout)
        self.setGeometry(300,300,250,150)
        self.show()

    def showTime(self):
        time = QTime.currentTime()
        text = time.toString('hh:mm')
        if (time.second() % 2) == 0:
            text = text[:2] + ' ' + text[3:]
        self.lbl1.setText(text)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

The problem is using QStackedWidget, according to the documentation: 根据文档,问题是使用QStackedWidget:

The QStackedLayout class provides a stack of widgets where only one widget is visible at a time. QStackedLayout类提供了一组窗口小部件,其中一次只能看到一个窗口小部件。

In it tells us that only a widget is visible. 它告诉我们只有一个小部件是可见的。 That is why the image is not shown, to force make them visible we use: 这就是为什么图像没有显示,强制使它们可见我们使用:

layout.setStackingMode(QStackedLayout.StackAll)

Obtaining the following: 获得以下内容:

在此输入图像描述

As mentioned in the documentation QStackedlayout is used to display one widget at a time. 如文档中所述,QStackedlayout用于一次显示一个小部件。 In your case I think it is unnecessary, you could only use a label and any layout, you can use styleSheet to place the background image, the following code shows what I say. 在你的情况下,我认为这是不必要的,你只能使用标签和任何布局,你可以使用styleSheet来放置背景图片,下面的代码显示我说的。

class Example(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent=parent)
        self.initUI()

    def initUI(self):
        self.setGeometry(300,300,250,150)
        layout = QVBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        newfont = QFont("Consolas",120, QFont.Bold)
        self.lbl1 = QLabel(self)
        self.lbl1.setStyleSheet("border-image:url(image.jpeg);")
        self.lbl1.setAlignment(Qt.AlignCenter)
        self.lbl1.setFont(newfont)
        layout.addWidget(self.lbl1)

        #Timer to refresh clock
        timer = QTimer(self)
        timer.timeout.connect(self.showTime)
        timer.start(1000)
        self.showTime()
        self.show()

    def showTime(self):
        time = QTime.currentTime()
        text = time.toString('hh:mm')
        if (time.second() % 2) == 0:
            text = text[:2] + ' ' + text[3:]
        self.lbl1.setText(text)

The following image shows the new output: 下图显示了新输出:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM