zoukankan      html  css  js  c++  java
  • 生成具有三态背景图片的按钮

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

    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_())
    
  • 相关阅读:
    OpenLayers 添加OpenStreetMap(OSM)瓦片层示例
    [转]Net Framework引路蜂地图开发示例
    SVG关注复杂图形的网页绘制技术
    解决Easyui1.3.3 IE8兼容性问题
    Ajax学习教程在线阅读
    判断访问站点的浏览器类型
    修改TOMCAT服务器图标为应用LOGO
    ArcGIS学习推荐基础教程摘录
    Jquery html页面处理基础
    android AppWidget的使用以及利用TimerTask实现widget的定时更新
  • 原文地址:https://www.cnblogs.com/ribavnu/p/4978127.html
Copyright © 2011-2022 走看看