由於PyQt5的資料比較少,這里主要是記錄下我的學習經歷
lable1.setAutoFillBackground(True)
很早之前就開始遇到這個問題,就是在給label設置背景的時候,總感覺這個屬性沒有用,今天才發現這個屬性是需要和調色板結合起來一起使用才行,下面先看下這段代碼
#初始化標簽控件
lable1.setText("文本標簽")
lable1.setAutoFillBackground(True)
palette = QPalette()
palette.setColor(QPalette.Window, Qt.red)
lable1.setPalette(palette)
lable1.setAlignment(Qt.AlignCenter)
上面的顯示結果如下圖

解釋下上面的代碼,setAutoFillBackground(True)表示的是自動填充背景,如果想使用后面的代碼。這里必須要設置為True
palette = QPalette(),這里是實例化一個調色板對象,大家可以這樣考慮,你想給label設置背景那么肯定需要一個顏色器來生成,這里的QPalette就是這個顏色器
palette.setColor(QPalette.Window, Qt.red)這里就是顏色器設置顏色
lable1.setPalette(palette)這里就是想顏色器上的顏色整合到label上去
當然這樣的話是可以設置label的背景顏色,但是只能設置紅綠藍這些基本的顏色,在項目開發的時候肯定是設置自定義的詳細顏色,那么我們可以采取下面的方法設置
background_color = QColor()
background_color.setNamedColor('#282821')
#初始化標簽控件
lable1.setText("文本標簽")
lable1.setAutoFillBackground(True)
palette = QPalette()
palette.setColor(QPalette.Window, background_color)
lable1.setPalette(palette)
lable1.setAlignment(Qt.AlignCenter)
其實大家也可以看到
background_color = QColor()
background_color.setNamedColor(‘#282821’)
這兩句就是在設置我們的自定義顏色值
palette.setColor(QPalette.Window, background_color)這個就是應用我們的顏色值,具體效果如下圖
下面是完整的代碼
import PyQt5
import sys
import requests
from PyQt5.QtGui import QPalette, QPixmap, QColor
from PyQt5.QtWidgets import QWidget, QLabel, QVBoxLayout, QApplication
from PyQt5.QtCore import Qt
# http://imgs.technews.cn/wp-content/uploads/2014/10/Baidu.jpg
class WindowDemo(QWidget):
def __init__(self):
super().__init__()
lable1 = QLabel(self)
lable2 = QLabel(self)
lable3 = QLabel(self)
lable4 = QLabel(self)
url = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1516814788757&di=b719cdd79b84b6f563a36617911e2cf8&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20180112%2Ff309b61eb3bd44e6956948de77f78ec3.jpg'
req = requests.get(url)
photo = QPixmap()
photo.loadFromData(req.content)
background_color = QColor()
background_color.setNamedColor('#282821')
#初始化標簽控件
lable1.setText("文本標簽")
lable1.setAutoFillBackground(True)
palette = QPalette()
palette.setColor(QPalette.Window, background_color)
lable1.setPalette(palette)
lable1.setAlignment(Qt.AlignCenter)
lable2.setText("<a href='#'>歡迎使用PythonGUI應用</a>")
lable3.setAlignment(Qt.AlignCenter)
lable3.setToolTip("這個一個圖片標簽")
lable3.setPixmap(photo)
lable4.setText("<a href='http://www.baidu.com/'>WelCome to Baidu</a>")
lable4.setAlignment(Qt.AlignRight)
lable4.setToolTip("這是一個超鏈接標簽")
lable4.setAutoFillBackground(True)
palette = QPalette()
palette.setColor(QPalette.Window, Qt.red)
lable4.setPalette(palette)
lable4.setAlignment(Qt.AlignCenter)
vbox = QVBoxLayout()
vbox.addWidget(lable1)
vbox.setStretch(1,1)
vbox.addWidget(lable2)
vbox.setStretch(1, 1)
# vbox.addStretch()
vbox.addWidget(lable3)
vbox.setStretch(1, 1)
# vbox.addStretch()
# vbox.addStretch()
vbox.addWidget(lable4)
#允許label1訪問超鏈接
lable1.setOpenExternalLinks(True)
#允許label4訪問超鏈接並且打開瀏覽器訪問,設置了False則不會訪問網頁
lable4.setOpenExternalLinks(True)
lable4.linkActivated.connect(link_clicked)
lable2.linkHovered.connect(link_hovered)
lable1.setTextInteractionFlags(Qt.TextSelectableByMouse)
self.setLayout(vbox)
self.setWindowTitle("例子")
def link_clicked():
print("鼠標點擊了label標簽")
def link_hovered():
print("鼠標畫過了lable2標簽")
if __name__=="__main__":
app = QApplication(sys.argv)
win = WindowDemo()
win.show()
app.exit(app.exec_())

