2016-07-27 49 views
0

有人可以請回答我的問題嗎?PyQt pushButton setstylesheet

1)我想要當我按下一個按鈕,按鈕的顏色永久改變,直到我再次推它,它回到原來的顏色。

2)我怎麼能改變他的按鈕的形狀,例如一個圓?

+0

的[Qt的樣式表的文檔(https://doc-snapshots.qt.io/qt5-5.6/stylesheet-examples.html#customizing-qpushbutton)有一些例子。 –

回答

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