我想知道如何在PyQt5中用Python3改变QPushButton的颜色。
我可以设置背景色和悬停色,但程序保持了焦点色或悬停色。
强焦色为默认颜色。我找不到任何关于它的东西,我在我的qss文件中加载了StyleSheet。
main.py。
喜欢
QPushButton{
background-color: #9de650; #works fine
}
QPushButton:hover{
background-color: green; #works fine
}
QPushButton:focus {
background-color: red; #doesnt work if the focus color is the color of the button you can
} # change using tab to navigate among a list of buttons
QPushButton:strong:focus { #doenst work
background-color: red;
}
QPushButton:pressed { #doesnt work get the tab color
background-color: green;
}
我不明白为什么QPushButton:pressed不工作。我的脚本上的按钮没有连接或点击的功能,我使用的是Designer,QPushButton:pressed的颜色和设计器预览中的颜色一样。我使用的是Designer,我的QPushButton上的颜色和设计器预览中的颜色是一样的。
如果我把qss代码直接保存到设计器的ui文件中,也是同样的行为。
当我在我的按钮列表之间切换时,我可以看到它们在很短的时间内变成红色。
在变成蓝色之前,红色是我的焦点选择的颜色。
任何提示?
musicmante q回答了我的问题。
如我所想 如果widget的css定义不完整,Breeze会覆盖一些颜色和行为;奇怪的是你说button:pressed不起作用,但在我的测试中却起作用了。不管怎么说,Fusion是通常处理样式表比较好的样式,如果用那个没有问题,你可以用app.setStyle(QtWidgets.QStyleFactory.create('Fusion'))来设置应用样式就可以了。