gpt4 book ai didi

python - 如何让QTabWidget看起来透明?

转载 作者:可可西里 更新时间:2023-11-01 10:34:44 30 4
gpt4 key购买 nike

我想使用 python 制作一个简单的 GUI 桌面应用程序。

我制作了一个带有背景图像的简单窗口,并在右上角添加了一个选项卡小部件。它工作正常。但是标签栏和标签内容区域是白色的。

我想要的是选项卡小部件的背景显示其父窗口的背景图像(这意味着它是透明的)。但我不知道该怎么做。

这是我的工作环境、代码和屏幕截图:

工作环境:

  • Windows 7
  • python 3.4
  • PyQt5 5.5

源代码:

# coding: utf-8

# There are some unnecessary module.

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QTabWidget, QWidget, QLabel, QHBoxLayout, QVBoxLayout, QGridLayout
from PyQt5.QtGui import QPixmap, QPalette, QBrush, QColor
from PyQt5.QtCore import Qt

class Example(QWidget):

def __init__(self):
super().__init__()
self.initUI()

def initUI(self):

tab1 = QWidget()
tab2 = QWidget()

vbox1 = self.makeTab1()
vbox2 = self.makeTab2()

tab1.setLayout(vbox1)
tab2.setLayout(vbox2)

tabs = QTabWidget()
tabs.addTab(tab1, "firstTab")
tabs.addTab(tab2, "secondTab")

picLabel = QLabel(self)
picFile = 'background_img.jpg'
pixmap = QPixmap(picFile)
palette = QPalette()
palette.setBrush(QPalette.Background, QBrush(pixmap))

hboxEX = QHBoxLayout()
hboxEX.addStretch(2)
hboxEX.addWidget(tabs)
hboxEX.setStretchFactor(tabs, 1)

vboxEX = QVBoxLayout()
vboxEX.addStretch(1)
vboxEX.addLayout(hboxEX)
vboxEX.setStretchFactor(hboxEX, 1)

self.setLayout(vboxEX)

self.setPalette(palette)
self.resize(pixmap.width(), pixmap.height())
self.show()

def makeTab1(self):
lbl1 = QLabel(self)
lbl2 = QLabel(self)
lbl3 = QLabel(self)

lbl1.setText("<a href=\"http://www.google.com\">Google</a>")
lbl2.setText("<a href=\"https://www.wikipedia.org/\">WikiPedia</a>")
lbl3.setText("<a href=\"http://www.stackoverflow.com\">StackOverflow</a>")

lbl1.setOpenExternalLinks(True)
lbl2.setOpenExternalLinks(True)
lbl3.setOpenExternalLinks(True)

vbox1 = QVBoxLayout()
vbox1.addWidget(lbl1)
vbox1.addWidget(lbl2)
vbox1.addWidget(lbl3)

return vbox1

def makeTab2(self):
lbl4 = QLabel(self)
lbl5 = QLabel(self)
lbl6 = QLabel(self)

lbl4.setText("<a href=\"https://www.python.org/\">Python</a>")
lbl5.setText("<a href=\"https://www.centos.org/\">CentOS</a>")
lbl6.setText("<a href=\"https://mariadb.org/\">MariaDB</a>")

lbl4.setOpenExternalLinks(True)
lbl5.setOpenExternalLinks(True)
lbl6.setOpenExternalLinks(True)

vbox2 = QVBoxLayout()
vbox2.addWidget(lbl4)
vbox2.addWidget(lbl5)
vbox2.addWidget(lbl6)

return vbox2

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

屏幕截图:

screenshot

我尝试过的:

我添加了这些代码,它改变了标签的颜色。但它并没有使标签区域看起来透明:

    tabPalette = QPalette()
tabPalette.setColor(QPalette.Background, QColor("cyan"))
tab1.setAutoFillBackground(True)
tab1.setPalette(tabPalette)

最佳答案

我认为这是 Windows 的问题。您可能想尝试将您的应用程序切换为不同的样式。

请参阅 PyQt 引用页面 http://pyqt.sourceforge.net/Docs/PyQt4/qstyle.html

一些示例代码:

def changeStyle(self, styleName):
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create(styleName))
self.changePalette()

调用它:

changeStyle("plastique")

关于python - 如何让QTabWidget看起来透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33491758/

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