简体   繁体   English

我在组织 PyQt5 Gui 代码时遇到问题

[英]I'm facing a problem organizing my PyQt5 Gui code

I'm writing a Pyqt5 GUI code but I'm facing a problem with organizing my code especially by setStyle because the style is to long what should I do?我正在编写 Pyqt5 GUI 代码,但我在组织代码时遇到了问题,尤其是通过 setStyle,因为样式太长了我该怎么办? any advices Example任何建议示例

setStyleSheet("QPushButton{ color: #b1b1b1;"
                            " background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #565656, stop: 0.1 #525252, stop: 0.5 #4e4e4e, stop: 0.9 #4a4a4a, stop: 1 #464646);"
                            " border-width: 1px;"
                            " border-color: #1e1e1e;"
                            " border-style: solid;"
                            " border-radius: 6;"
                             "padding: 3px;"
                             "font-size: 20px;"
                            " padding-left: 5px;"
                             "padding-right: 5px;"
                             "min-width: 40px;"
                             "} QPushButton::hover"
                             "{"
                             "background-color : #444444; color : green"
                             "}"
                            " QPushButton:pressed"
                             "{"
                             "background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 
                #2d2d2d, stop: 0.1 #2b2b2b, stop: 0.5 #292929, stop: 0.9 #282828, stop: 1 #252525);"
                             "}"
                             ";") 

Qt Style Sheet is based on CSS 2.1 so you can use this format: Qt 样式表基于 CSS 2.1,因此您可以使用以下格式:

style.css样式.css

QPushButton {
    color: #b1b1b1;
    background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #565656, stop: 0.1 #525252, stop: 0.5 #4e4e4e, stop: 0.9 #4a4a4a, stop: 1 #464646);
    border-width: 1px;
    border-color: #1e1e1e;
    border-style: solid;
    border-radius: 6;
    padding: 3px;
    font-size: 20px;
    padding-left: 5px;
    padding-right: 5px;
    min-width: 40px;
}

QPushButton::hover {
    background-color: #444444;
    color: green
}

QPushButton:pressed {
    background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: #2d2d2d, stop: 0.1 #2b2b2b, stop: 0.5 #292929, stop: 0.9 #282828, stop: 1 #252525);
}

* .py * .py

with open("style.css", "r") as f:
   app.setStyleSheet(f.read())

There's nothing wrong with having a long stylesheet.有一个长的样式表并没有错。 I often have a large stylesheet string in the top level widget of my app.我的应用程序的顶级小部件中经常有一个大的样式表字符串。 In Python, you can do a multiline string using three single quotes:在 Python 中,您可以使用三个单引号来执行多行字符串:

style = '''
           QPushButton {
               color: #b1b1b1;
               background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #565656, stop: 0.1 #525252, stop: 0.5 #4e4e4e, stop: 0.9 #4a4a4a, stop: 1 #464646);
               border-width: 1px;
               border-color: #1e1e1e;
               border-style: solid;
               border-radius: 6;
               padding: 3px;
               font-size: 20px;
               padding-left: 5px;
               padding-right: 5px;
               min-width: 40px;
           }

           QPushButton::hover {
               background-color: #444444;
               color: green;
           }

           QPushButton:pressed {
               background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: #2d2d2d, stop: 0.1 #2b2b2b, stop: 0.5 #292929, stop: 0.9 #282828, stop: 1 #252525);
           } '''
widget.setStyleSheet(style)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM