python Qt5 实战(一)按钮颜色

系统 1677 0

  工作中,工具用到了python Qt5,涉及到了按钮颜色,这里就做个总结。也顺便给要用这块的同仁抛出来一个砖头,把大牛引出来做个指导。

一般设置按钮的颜色有三种表达:如下所示:具体的怎么使用,估计要看一下用例就清楚了。

            
              QPushButton button1, button2, button3;

button1.setStyleSheet(
            
            
              "
            
            
              background-color: red
            
            
              "
            
            
              );

button2.setStyleSheet(
            
            
              "
            
            
              background-color:#ff0000;
            
            
              "
            
            
              );

button3.setStyleSheet(
            
            
              "
            
            
              background-color:rgb(255,0,0)
            
            
              "
            
            );
          

  接下来上一个例子:

            
               1
            
            
              import
            
            
               sys

            
            
               2
            
            
              from
            
             PyQt5.QtWidgets 
            
              import
            
            
               QApplication, QWidget, QPushButton

            
            
               3
            
            
              from
            
             PyQt5.QtGui 
            
              import
            
            
               QIcon

            
            
               4
            
            
              from
            
             PyQt5.QtCore 
            
              import
            
            
               pyqtSlot

            
            
               5
            
            
              global
            
            
               ival

            
            
               6
            
            
              class
            
            
               App(QWidget):

            
            
               7
            
            
               8
            
            
              def
            
            
              __init__
            
            
              (self):

            
            
               9
            
                     super().
            
              __init__
            
            
              ()

            
            
              10
            
                     self.title = 
            
              '
            
            
              PyQt5 button color:https://www.cnblogs.com/dylancao/
            
            
              '
            
            
              11
            
                     self.left = 10

            
              12
            
                     self.top = 10

            
              13
            
                     self.width = 320

            
              14
            
                     self.height = 200

            
              15
            
            
                      self.initUI()

            
            
              16
            
            
              global
            
            
               ival

            
            
              17
            
                     ival =
            
               0

            
            
              18
            
            
              19
            
            
              def
            
            
               initUI(self):

            
            
              20
            
            
                      self.setWindowTitle(self.title)

            
            
              21
            
            
                      self.setGeometry(self.left, self.top, self.width, self.height)

            
            
              22
            
            
              23
            
                     self.button = QPushButton(
            
              '
            
            
              Color
            
            
              '
            
            
              , self)

            
            
              24
            
                     self.button.setToolTip(
            
              '
            
            
              This is an example button about color 
            
            
              '
            
            
              )

            
            
              25
            
                     self.button.setStyleSheet(
            
              "
            
            
              background-color: red
            
            
              "
            
            
              )

            
            
              26
            
                     self.button.move(100,70
            
              )

            
            
              27
            
            
                      self.button.clicked.connect(self.on_click)

            
            
              28
            
            
              29
            
            
                      self.show()

            
            
              30
            
            
              31
            
            
                  @pyqtSlot()

            
            
              32
            
            
              def
            
            
               on_click(self):

            
            
              33
            
            
              global
            
            
               ival

            
            
              34
            
                     ival += 1

            
              35
            
            
              if
            
             ival == 1
            
              :

            
            
              36
            
                             self.button.setStyleSheet(
            
              "
            
            
              background-color: red
            
            
              "
            
            
              )

            
            
              37
            
            
              elif
            
             ival == 2
            
              :

            
            
              38
            
                             self.button.setStyleSheet(
            
              "
            
            
              background-color: #ffff00;
            
            
              "
            
            
              )

            
            
              39
            
            
              elif
            
             ival == 3
            
              :

            
            
              40
            
                             ival =
            
               0

            
            
              41
            
                             self.button.setStyleSheet(
            
              "
            
            
              background-color: rgb(0,255,255)
            
            
              "
            
            
              )

            
            
              42
            
            
              43
            
            
              print
            
            (
            
              '
            
            
              PyQt5 button click:
            
            
              '
            
            
              ,ival)

            
            
              44
            
            
              45
            
            
              if
            
            
              __name__
            
             == 
            
              '
            
            
              __main__
            
            
              '
            
            
              :

            
            
              46
            
                 app =
            
               QApplication(sys.argv)

            
            
              47
            
                 ex =
            
               App()

            
            
              48
            
                 sys.exit(app.exec_())
          

  运行的结果:


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论