-
Notifications
You must be signed in to change notification settings - Fork 2
/
CryptoConverter.py
87 lines (76 loc) · 2.87 KB
/
CryptoConverter.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
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from MainWindow import Ui_MainWindow
import json
import requests
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
self.show()
# Vars
self.new_label = '0'
self.cur1 = 'BTC'
self.cur2 = 'USD'
self.result = ''
# Connect buttons
for n in range(0, 10):
getattr(self, 'pushButton_n%s' % n).clicked.connect(self.digit_pressed)
self.pushButton_n10.clicked.connect(self.decimal_point)
self.pushButton_del.clicked.connect(self.del_digit)
self.pushButton_convert.clicked.connect(self.convert_fun)
self.comboBox.activated[str].connect(self.currencies1)
self.comboBox_2.activated[str].connect(self.currencies2)
def digit_pressed(self):
button = self.sender()
self.new_label = self.label_1.text() + button.text()
if '.' in self.new_label:
self.label_1.setText(str(self.new_label))
else:
self.label_1.setText(str(int(self.new_label)))
def decimal_point(self):
if '.' in self.label_1.text():
pass
else:
self.label_1.setText(self.label_1.text() + '.')
def del_digit(self):
self.new_label = self.new_label[:-1]
self.label_1.setText(self.new_label)
def currencies1(self, item1):
self.cur1 = item1
# print(self.cur1)
def currencies2(self, item2):
self.cur2 = item2
# print(self.cur2)
# Live data from API
def api(self, cur1, cur2):
api_link = "https://min-api.cryptocompare.com/data/pricemulti?fsyms={}&tsyms={}".format(cur1, cur2)
resp = requests.get(api_link)
# print(r.status_code)
data = json.loads(resp.content)
# print(data)
var = data[self.cur1][self.cur2]
return var
def convert_fun(self):
try:
if len(self.new_label) == 0:
self.label_1.setText('0')
self.label_2.setText('0')
if '.' in self.new_label:
self.result = float(self.new_label) * self.api(self.cur1, self.cur2)
self.result = round(self.result, 2)
self.label_2.setText(str(self.result))
else:
self.result = int(self.new_label) * self.api(self.cur1, self.cur2)
self.result = round(self.result, 2)
self.label_2.setText(str(self.result))
except (KeyError, ValueError):
pass
except requests.exceptions.ConnectionError:
print('Please verify your internet connection!')
if __name__ == '__main__':
app = QApplication([])
app.setApplicationName("CryptoConverter")
window = MainWindow()
app.exec_()