我一直在尝试使用PyQt5创建一个基本时钟。时钟部分工作,时钟加载后面的背景图像,但我无法使文本的背景变得透明。PyQt5中堆叠小部件的透明背景
我已经尝试了下面列出的方式和self.lbl1.setStyleSheet("background-color: rgba(255, 255, 255, 10);")
,但在两种情况下我都得到了相同的结果。
如果我设置layout.setCurrentIndex(0)
我看到后台jpeg在那里,所以加载就好了。
有什么想法,我需要做什么才能使透明的分层小部件?
要尽量把它清除掉一点
这里是我所得到的
这里就是我想要的
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_())
你可以表现出你想要得到你所得到的,什么图像,这将澄清你的问题很多。 – eyllanesc
@ eyllanesc是的,可以做。在我现在正在做什么以及我希望做什么的问题中添加了一些图片。 – Klankins