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

2024-10-01 19:14:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个Pyqt5 GUI代码,但是我在组织代码时遇到了一个问题,特别是通过setStyle,因为样式太长了,我该怎么办?有什么建议吗 范例

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);"
                             "}"
                             ";") 

Tags: 代码guiwidthpyqt5colorbackgroundstopx1
2条回答

Qt样式表基于CSS 2.1,因此您可以使用以下格式:

style.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

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

有一个长的样式表没有什么错。我经常在我的应用程序的顶级小部件中有一个大的样式表字符串。在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)

相关问题 更多 >

    热门问题