[英]PyQt changing QPushButton background color without resetting style
我想在不更改/重置整個樣式的情況下更改按鈕的背景顏色和文本顏色。
目前,我正在使用這個:
dl_btt.setStyleSheet("background-color: green; color: white")
但這改變了按鈕的整體風格:
由於默認樣式如下所示:
我想要這樣的東西:
所以,如果我要更改操作系統,我希望它使用默認樣式並且只更改背景顏色。
我可以在不手動復制樣式的情況下做到這一點嗎?
編輯:
@Controlix 感謝您為我指明正確的方向。 但是,我似乎無法更改背景顏色。
我只能使用以下方法更改邊框和文本顏色:
dl_btt = DownloadButton(self, "Skini")
#dl_btt.setStyleSheet("background-color: green; color: white")
#dl_btt.setPalette(QtGui.QPalette(QtCore.Qt.green))
palette = dl_btt.palette()
role = dl_btt.foregroundRole()
palette.setColor(role, QtGui.QColor('white'))
role = dl_btt.backgroundRole()
palette.setColor(role, QtGui.QColor('green'))
dl_btt.setAutoFillBackground(True)
dl_btt.setPalette(palette)
這給了我這個結果:
搜索給了我相同或相似的代碼片段,這些片段沒有按照我的預期去做。
編輯 2 :
我放棄了這個搜索並使用了樣式表,重建了我認為合適的樣式。
不過我仍然想知道......在 PyQt 中,是否有可能在僅更改小部件的某些部分的同時復制本機樣式?
編輯 3
我試過:
palette = dl_btt.palette()
role = dl_btt.foregroundRole()
palette.setColor(role, QtGui.QColor('white'))
role = dl_btt.buttonRole()
palette.setColor(role, QtGui.QColor('green'))
dl_btt.setAutoFillBackground(True)
dl_btt.setPalette(palette)
但我得到了這個錯誤:
AttributeError: 'DownloadButton' object has no attribute 'buttonRole'
如何訪問按鈕角色? 怎么稱呼?
以防萬一您需要它,這里是 DownloadButton 類:
class DownloadButton(QtGui.QPushButton):
def __init__(self, master, *args, **kwargs):
super(DownloadButton, self).__init__(*args, **kwargs)
self.master = master
def mousePressEvent(self, ev):
self.master.startDownload()
我試過這個是有效的。 我已經在 UI 部分設置了按鈕樣式。 如果我只想更改按鈕的顏色,則以下有效。
以下工作雖然不是一種優雅的方式:
btns = ['self.hBeamBtn','self.lBeamBtn','self.allTestBtn','self.prnStatusBtn']
for btn in btns:
if str(btn_name) == str(btn):
styl = btn+'.setStyleSheet("font: bold;background-color: red;font-size: 12px;height: 28px;width: 80px;")'
eval(styl)
這是一種愚蠢但有效的方法:
def change_button_color(button, color):
"""change_button_color
Change a button's color
:param button: target button
:type button: QPushButton
:param color: new color (any format)
:type color: str
:return: None
"""
style_sheet = button.styleSheet()
pairs = [pair.replace(' ', '') for pair in style_sheet.split(';') if pair]
style_dict = {}
for pair in pairs:
key, value = pair.split(':')
style_dict[key] = value
style_dict['background-color'] = color
style_sheet = '{}'.format(style_dict)
chars_to_remove = ('{', '}', '\'')
for char in chars_to_remove:
style_sheet = style_sheet.replace(char, '')
style_sheet = style_sheet.replace(',', ';')
button.setStyleSheet(style_sheet)
有了這個答案,我設法將一組按鈕的顏色設置為淺藍色,並將其他未選擇的按鈕重置為默認值。
假設您的主窗口存儲了默認調色板,您可以訪問它並使用self.palette().button().color()
再次將按鈕的顏色設置為默認值。
# Reset to default colors
for btn in self.btn_group.buttons():
pal = btn.palette()
pal.setColor(QPalette.Button, self.palette().button().color())
btn.setPalette(pal)
btn.update()
# Except for selected button
btn = self.btn_group.checkedButton()
pal = btn.palette()
pal.setColor(QPalette.Button, QColor.fromRgb(173, 216, 230))
btn.setPalette(pal)
btn.update()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.