前言:

说完了拥有按钮共性的抽象类QAbstractButton之后,接下来就是具体了看下它的子类:

首先是QPushButton

QPushButton描述:

PyQt5 控件学习(一个一个学习之QPushButton)

 

QPushButton功能作用:

 

一:QPushButton 功能作用之创建按钮:

PyQt5 控件学习(一个一个学习之QPushButton)

 1 from PyQt5.Qt import * #刚开始学习可以这样一下导入
 2 import sys
 3 
 4 
 5 class Btn(QPushButton):
 6     # def hitButton(self, QPoint):
 7     def hitButton(self, point):
 8         print(point)  #用户点击按钮之后,这个函数会将用户点击的点传出来
 9         cir_x = self.width()/2
10         cir_y = self.height()/2
11 
12         hit_x = point.x()
13         hit_y = point.y()
14 
15         if pow(hit_x-cir_x,2)+pow(hit_y-cir_y,2) >pow(self.width()/2,2):
16             return False
17         return True
18 ############################画内切圆###############################
19     def paintEvent(self, event):
20         ###########################################################
21         super().paintEvent(event)
22         ###########################################################
23 
24         painter = QPainter(self) # 它里面的参数是 QPaintDevice “纸”,所有控件都可以当做纸,因为QWidget也继承了QPaintDevice
25         #这时就有了画家和 纸
26 
27         #给画家支笔
28         pen = QPen(QColor(100,10,155),4)  #笔的颜色和宽度
29         painter.setPen(pen)
30 
31 
32         #画家开始画
33         painter.drawEllipse(self.rect()) #这就是两个点,四个值
34 ############################画内切圆###############################
35 
36 
37 #1,创建app
38 app  = QApplication(sys.argv)
39 
40 
41 #2,控件的操作:
42 #创建控件
43 window = QWidget()
44 
45 
46 #设置控件
47 window.setWindowTitle("QAbstactButton 有效区域")
48 window.resize(500,500)
49 
50 ############################QPushButton的构造函数###############################
51 #两种方式:
52 # 第一种
53     # btn = QPushButton()
54     # btn.setParent(window)
55     # btn.setText("xxx")
56     # btn.setIcon(QIcon("icon.ico"))
57 #第二种:
58 # btn = QPushButton(QIcon("icon.ico"),"xxx",window)  #一步搞定
59 
60 #我喜欢的方式:
61 btn = QPushButton(window)
62 btn.setText("xxx")
63 btn.setIcon(QIcon("icon.ico"))
64 
65 
66 ############################QPushButton的构造函数###############################
67 
68 
69 
70 #展示控件
71 window.show()
72 
73 #3,进入消息循环
74 sys.exit(app.exec_())
QPushButton的构造函数

相关文章: