Pyside setStyleSheet()和长行?

时间:2014-08-05 17:24:16

标签: python pyside

我还没有找到一种方法来削减这些部分的长代码行。无论从哪个角度切割到下一行,它都会破坏弦乐。 有没有办法以某种方式将这些切入短线?

self.setStyleSheet('ApplicationWindow { background-color: rgba(10, 10, 10, 10); } ButtonDefault { background-color: rgb(255, 10, 10); color: rgb(255, 255, 255); }')

我自己的解决方案是将样式表移到单独的.css文件中,并将整个事物作为一个简单的字符串从中削减。以这种方式开发也更好,但这种方法听起来合理吗?

    stylesheet_string = ''

    # Opens external stylesheet file
    with open('stylesheet.css', 'r') as stylesheet:
        for line in stylesheet:
            line.replace('\n', ' ')
            line.replace('\t', ' ')

            stylesheet_string = stylesheet_string+line

    self.setStyleSheet(stylesheet_string)

2 个答案:

答案 0 :(得分:1)

我有点困惑,因为你的示例代码行没有将字符串传递给setStyleSheet。无论如何,你应该能够做到这样的事情:

self.setStyleSheet('ApplicationWindow { background-color: rgba(10, 10, 10, 10); } '
                   'ButtonDefault { background-color: rgb(255, 10, 10); '
                       'color: rgb(255, 255, 255); }')

如果您希望在外部存储.css文件,那么您所做的事情听起来很合理。

答案 1 :(得分:1)

为了获得更短的代码行,请参阅How can I break up this long line in Python?

对于样式表,特别是如果您希望它们从文件加载,只需加载它们并且不要替换任何内容。它有效!

with open('path', 'r', encoding='utf-8') as file:
    style_sheet = file.read()
app.setStyleSheet(style_sheet)

显示其有效的示例:

from PySide import QtGui

app = QtGui.QApplication([])
window = QtGui.QMainWindow()
window.setStyleSheet('/*\n some comment */\n\nbackground-color:black;\n\r\t\n')
window.show()

app.exec_()
尽管样式表字符串中有许多换行符和注释,

仍将显示黑色窗口。

相关问题