码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-11-19 18:53:32      阅读:450      评论:0      收藏:0      [点我收藏+]

标签:

 

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

 

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_())

 

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

标签:

原文地址:http://www.cnblogs.com/ribavnu/p/4978127.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!