实现QQ抽屉效果

代码:

  1 #coding: utf-8
  2 from PyQt4.QtCore import *
  3 from PyQt4.QtGui import *
  4 import sys
  5 
  6 QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))
  7 
  8 class MyQQ(QToolBox):
  9     def __init__(self, parent=None):
 10         super(MyQQ, self).__init__(parent)
 11         #创建了一个 QToolButton 类实例, 在这里 QToolButton 分别对应于抽屉中的每一个按钮。
 12         toolButton1_1 = QToolButton()
 13         toolButton1_1.setText(self.tr("朽木"))
 14         toolButton1_1.setIcon(QIcon("image/9.gif"))
 15         #toolButton1_1.setWindowIcon(QSize(60, 60))
 16         #设置按钮的 AutoRaise 属性为 True,当鼠标离开时,按钮自动恢复成弹起状态
 17         toolButton1_1.setAutoRaise(True)
 18         toolButton1_1.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
 19         #设置按钮的 ToolButtonStyle 属性,ToolButtonStyle 属性主要用来描述按钮的文字和图标的显示方式
 20         toolButton1_2 = QToolButton()
 21         toolButton1_2.setText(self.tr("Cindy"))
 22         toolButton1_2.setIcon(QIcon("image/8.gif"))
 23         #toolButton1_2.setWindowIcon(QSize(60, 60))
 24         toolButton1_2.setAutoRaise(True)
 25         toolButton1_2.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
 26 
 27         toolButton1_3 = QToolButton()
 28         toolButton1_3.setText(self.tr("了了"))
 29         toolButton1_3.setIcon(QIcon("image/1.gif"))
 30         #toolButton1_3.setWindowIcon(QSize(60, 60))
 31         toolButton1_3.setAutoRaise(True)
 32         toolButton1_3.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
 33 
 34         toolButton1_4 = QToolButton()
 35         toolButton1_4.setText(self.tr("张三虎"))
 36         toolButton1_4.setIcon(QIcon("image/3.gif"))
 37         #toolButton1_4.setWindowIcon(QSize(60, 60))
 38         toolButton1_4.setAutoRaise(True)
 39         toolButton1_4.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
 40 
 41         toolButton1_5 = QToolButton()
 42         toolButton1_5.setText(self.tr("CSDN"))
 43         toolButton1_5.setIcon(QIcon("image/4.gif"))
 44         #toolButton1_5.setWindowIcon(QSize(60, 60))
 45         toolButton1_5.setAutoRaise(True)
 46         toolButton1_5.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
 47 
 48         toolButton2_1 = QToolButton()
 49         toolButton2_1.setText(self.tr("丫头"))
 50         toolButton2_1.setIcon(QIcon("image/5.gif"))
 51         #toolButton2_1.setWindowIcon(QSize(60, 60))
 52         toolButton2_1.setAutoRaise(True)
 53         toolButton2_1.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
 54 
 55         toolButton2_2 = QToolButton()
 56         toolButton2_2.setText(self.tr("梅子"))
 57         toolButton2_2.setIcon(QIcon("image/6.gif"))
 58         #toolButton2_2.setWindowIcon(QSize(60, 60))
 59         toolButton2_2.setAutoRaise(True)
 60         toolButton2_2.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
 61 
 62         toolButton3_1 = QToolButton()
 63         toolButton3_1.setText(self.tr("在耀"))
 64         toolButton3_1.setIcon(QIcon("image/7.gif"))
 65         #toolButton3_1.setWindowIcon(QSize(60, 60))
 66         toolButton3_1.setAutoRaise(True)
 67         toolButton3_1.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
 68 
 69         toolButton3_2 = QToolButton()
 70         toolButton3_2.setText(self.tr("和梁"))
 71         toolButton3_2.setIcon(QIcon("image/6.gif"))
 72         #toolButton3_2.setWindowIcon(QSize(60, 60))
 73         toolButton3_2.setAutoRaise(True)
 74         toolButton3_2.setToolButtonStyle(Qt.ToolButtonTextUnderIcon)
 75         #创建了一个 QGroupBox 类实例,在本例中对应每一个抽屉
 76         groupbox1 = QGroupBox()
 77         #创建一个 QVBoxLayout 类实例,用来设置抽屉内各按钮的布局
 78         vlayout1 = QVBoxLayout(groupbox1)
 79         vlayout1.setMargin(10)
 80         vlayout1.setAlignment(Qt.AlignCenter)
 81         vlayout1.addWidget(toolButton1_1)
 82         vlayout1.addWidget(toolButton1_2)
 83         vlayout1.addWidget(toolButton1_3)
 84         vlayout1.addWidget(toolButton1_4)
 85         vlayout1.addWidget(toolButton1_5)
 86         vlayout1.addStretch()
 87         #调用 addStretch()方法在按钮之后插入一个占位符,使得所有按钮能靠上对齐。并且在整个抽屉大小发生改变时,保证按钮的大小不发生变化
 88         groupbox2 = QGroupBox()
 89         vlayout2 = QVBoxLayout(groupbox2)
 90         vlayout2.setMargin(10)
 91         vlayout2.setAlignment(Qt.AlignCenter)
 92         vlayout2.addWidget(toolButton2_1)
 93         vlayout2.addWidget(toolButton2_2)
 94 
 95         groupbox3 = QGroupBox()
 96         vlayout3 = QVBoxLayout(groupbox3)
 97         vlayout3.setMargin(10)
 98         vlayout3.setAlignment(Qt.AlignCenter)
 99         vlayout3.addWidget(toolButton3_1)
100         vlayout3.addWidget(toolButton3_2)
101         #把准备好的抽屉插入至 QToolBox中
102         self.addItem(groupbox1, self.tr("我的好友"))
103         self.addItem(groupbox2, self.tr("丫头"))
104         self.addItem(groupbox3, self.tr("朋友"))
105 
106 app = QApplication(sys.argv)
107 form = MyQQ()
108 form.setWindowTitle("MyQQ")
109 form.show()
110 app.exec_()

 

posted @ 2015-05-15 10:28  Chris*Chen  阅读(627)  评论(0编辑  收藏  举报