2016-07-27 9 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 

 Смежные вопросы

  • Нет связанных вопросов^_^