Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import urllib.request
- from PySide.QtCore import Slot
- from PySide.QtGui import *
- app = QApplication(sys.argv)
- win = QWidget()
- win.setWindowTitle('Test Window')
- @Slot()
- def update_ui():
- to = toComboBox.currentText()
- from_ = fromComboBox.currentText()
- amount = (rates[from_] / rates[to]) * fromSpinBox.value()
- toLabel.setText("%0.2f" % amount)
- def get_date():
- try:
- d = "Unknown"
- fh = urllib.request.urlopen("http://www.bankofcanada.ca/en/markets/csv/exchange_eng.csv").read()
- for line in fh:
- line = line.strip()
- if not line or line.startsWith(("#", "Closing")):
- continue
- fields = line.split(",")
- if line.startsWith("Date"):
- d = fields[-1]
- else:
- try:
- value = float(fields[-1])
- rates[fields[0]] = value
- except ValueError:
- pass
- return "Exchange rates date: " + d
- except Exception as e:
- return "Failed to download:\n%s" % e
- rates = {}
- date = get_date()
- rates = sorted(rates.keys())
- dateLabel = QLabel(date)
- fromComboBox = QComboBox()
- fromComboBox.addItems(rates)
- fromSpinBox = QDoubleSpinBox()
- fromSpinBox.setRange(0.01, 100000000.00)
- fromSpinBox.setValue(1.00)
- toComboBox = QComboBox()
- toComboBox.addItems(rates)
- toLabel = QLabel("1.00")
- grid = QGridLayout()
- grid.addWidget(dateLabel, 0, 0)
- grid.addWidget(fromComboBox, 1, 0)
- grid.addWidget(fromSpinBox, 1, 1)
- grid.addWidget(toComboBox, 2, 0)
- grid.addWidget(toLabel, 2, 1)
- win.setLayout(grid)
- win.show()
- app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement