-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
159 lines (142 loc) · 5.72 KB
/
calculator.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#encoding:utf-8
import sys
from PyQt5.QtCore import QRect, QRegExp, Qt
from PyQt5.QtGui import QColor, QRegExpValidator
from PyQt5.QtWidgets import (QApplication, QLabel, QLineEdit, QListWidget,
QTextBrowser, QWidget)
class Calculator(QWidget):
def __init__(self):
super(Calculator, self).__init__()
self.setWindowTitle("Calculator")
self.resize(400, 286)
self.setFixedSize(400, 286)
self.name_line = QLineEdit(self)
self.name_line.setGeometry(QRect(240, 10, 151, 21))
self.card_list = QListWidget(self)
self.card_list.setGeometry(QRect(10, 10, 151, 261))
self.atk_line = QTextBrowser(self)
self.atk_line.setGeometry(QRect(240, 40, 151, 21))
self.def_line = QTextBrowser(self)
self.def_line.setGeometry(QRect(240, 70, 151, 21))
self.num_a_line = QLineEdit(self)
self.num_a_line.setGeometry(QRect(240, 100, 151, 21))
self.num_b_line = QLineEdit(self)
self.num_b_line.setGeometry(QRect(240, 130, 151, 21))
self.add_line = QTextBrowser(self)
self.add_line.setGeometry(QRect(240, 160, 151, 21))
self.sub_line = QTextBrowser(self)
self.sub_line.setGeometry(QRect(240, 190, 151, 21))
self.mul_line = QTextBrowser(self)
self.mul_line.setGeometry(QRect(240, 220, 151, 21))
self.div_line = QTextBrowser(self)
self.div_line.setGeometry(QRect(240, 250, 151, 21))
self.label = QLabel(self)
self.label.setGeometry(QRect(170, 10, 41, 16))
self.label_2 = QLabel(self)
self.label_2.setGeometry(QRect(170, 40, 61, 20))
self.label_3 = QLabel(self)
self.label_3.setGeometry(QRect(170, 70, 61, 20))
self.label_4 = QLabel(self)
self.label_4.setGeometry(QRect(170, 100, 61, 20))
self.label_5 = QLabel(self)
self.label_5.setGeometry(QRect(170, 130, 61, 20))
self.label_6 = QLabel(self)
self.label_6.setGeometry(QRect(170, 160, 61, 20))
self.label_7 = QLabel(self)
self.label_7.setGeometry(QRect(170, 190, 61, 20))
self.label_8 = QLabel(self)
self.label_8.setGeometry(QRect(170, 220, 61, 20))
self.label_9 = QLabel(self)
self.label_9.setGeometry(QRect(170, 250, 61, 20))
self.label.setText("卡名")
self.label_2.setText("攻击力")
self.label_3.setText("防御力")
self.label_4.setText("a")
self.label_5.setText("b")
self.label_6.setText("a+b")
self.label_7.setText("a-b")
self.label_8.setText("a*b")
self.label_9.setText("a/b")
# set regex for numeric
regx = QRegExp("^[0-9]{15}$")
for num_line in [self.num_a_line, self.num_b_line]:
validator = QRegExpValidator(regx, num_line)
num_line.setValidator(validator)
self.name_line.setPlaceholderText("输入卡名")
self.num_a_line.setPlaceholderText("输入非负整数")
self.num_b_line.setPlaceholderText("输入非负整数")
# hide scroll bar
for bar in [self.atk_line, self.def_line, self.add_line, self.sub_line, self.mul_line, self.div_line]:
bar.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
bar.setTextColor(QColor('green'))
# connect
self.name_line.textChanged.connect(self.search_card)
self.card_list.itemSelectionChanged.connect(self.show_ad)
self.num_a_line.textChanged.connect(self.cal)
self.num_b_line.textChanged.connect(self.cal)
def setdatas(self, data):
self.card_data = data
a = {}
if len(self.card_data) == 0:
self.name_line.setEnabled(False)
self.card_list.clear()
self.card_list.addItem("无数据库")
self.name_line.setPlaceholderText("无数据库")
self.card_list.setEnabled(False)
else:
self.card_list.clear()
def cal(self):
# get number
num_a_str = self.num_a_line.text()
num_b_str = self.num_b_line.text()
try:
num_a = int(num_a_str)
num_b = int(num_b_str)
except:
return
# calculate
add_result = num_a + num_b
sub_result = num_a - num_b
mul_result = num_a * num_b
if num_b != 0:
div_result = num_a // num_b
else:
div_result = 0
# show
self.add_line.setText(str(add_result))
self.sub_line.setText(str(sub_result))
self.mul_line.setText(str(mul_result))
self.div_line.setText(str(div_result))
def search_card(self):
# 判断是否有名字
text = self.name_line.text()
if not self.name_line.isEnabled():
return
if len(text) == 0:
self.name_line.clear()
return
self.card_list.clear()
# 名字相同的优先
if text in self.card_data.keys():
self.card_list.addItem(text)
# 遍历搜索符合条件的卡片
for cardname in self.card_data.keys():
if text in cardname and text != cardname:
self.card_list.addItem(cardname)
def show_ad(self):
idx = self.card_list.selectedIndexes()
if len(idx) < 1:
return
cardname = self.card_list.item(idx[0].row()).text()
if cardname in self.card_data:
args = self.card_data[cardname]
self.atk_line.setText(str(args[0]))
self.def_line.setText(str(args[1]))
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
self.close()
if __name__ == "__main__":
app = QApplication(sys.argv)
m_window = Calculator()
m_window.show()
sys.exit(app.exec_())