Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PySide.QtGui import *
- import urllib.request
- import sys
- class Form(QDialog):
- def __init__(self, parent=None):
- super(Form, self).__init__(parent)
- self.rates = {}
- date = self.get_date()
- rates = sorted(self.rates.keys())
- self.dateLabel = QLabel(date)
- self.fromComboBox = QComboBox()
- self.fromComboBox.addItems(rates)
- self.fromSpinBox = QDoubleSpinBox()
- self.fromSpinBox.setRange(0.01, 100000000.00)
- self.fromSpinBox.setValue(1.00)
- self.toComboBox = QComboBox()
- self.toComboBox.addItems(rates)
- self.toLabel = QLabel("1.00")
- grid = QGridLayout()
- grid.addWidget(self.dateLabel, 0, 0)
- grid.addWidget(self.fromComboBox, 1, 0)
- grid.addWidget(self.fromSpinBox, 1, 1)
- grid.addWidget(self.toComboBox, 2, 0)
- grid.addWidget(self.toLabel, 2, 1)
- self.setLayout(grid)
- self.fromComboBox.currentIndexChanged().connect(self.update_ui)
- self.toComboBox.currentIndexChanged().connect(self.update_ui)
- self.fromSpinBox.valueChanged().connect(self.update_ui)
- def update_ui(self):
- to = self.toComboBox.currentText()
- from_ = self.fromComboBox.currentText()
- amount = (self.rates[from_] / self.rates[to]) * self.fromSpinBox.value()
- self.toLabel.setText("%0.2f" % amount)
- def get_date(self):
- try:
- date = "Unknown"
- fh = urllib.request.urlopen("http://www.bankofcanada.ca/en/markets/csv/exchange_eng.csv")
- for line in fh:
- line = line.rstrip()
- if not line or line.startsWith(("#", "Closing")):
- continue
- fields = line.split(",")
- if line.startsWith("Date"):
- date = fields[-1]
- else:
- try:
- value = float(fields[-1])
- self.rates[fields[0]] = value
- except ValueError:
- pass
- return "Exchange rates date: " + date
- except Exception as e:
- return "Failed to download:\n%s" % e
- app = QApplication(sys.argv)
- form = Form()
- form.show()
- app.exec_()
- =========================
- Traceback (most recent call last):
- File "C:xxx/main.py", line 70, in <module>
- form = Form()
- File "C:xxx/main.py", line 32, in __init__
- self.fromComboBox.currentIndexChanged().connect(self.update_ui)
- TypeError: native Qt signal is not callable
- Process finished with exit code 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement