Advertisement
jcfla

Untitled

Aug 31st, 2014
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 KB | None | 0 0
  1. import codecs
  2. import csv
  3. import sys
  4. import urllib.request
  5.  
  6. from PySide.QtCore import Slot
  7. from PySide.QtGui import *
  8.  
  9.  
  10. app = QApplication(sys.argv)
  11. win = QWidget()
  12. win.setWindowTitle('Test Window')
  13.  
  14.  
  15. @Slot()
  16. def update_ui():
  17.     to = toComboBox.currentText()
  18.     from_ = fromComboBox.currentText()
  19.  
  20.     amount = (rates[from_] / rates[to]) * fromSpinBox.value()
  21.     toLabel.setText("%0.2f" % amount)
  22.  
  23.  
  24. def get_rates():
  25.     d = "Unknown"
  26.     url_string = "http://www.bankofcanada.ca/en/markets/csv/exchange_eng.csv"
  27.     raw_file = urllib.request.urlopen(url_string)
  28.     decoded_file = codecs.getreader("utf-8")(raw_file)
  29.     rows = csv.reader(decoded_file)
  30.  
  31.     for row in rows:
  32.         if not row or row[0].startswith("#"):
  33.             continue
  34.  
  35.         if row[0].startswith("Date "):
  36.             d = row[-1]
  37.         else:
  38.             try:
  39.                 value = float(row[-1])
  40.                 rates[row[0]] = value
  41.             except ValueError:
  42.                 pass
  43.     return "Exchange rates date: " + d
  44.  
  45.  
  46. rates = {}
  47. date = get_rates()
  48.  
  49. dateLabel = QLabel(date)
  50. fromComboBox = QComboBox()
  51. fromComboBox.addItems(sorted(rates.keys()))
  52. fromSpinBox = QDoubleSpinBox()
  53. fromSpinBox.setRange(0.01, 100000000.00)
  54. fromSpinBox.setValue(1.00)
  55. toComboBox = QComboBox()
  56. toComboBox.addItems(sorted(rates.keys()))
  57. toLabel = QLabel("1.00")
  58.  
  59. grid = QGridLayout()
  60. grid.addWidget(dateLabel, 0, 0)
  61. grid.addWidget(fromComboBox, 1, 0)
  62. grid.addWidget(fromSpinBox, 1, 1)
  63. grid.addWidget(toComboBox, 2, 0)
  64. grid.addWidget(toLabel, 2, 1)
  65. win.setLayout(grid)
  66.  
  67. fromSpinBox.valueChanged.connect(update_ui)
  68.  
  69. win.show()
  70. app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement