Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Dynamic Color assignment and CSS
Forum Updated to NodeBB v4.3 + New Features

Dynamic Color assignment and CSS

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 3 Posters 558 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maxTim
    wrote on last edited by
    #1

    Hello,

    I'm using PyQt5 to play around with some guis. I previously used tkinter, but when I learned that you can import a CSS file, I flocked to qt. So with tkinter I can:

    from pathlib import Path
    
    def get_colors():
        return Path(Path('~/.cache/wal/colors').expanduser()).read_text().split('\n')
    

    and that returns a list I can call like:

    >>> colors = get_colors()
    >>> colors[0]
    #040405
    

    with qt, I have an external css file so I can quickly change it, or import it to different apps. I import it using:

    self.setStyleSheet(open('myCSS.css').read())
    

    Is there a way I can import the colors in a similar manner in qt and use them in the separate stylesheet?

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      The way you load the colors does not need to change. However I have the feeling that it is not the main question.

      Do you want to replace values in the stylesheet content ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M maxTim

        Hello,

        I'm using PyQt5 to play around with some guis. I previously used tkinter, but when I learned that you can import a CSS file, I flocked to qt. So with tkinter I can:

        from pathlib import Path
        
        def get_colors():
            return Path(Path('~/.cache/wal/colors').expanduser()).read_text().split('\n')
        

        and that returns a list I can call like:

        >>> colors = get_colors()
        >>> colors[0]
        #040405
        

        with qt, I have an external css file so I can quickly change it, or import it to different apps. I import it using:

        self.setStyleSheet(open('myCSS.css').read())
        

        Is there a way I can import the colors in a similar manner in qt and use them in the separate stylesheet?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @maxTim
        I'm not clear what you are asking/seeking to do. But from the title, "Dynamic Color assignment and CSS", if you are wanting to control which colors are used in the stylesheet you need to process the open('myCSS.css').read() to do some kind of substitution/insertion of colors as desired, passing the result on to the setStyleSheet().

        M 1 Reply Last reply
        0
        • JonBJ JonB

          @maxTim
          I'm not clear what you are asking/seeking to do. But from the title, "Dynamic Color assignment and CSS", if you are wanting to control which colors are used in the stylesheet you need to process the open('myCSS.css').read() to do some kind of substitution/insertion of colors as desired, passing the result on to the setStyleSheet().

          M Offline
          M Offline
          maxTim
          wrote on last edited by
          #4

          @JonB said in Dynamic Color assignment and CSS:

          you are wanting to control which colors are used in the stylesheet

          This is precisely what I'm trying to do. And you've pretty much answered how I'll do that I think.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maxTim
            wrote on last edited by maxTim
            #5

            And this is what I came up with:

            from pathlib import Path
            
            class Menu(QDialog):
                def __init__(self):
                    super().__init__()
                    self.initUI()
            
                def get_colors(self):
                    colors = Path(Path('~/.cache/wal/colors').expanduser()).read_text().split('\n')
                    css = open('vMenu.css').read()
                    for x in range(len(colors)):
                        css = css.replace(f'color{x}', colors[x])
                    return css
            
                def initUI(self):
                    # [ . . . ]
                    self.setStyleSheet(self.get_colors())
            

            vMenu.css

            QDialog {
                background-color: color0;
            }
            
            QPushButton, QGroupBox, QLabel {
                font-family: 'Swiss911 XCm BT';
                font-size: 24px;
                border: 0px;
                color: color2;
            }
            
            QPushButton {
                text-align: right;
            }
            
            QLabel#title {
                font-size: 32px;
                text-align: center;
            }
            

            colors file

            #040405
            #5B5F5B
            #9B544A
            #6E8B6A
            #ABA764
            #5D758B
            #709996
            #c3c9c3
            #888c88
            #5B5F5B
            #9B544A
            #6E8B6A
            #ABA764
            #5D758B
            #709996
            #c3c9c3
            
            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved