【问题标题】:pyqt changing the color of a button when pressed using a stylesheetpyqt在使用样式表按下时改变按钮的颜色
【发布时间】:2014-02-13 04:19:30
【问题描述】:

我正在尝试在按下按钮时更改按钮的颜色,并且看起来可以直接在样式表中完成,而无需执行功能然后连接它。

http://qt.developpez.com/doc/4.7-snapshot/stylesheet-examples/ 显示这个例子:

QPushButton#evilButton {
    background-color: red;
    border-style: outset;
    border-width: 2px;
    border-radius: 10px;
    border-color: beige;
    font: bold 14px;
    min-width: 10em;
    padding: 6px;
}
QPushButton#evilButton:pressed {
    background-color: rgb(224, 0, 0);
    border-style: inset;
}

如何将它翻译成 python pyqt?

【问题讨论】:

    标签: button colors pyqt stylesheet


    【解决方案1】:

    我能够让它像这样工作,这样颜色按钮是蓝色的,当按下时变成红色

        btn_text = QString("this font color")
        btn = QPushButton(btn_text)
    
        btn.setStyleSheet("QPushButton { background-color: blue }"
                          "QPushButton:pressed { background-color: red }" )
    

    【讨论】:

      【解决方案2】:

      不需要翻译,它是一个样式表:qt 使用的文本文档(因此也被 pyqt 使用,因为 pyqt 调用 qt)。例如:

      self.groupBox.setStyleSheet("""
             QGroupBox 
             { 
                 background-color: rgb(255, 255,255); 
                 border:1px solid rgb(255, 170, 255); 
             }
             """
      )
      

      看看http://vimeo.com/50033293

      【讨论】:

      • 在 SO 上,您通过投票表示感谢,并通过接受让其他人知道哪个答案最有用,如果没有用,请发表评论以防人们可以编辑
      【解决方案3】:
      self.button.setStyleSheet('background-color:#FFFFFF;color:#000000;')    
      
      nameofbutton.setStyleSheet('background-color:#FFFFFF;color:#000000;')   
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-28
        • 2023-02-22
        • 1970-01-01
        • 2018-11-12
        • 1970-01-01
        • 2016-09-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多