forked from pbrotoisworo/currency_converter_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
90 lines (62 loc) · 2.57 KB
/
main.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
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import ui.mainDialog as mainDialog
import ui.configDialog as configDialog
import sys
import requests
import pickle
import os
global CONFIG_FILE
CONFIG_FILE = 'config.pkl'
class ConfigParameters(QDialog, configDialog.Ui_Dialog):
def __init__(self, parent=None):
super(ConfigParameters, self).__init__(parent)
self.setupUi(self)
with open(CONFIG_FILE, 'rb') as f:
self.config = pickle.load(f)
self.comboBoxDefaultReferenceCurrency.setCurrentText(self.config['ref_currency'])
self.pushButtonSave.clicked.connect(self.update_config)
self.pushButtonClose.clicked.connect(self.close_window)
def update_config(self):
self.config['ref_currency'] = self.comboBoxDefaultReferenceCurrency.currentText()
with open(CONFIG_FILE, 'wb') as f:
pickle.dump(self.config, f)
def close_window(self):
self.close()
class MainDialog(QDialog, mainDialog.Ui_Dialog):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
self.setupUi(self)
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, 'rb') as f:
self.config = pickle.load(f)
else:
with open(CONFIG_FILE, 'wb') as f:
self.config = {'ref_currency': 'USD'}
pickle.dump(self.config, f)
pixmap = QPixmap('ui/logo.JPG')
self.labelLogo.setPixmap(pixmap)
self.labelLogo.setScaledContents(True)
self.comboBoxReferenceCurrency.setCurrentText(self.config['ref_currency'])
self.pushButtonLoadRates.clicked.connect(self.load_rates)
self.pushButtonConfigure.clicked.connect(self.open_config_window)
def open_config_window(self):
self.configParameters = ConfigParameters(self)
self.configParameters.exec_()
with open(CONFIG_FILE, 'rb') as f:
self.config = pickle.load(f)
self.comboBoxReferenceCurrency.setCurrentText(self.config['ref_currency'])
def load_rates(self):
reference = self.comboBoxReferenceCurrency.currentText()
url = 'https://api.exchangerate-api.com/v4/latest/' + reference
response = requests.get(url)
data = response.json()
self.lineEditIDR.setText(str(data['rates']['IDR']))
self.lineEditPHP.setText(str(data['rates']['PHP']))
self.lineEditUSD.setText(str(data['rates']['USD']))
if __name__ == "__main__":
app = QApplication(sys.argv)
form = MainDialog()
form.show()
app.exec_()