在python pyqt4中显式设置样式表? [英] explicitly setting style sheet in python pyqt4?
问题描述
在pyqt中设置样式表的标准方法是这样的:
In pyqt standard way for setting style sheet is like this:
MainWindow.setStyleSheet(_fromUtf8("/*\n"
"gridline-color: rgb(85, 170, 255);\n"
"QToolTip\n"
"{\n"
" border: 1px solid #76797C;\n"
" background-color: rgb(90, 102, 117);;\n"
" color: white;\n"
" padding: 5px;\n"
" opacity: 200;\n"
"}\n"
"#label_3{\n"
" background-color:rgb(90, 102, 117);\n"
" color:white;\n"
" padding-left:20px;\n"
" \n"
"}\n"
"#label_2{\n"
" color:white;\n"
" padding-left:20px;\n"
" \n"
"}\n"
但是就像我们在 html 中链接样式表一样.我们不能在 pyqt 中做同样的事情吗?它有助于组织事情.
But like we link the stylesheet in html
<link rel="stylesheet" href="style.css">
.can't we do the same in pyqt?It helps in organizing the things.
推荐答案
目前只有两种主要的方式来设置样式表.第一种是使用setStyleSheet
方法:
There are currently only two main ways to set a stylesheet. The first is to use the setStyleSheet
method:
widget.setStyleSheet("""
QToolTip {
border: 1px solid #76797C;
background-color: rgb(90, 102, 117);
color: white;
padding: 5px;
opacity: 200;
}
""")
这只需要一个字符串,因此需要从文件中显式读取外部资源,或从模块导入.
This will only take a string, so an external resource would need to be explicitly read from a file, or imported from a module.
第二种方法是使用-stylesheet
命令行参数,允许将外部qss资源指定为路径:
The second method is to use the -stylesheet
command-line argument, which allows an external qss resource to be specified as a path:
python myapp.py -stylesheet style.qss
这开启了一个诱人的黑客攻击的可能性,因为操作传递给 QApplication
构造函数的参数并显式插入默认样式表很容易:
This opens up the possibility of a tempting hack, since it is easy enough to manipulate the args passed to the QApplication
constructor, and explicitly insert a default stylesheet:
import sys
args = list(sys.argv)
args[1:1] = ['-stylesheet', 'style.qss']
app = QtGui.QApplication(args)
(在列表的开头插入额外的参数确保用户仍然可以使用他们自己的样式表覆盖默认值).
(Inserting the extra arguments at the beginning of the list ensures that it is still possible for the user to override the default with their own stylesheet).
这篇关于在python pyqt4中显式设置样式表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!