1

I haven't found a way to cut the long code lines on these parts. No matter from what point I cut the line to the next, it breaks the string. Is there a way to cut these in to shorter lines somehow?

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

My own solution was to move stylesheets in to seperate .css file and pare the whole thing from there as a simple string. It's nicer to develop that way too, but does this method sound reasonable?

    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 Answers2

2

I am a little confused, because your example code line is not passing a string to setStyleSheet. In any case, you should be able to do something like this:

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

If you would rather store your .css file externally, what you are doing sounds reasonable.

Becca codes
  • 542
  • 1
  • 4
  • 14
  • Ah I forgot to paste the line creating the variable stylesheet_string. The last line passes the whole string to setStyleSheet(). It does work, however I'm a little concerned my way might be a bad way. Thank you for the answer! – Mikko-Pentti Einari Eronen Aug 06 '14 at 04:09
  • 1
    I actually meant your very first code example, where there are no quotes around the string. But no worries. I think either way is fine. Sometimes parsing a file can be a little tricky if you have to worry about special characters and such, but it can worth it. Like if you don't want to edit your code file every time the stylesheet changes. :-) – Becca codes Aug 06 '14 at 14:29
  • Ah yes I forgot to add the '' marks in here, sorry about that :) – Mikko-Pentti Einari Eronen Aug 07 '14 at 07:20
2

For having shorter code lines in general see How can I break up this long line in Python?.

For stylesheets in particular and if you want them to load from file just load them and do not replace anything. It works!

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

An example that shows that it works:

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_()

will show a black window despite the many newlines and comments in the stylesheet string.

Community
  • 1
  • 1
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104