forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ButtonHover.py
113 lines (99 loc) · 3.11 KB
/
ButtonHover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2018年1月29日
@author: Irony."[讽刺]
@site: http://alyl.vip, http://orzorz.vip, https://coding.net/u/892768447, https://github.com/892768447
@email: [email protected]
@file: ButtonHover
@description:
'''
import sys
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QPushButton, QApplication
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: [email protected]"
__Copyright__ = "Copyright (c) 2018 Irony.\"[讽刺]"
__Version__ = "Version 1.0"
StyleSheet = '''
/*这里是通用设置,所有按钮都有效,不过后面的可以覆盖这个*/
QPushButton {
border: none; /*去掉边框*/
}
/*
QPushButton#xxx
或者
#xx
都表示通过设置的objectName来指定
*/
QPushButton#RedButton {
background-color: #f44336; /*背景颜色*/
}
#RedButton:hover {
background-color: #e57373; /*鼠标悬停时背景颜色*/
}
/*注意pressed一定要放在hover的后面,否则没有效果*/
#RedButton:pressed {
background-color: #ffcdd2; /*鼠标按下不放时背景颜色*/
}
#GreenButton {
background-color: #4caf50;
border-radius: 5px; /*圆角*/
}
#GreenButton:hover {
background-color: #81c784;
}
#GreenButton:pressed {
background-color: #c8e6c9;
}
#BlueButton {
background-color: #2196f3;
/*限制最小最大尺寸*/
min-width: 96px;
max-width: 96px;
min-height: 96px;
max-height: 96px;
border-radius: 48px; /*圆形*/
}
#BlueButton:hover {
background-color: #64b5f6;
}
#BlueButton:pressed {
background-color: #bbdefb;
}
#OrangeButton {
max-height: 48px;
border-top-right-radius: 20px; /*右上角圆角*/
border-bottom-left-radius: 20px; /*左下角圆角*/
background-color: #ff9800;
}
#OrangeButton:hover {
background-color: #ffb74d;
}
#OrangeButton:pressed {
background-color: #ffe0b2;
}
/*根据文字内容来区分按钮,同理还可以根据其它属性来区分*/
QPushButton[text="purple button"] {
color: white; /*文字颜色*/
background-color: #9c27b0;
}
'''
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
layout = QHBoxLayout(self)
layout.addWidget(QPushButton("red button", self,
objectName="RedButton", minimumHeight=48))
layout.addWidget(QPushButton("green button", self,
objectName="GreenButton", minimumHeight=48))
layout.addWidget(QPushButton("blue button", self,
objectName="BlueButton", minimumHeight=48))
layout.addWidget(QPushButton("orange button", self,
objectName="OrangeButton", minimumHeight=48))
layout.addWidget(QPushButton("purple button", self,
objectName="PurpleButton", minimumHeight=48))
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setStyleSheet(StyleSheet)
w = Window()
w.show()
sys.exit(app.exec_())