2015-11-03 134 views
2

我想用python製作一個簡單的GUI桌面應用程序。如何讓QTabWidget看起來透明?

我製作了一個帶有背景圖片的簡單窗口,並在右上角添加了一個tab-widget。它工作正常。但是標籤欄和標籤內容區域是白色的。

我想要的是tab-widget的背景顯示它是父窗口的背景圖像(這意味着它是透明的)。但我不知道該怎麼做。

這是我的工作環境,代碼和屏幕截圖:

工作環境

  • 的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) 

回答

1

我認爲這是一個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") 
+0

而在答案的鏈接可能會解決這個問題。你應該考慮在這個答案中添加一些代碼。 –

+0

我按照建議添加了一個代碼示例。 –

+0

謝謝凱文Eaverquepedo。我正在研究如何採用你的答案來處理我的代碼。你的回答給我解決了一個問題。再次感謝你。 – passion053