PyQt pushButton setstylesheet

时间:2016-07-27 10:13:36

标签: pyqt qpushbutton

有人可以回答我的问题吗?

1)我希望当我按下按钮时,按钮的颜色会永久变化,直到我再次按下它并恢复原来的颜色。

2)如何将按钮的形状更改为圆形?

1 个答案:

答案 0 :(得分:2)

#PyQt pushButton setstylesheet
#This is the example code for PyQt pushButton setstylesheet
#If your are not expecting this answer, sorry.

#QPushButton color change while clicking on same QPushButton
#Circle QPushButton

import sys, os
from PyQt4 import QtGui, QtCore

class Window (QtGui.QWidget):
    def __init__(self, parent=None):        

         super(Window, self).__init__(parent)        

         self.pushButton = QtGui.QPushButton(self)
         self.pushButton.setObjectName('pushButton')
         self.pushButton.setGeometry (QtCore.QRect(20, 10, 250, 50))
         self.pushButton.setStyleSheet('background-color: rgb()')
         self.pushButton.setText ('Color')

         self.pushButton_2 = QtGui.QPushButton(self)
         self.pushButton_2.setObjectName('pushButton_2')     
         self.pushButton_2.setGeometry (QtCore.QRect(20, 70, 150, 150))
         self.pushButton_2.setText('Cricle')

         #width     = 150
         #height    = width  
         #border-radius = width/2
         #self.pushButton_2.setStyleSheet ('background-color: red;border-style: outset;border-width: 2px;border-radius: 200px;border-color: beige;font: bold 14px;min-width: 10em;padding: 6px;')       
         self.pushButton_2.setStyleSheet ('background-color: red; border-width: 2px; border-radius: 75px;')      


         self.resize(300, 240)       

         self.pushButton.clicked.connect (self.colorChange)
         self.pushButton_2.clicked.connect (self.cricle)

         self.currentColor  = 'default'


    def colorChange (self) :        
        if self.currentColor=='default' :        
            self.pushButton.setStyleSheet('background-color: red')            
            self.currentColor  = 'red'            
        else :
            self.pushButton.setStyleSheet('background-color: rgb()')
            self.currentColor  = 'default'            

    def cricle (self) :
        print 'Hai...............'  


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())

#Thanks, 
#Subin Gopi