gpt4 book ai didi

python - 在 pyqt4 中的特定小部件(透明标签)上覆盖文本

转载 作者:太空宇宙 更新时间:2023-11-03 11:08:01 32 4
gpt4 key购买 nike

考虑修改 answer from @ekhumoro 如下所示。

QWidget 中嵌入了一个 mplayer。现在我想在视频上叠加一些文字。但是我下面的方法不起作用(标签的背景不透明,以至于只能看到视频和文本)。有解决办法吗?

更笼统地说:如何在自定义小部件(在我的例子中是 mplayer-widget)上放置透明标签?

如果无法准确实现我想要的效果,那么只显示卡住视频最后一帧(或预定义帧)并在其上显示一些文本的内容就足够了。

请注意,在稍后阶段,我希望覆盖视频的文本随时间变化,因此解决方案应该已经考虑到这一点。

为了透明性,可能需要注意我使用的是 linux 环境,这在 xmonad 下应该尤其有效。

import mpylayer
from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.container = QtGui.QWidget(self)

#self.container.setStyleSheet('background: black')
self.button = QtGui.QPushButton('Open', self)
self.button.clicked.connect(self.handleButton)


self.layout = QtGui.QVBoxLayout(self)
self.layout.addWidget(self.button)

self.layout.addWidget(self.container)

self.mplayer = mpylayer.MPlayerControl(
'mplayer', ['-wid', str(self.container.winId())])


self.label = QtGui.QLabel('Some text\n and more',self)
self.label.move(100,100)
self.label.setGeometry(200,200,900,300)

#This doesn't work
self.label.setAttribute(QtCore.Qt.WA_TranslucentBackground)

#opacity doesn't work
self.label.setStyleSheet("QLabel {font-size: 100px; opacity:0.5}")


def handleButton(self):
path = QtGui.QFileDialog.getOpenFileName()
if not path.isEmpty():
self.mplayer.loadfile(unicode(path))

if __name__ == '__main__':

import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.resize(640, 480)
window.show()
sys.exit(app.exec_())

这是我的非工作方法的截图:

non working approach

这是我想用 gimp 伪造的(也许我应该使用红色字体颜色,但那应该只是简单的 css):

gimp fake

编辑以下是我如何尝试使 X.Jacobs 的答案适应我的示例。但是它不起作用。只有当我调整窗口大小时,覆盖的文本/线条才会在视频上出现一毫秒然后再次消失(在这两种情况下,如果视频正在运行和正在暂停)。

import mpylayer
from PyQt4 import QtGui, QtCore

class overlayLabel(QtGui.QLabel):
def __init__(self, parent=None):
super(overlayLabel, self).__init__(parent)
self.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignVCenter)

self.setText("OVERLAY TEXT")
self.setStyleSheet("QLabel {font-size: 100px;}")
self.setGeometry(200,200,900,300)

class overlay(QtGui.QWidget):
def __init__(self, parent=None):
super(overlay, self).__init__(parent)

palette = QtGui.QPalette(self.palette())
palette.setColor(palette.Background, QtCore.Qt.transparent)

self.setPalette(palette)

def paintEvent(self, event):
painter = QtGui.QPainter()
painter.begin(self)
painter.setRenderHint(QtGui.QPainter.Antialiasing)
painter.fillRect(event.rect(), QtGui.QBrush(QtGui.QColor(255, 255, 255, 27)))
painter.drawLine(self.width()/8, self.height()/8, 7*self.width()/8, 7*self.height()/8)
painter.drawLine(self.width()/8, 7*self.height()/8, 7*self.width()/8, self.height()/8)
painter.setPen(QtGui.QPen(QtCore.Qt.NoPen))


class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.container = QtGui.QWidget(self)

#self.container.setStyleSheet('background: black')
self.button = QtGui.QPushButton('Open', self)
self.button.clicked.connect(self.handleButton)


self.layout = QtGui.QVBoxLayout(self)
self.layout.addWidget(self.button)

self.layout.addWidget(self.container)

self.mplayer = mpylayer.MPlayerControl(
'mplayer', ['-wid', str(self.container.winId())])



## Both versions don't work:

#self.label = overlay(self.container)
self.label = overlayLabel(self.container)



def handleButton(self):
path = QtGui.QFileDialog.getOpenFileName()
if not path.isEmpty():
self.mplayer.loadfile(unicode(path))


if __name__ == '__main__':

import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.resize(640, 480)
window.show()
sys.exit(app.exec_())

最佳答案

查看此示例叠加小部件,您可以根据自己的需要进行调整:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class overlay(QWidget):
def __init__(self, parent=None):
super(overlay, self).__init__(parent)

palette = QPalette(self.palette())
palette.setColor(palette.Background, Qt.transparent)

self.setPalette(palette)

def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.fillRect(event.rect(), QBrush(QColor(255, 255, 255, 127)))
painter.drawLine(self.width()/8, self.height()/8, 7*self.width()/8, 7*self.height()/8)
painter.drawLine(self.width()/8, 7*self.height()/8, 7*self.width()/8, self.height()/8)
painter.setPen(QPen(Qt.NoPen))

class windowOverlay(QWidget):
def __init__(self, parent=None):
super(windowOverlay, self).__init__(parent)

self.editor = QTextEdit()
self.editor.setPlainText("OVERLAY"*100)

self.button = QPushButton("Toggle Overlay")

self.verticalLayout = QVBoxLayout(self)
self.verticalLayout.addWidget(self.editor)
self.verticalLayout.addWidget(self.button)

self.overlay = overlay(self.editor)
self.overlay.hide()

self.button.clicked.connect(lambda: self.overlay.setVisible(False) if self.overlay.isVisible() else self.overlay.setVisible(True))

def resizeEvent(self, event):
self.overlay.resize(event.size())
event.accept()

if __name__ == "__main__":
import sys

app = QApplication(sys.argv)
main = windowOverlay()
main.show()
sys.exit(app.exec_())

要覆盖文本,请使用如下内容:

class overlayLabel(QLabel):    
def __init__(self, parent=None):
super(overlayLabel, self).__init__(parent)
self.setAlignment(Qt.AlignHCenter|Qt.AlignVCenter)

self.setText("OVERLAY TEXT")

关于python - 在 pyqt4 中的特定小部件(透明标签)上覆盖文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14042060/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com