生成具有三态背景图片的按钮

 

生成具有三态背景图片的按钮

 

class PussButton:
    
    #
    # 生成具有三态背景图片的按钮
    #
    def init(self, *imgPath):
            from PyQt5.QtWidgets import  QPushButton
            from PyQt5.QtGui import  QPixmap

            length = len(imgPath)            
            
            if length==3:
                nomalPath = imgPath[0]
                hoverPath = imgPath[1]
                pressedPath = imgPath[2]
            else:
                pressedPath=hoverPath=nomalPath = imgPath[0]
            
            pixmap_mask=QPixmap('./img/1.png')
            but = QPushButton('')
            
            print('pressedPath', pressedPath,imgPath )
            
            #调整按钮的大小,使其和背景的大小一致
            but.setFixedSize( pixmap_mask.width(), pixmap_mask.height() )
            
            # 图片的三种状态
            but.setStyleSheet("QPushButton{border-image:url("+nomalPath+");}"
                              "QPushButton:hover{border-image:url("+hoverPath+");}"
                              "QPushButton:pressed{border-image:url("+pressedPath+");}")
            #
            # 设置遮罩,如果不设置遮罩,PNG图片的空白部分也可以点击的
            #     
            but.setMask(pixmap_mask.createHeuristicMask())    
            
            return but
    
    def init2(self):
        
            from PyQt5.QtWidgets import  QPushButton
            from PyQt5.QtGui import  QPixmap, QIcon
            from PyQt5.QtCore import QSize
            pixmap_mask=QPixmap('./img/1.png')
            but = QPushButton('')
            but.setFixedSize( pixmap_mask.width(), pixmap_mask.height() );  
            but.setMask(pixmap_mask.createHeuristicMask());  
            icon = QIcon(pixmap_mask)
            
            but.setIcon(icon);  
            but.setIconSize(QSize(pixmap_mask.width(),pixmap_mask.height()));  
            but.setToolTip("hello")
            but.clicked.connect(self.print_click)
            
            return but
            
    def print_click(self):
            print('print_click')
            
    
    
    
    
    

if __name__ == '__main__':  

    import sys
    from PyQt5.QtWidgets import QApplication
    from PyQt5.QtWidgets import (QWidget,  QLabel, QVBoxLayout)
    from PyQt5.QtCore   import Qt
    from PyQt5.QtGui import  QPixmap, QImage
            
    app = QApplication(sys.argv)  
    
    
#####################################################   
    main= QWidget()

        
    layout_main = QVBoxLayout()
    layout_main.setAlignment( Qt.AlignLeft )
    main.setLayout(layout_main)
    
                             
                              
    label_top = QLabel()
    pixmap_top=QPixmap('./img/3.png')
    label_top.setPixmap(pixmap_top)
    layout_main.addWidget(label_top)
    
    tool = PussButton()
    paths = ['./img/1.png','./img/2.png','./img/3.png' ]
    but_3 = tool.init(*paths)
    layout_main.addWidget(but_3)
    
    but_4 =tool.init2()
    layout_main.addWidget(but_4)
    
    
    image = QImage("./img/2.png");
    pixmap=  QPixmap()
    pixmap.convertFromImage(image)
    label= QLabel()

    label.setPixmap(pixmap)
    
    main.show()
#####################################################   
    sys.exit(app.exec_())

 

posted @ 2015-11-19 16:46  宁静的天空  阅读(1184)  评论(0编辑  收藏  举报