gpt4 book ai didi

python - 如何在 QTextEdit 中创建完全透明

转载 作者:太空宇宙 更新时间:2023-11-03 19:28:59 24 4
gpt4 key购买 nike

我已经尝试了很多天来找出一种方法来创建带有不透明文本的透明 Qtextedit。因为术语“透明度”通常是不明确的,所以我将 Qtextedit“透明度”定义为能够看到 Qtextedit 中的文本覆盖在主窗口正后方的任何内容上(例如桌面背景、Windows 媒体播放器等)。我希望能够设置各个级别的透明度并跨系统兼容,但这不是必需的。

我是一个极端的初学者,因为我只使用了 pyqt4 3 周,使用 python 3.x 几个月,这就是我一生中获得的所有编程经验。我一直在尝试破译有关此事的 Pyqt 文档,但它的编写方式似乎假设一个人已经成为一名 gui 程序员数十年,更不用说具备 C++ 知识了。此外,当在网上提出这个问题时,它似乎永远不会以以下方式解决:a)有据可查或b)可推广

这非常令人惊讶,因为这似乎是人们想要做的基本操作

这个解决方案有效,但除了显示透明图像之外似乎没有直接用处。我也不太理解它,因为简单地将基类从 QWidget 更改为 QMainWindow 会使整个事情失败

http://www.loopbacking.info/blog/2008/07/11/transparent-windows-howto/

以下链接体现了人们建议解决与此类似的问题的常见方法、它们的陷阱以及它们不起作用的原因,但不幸的是,它们使用 Qt 的 C++ 版本,并且对于我目前的技能来说也有点先进。

http://www.qtcentre.org/threads/18072-How-to-set-Qt-window-transparent

我的系统是 Windows 7 Ultimate 32 位,位于 Dell Latitude d830 上,配有 Quadro NVS 140,其驱动程序版本截至本文为最新版本 (Verde 275.33) 我的 Pyqt 版本是 4.8 (PyQt-Py3.2-x86-gpl) -4.8.5-1.exe Windows 32位安装程序)我也使用Python 3.2.1(开源版本)

我的代码的基本示例位于下面,相关(和失败的)行被注释掉:

当我尝试注释掉的代码时,我通常只看到黑色。另外,当我调整窗口大小时,黑暗会随机改变强度,并且主窗口的显示在最大化时似乎会被损坏。

对于此事的任何帮助,我将不胜感激!

import sys
import PyQt4
from PyQt4 import QtGui, QtCore

class Transparent(QtGui.QMainWindow):
def __init__(self,parent = None):
QtGui.QMainWindow.__init__(self,parent)
self.initialize()

def initialize(self):
#self.colorset(self,'Window',200,255,100,20)
#self.colorset(self,'Base',200,255,100,20)
#self.setBackgroundRole(QtGui.QPalette.Base)
#self.setAttribute(QtCore.Qt.WA_NoSystemBackground)
#self.setAutoFillBackground(True)
#self.mask()
self.setWindowTitle("Chernobyl-like Failure")

self.answerlabel = QtGui.QLabel('Text Response Display')
self.answerlabel.setFrameStyle(QtGui.QFrame.Panel | QtGui.QFrame.Raised)
self.answerlabel.setMinimumHeight(25)
self.questionlabel = QtGui.QLabel("Question:")
self.questionlabel.setFrameStyle(QtGui.QFrame.Panel | QtGui.QFrame.Raised)

self.questionbox = QtGui.QLineEdit()
self.questionbox.setMinimumWidth(500)

self.askbutton = QtGui.QPushButton("Ask it!")

self.historybox = QtGui.QTextEdit('Question & Answer history will be displayed here')
self.historybox.setReadOnly(True)
#self.colorset(self.historybox,'Base',200,255,100,127)
self.grid = QtGui.QGridLayout()
widgetlist = [['answerlabel',0,0,1,3],['questionlabel',1,0,1,1],
['questionbox',1,1,1,1],['askbutton',1,2,1,1],['historybox',2,0,1,3]]
for widget in widgetlist:
self.grid.addWidget(eval("self.{0}".format(widget[0])),*widget[1:])

self.centralwidget = QtGui.QFrame()
self.centralwidget.setFrameStyle(QtGui.QFrame.Box|QtGui.QFrame.Raised)
self.centralwidget.setLineWidth(5)
self.centralwidget.setLayout(self.grid)
#self.colorset(self.centralwidget,'Base',200,255,100,127)
self.setCentralWidget(self.centralwidget)

def colorset(self,widget,part,h,s,l,a):
pal = widget.palette()
color = QtGui.QColor()
color.setHsl(h,s,l,a)
pal.setColor(eval('QtGui.QPalette.{0}'.format(part)),color)
widget.setPalette(pal)

if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
main_window = Transparent()
main_window.show()
sys.exit(app.exec_())

最佳答案

要使主窗口透明,您必须设置 Qt.WA_TranslucentBackground 属性(使用 setAttribute(Qt.WA_TranslucentBackground))。在 Windows 下,您还必须在主窗口上设置 Qt.FramelessWindowHint 属性。根据docs但是,“用户无法通过窗口系统移动无边框窗口或调整其大小。”因此,如果您想要该功能,则必须手动实现它。这是thread给出一个 C++ 示例。

一旦有了透明的主窗口,您就可以通过将背景颜色设置为 RGBA 值来控制它和任何子窗口小部件的不透明度。这是一个愚蠢的例子,

from PyQt4 import QtGui, QtCore
import sys

class Main(QtGui.QMainWindow):
def __init__(self, parent=None):
super(Main, self).__init__(parent)

self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)

frame = QtGui.QFrame(parent=self)
frame.setStyleSheet("QFrame {background: rgba(0,255,0,20%)}")
box=QtGui.QHBoxLayout()

edit = QtGui.QTextEdit()
edit.setStyleSheet("background: rgba(0,0,255,20%)")
box.addWidget(edit)

edit2=QtGui.QTextEdit()
edit2.setStyleSheet("background: rgb(255,0,0)")
box.addWidget(edit2)
frame.setLayout(box)

pushbutton = QtGui.QPushButton('Quit')
pushbutton.clicked.connect(self.close)
box.addWidget(pushbutton)

self.setCentralWidget(frame)

if __name__ == '__main__':

app = QtGui.QApplication(sys.argv)
main = Main()
main.show()

app.exec_()

关于python - 如何在 QTextEdit 中创建完全透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6939063/

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