|
| 1 | +import sys |
| 2 | +from PySide.QtCore import * |
| 3 | +from PySide.QtGui import * |
| 4 | + |
| 5 | +import urllib2 |
| 6 | + |
| 7 | + |
| 8 | +class Form(QDialog): |
| 9 | + |
| 10 | + def __init__(self, parent=None): |
| 11 | + super(Form, self).__init__(parent) |
| 12 | + |
| 13 | + date = self.getdata() |
| 14 | + rates = sorted(self.rates.keys()) |
| 15 | + |
| 16 | + dateLabel = QLabel(date) |
| 17 | + self.fromComboBox = QComboBox() |
| 18 | + self.fromComboBox.addItems(rates) |
| 19 | + self.fromSpinBox = QDoubleSpinBox() |
| 20 | + self.fromSpinBox.setRange(0.01, 10000000.00) |
| 21 | + self.fromSpinBox.setValue(1.00) |
| 22 | + self.toComboBox = QComboBox() |
| 23 | + self.toComboBox.addItems(rates) |
| 24 | + self.toLabel = QLabel("1.00") |
| 25 | + |
| 26 | + grid = QGridLayout() |
| 27 | + grid.addWidget(dateLabel, 0, 0) |
| 28 | + grid.addWidget(self.fromComboBox, 1, 0) |
| 29 | + grid.addWidget(self.fromSpinBox, 1, 1) |
| 30 | + grid.addWidget(self.toComboBox, 2, 0) |
| 31 | + grid.addWidget(self.toLabel, 2, 1) |
| 32 | + self.setLayout(grid) |
| 33 | + |
| 34 | + |
| 35 | + self.connect(self.fromComboBox, SIGNAL("currentIndexChanged(int)"), self.updateUi) |
| 36 | + self.connect(self.toComboBox, SIGNAL("currentIndexChanged(int)"), self.updateUi) |
| 37 | + self.connect(self.fromSpinBox, SIGNAL("valueChanged(double)"), self.updateUi) |
| 38 | + |
| 39 | + |
| 40 | + def updateUi(self): |
| 41 | + to = self.toComboBox.currentText() |
| 42 | + from_ = self.fromComboBox.currentText() |
| 43 | + |
| 44 | + amount = (self.rates[from_] / self.rates[to]) * self.fromSpinBox.value() |
| 45 | + self.toLabel.setText("%0.2f" % amount) |
| 46 | + |
| 47 | + def getdata(self): |
| 48 | + self.rates = {} |
| 49 | + |
| 50 | + try: |
| 51 | + date = "Unknown" |
| 52 | + |
| 53 | + fh = urllib2.urlopen("http://www.bankofcanada.ca/en/markets/csv/exchange_eng.csv") |
| 54 | + |
| 55 | + for line in fh: |
| 56 | + line = line.rstrip() |
| 57 | + if not line or line.startswith(("#", "Closing")): |
| 58 | + continue |
| 59 | + |
| 60 | + fields = line.split(",") |
| 61 | + if line.startswith("Date "): |
| 62 | + date = fields[-1] |
| 63 | + |
| 64 | + else: |
| 65 | + try: |
| 66 | + value = float(fields[-1]) |
| 67 | + self.rates[fields[0]] = value |
| 68 | + except ValueError: |
| 69 | + pass |
| 70 | + |
| 71 | + return "Exchange rates date: " + date |
| 72 | + except Exception, e: |
| 73 | + return "Failued to download:\n%s" % e |
| 74 | + |
| 75 | + |
| 76 | +app = QApplication(sys.argv) |
| 77 | +form = Form() |
| 78 | +form.show() |
| 79 | +app.exec_() |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
0 commit comments