Advertisement
Guest User

Untitled

a guest
Aug 30th, 2014
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 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[int(from_)] / rates[int(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.                 print(rates[row[0]] + " / VALUE : " + str(value))
  41.                 rates[row[0]] = value
  42.             except ValueError:
  43.                 pass
  44.     return "Exchange rates date: " + d
  45.  
  46.  
  47. rates = {}
  48. date = get_rates()
  49. rates = sorted(rates.keys())
  50.  
  51. dateLabel = QLabel(date)
  52. fromComboBox = QComboBox()
  53. fromComboBox.addItems(rates)
  54. fromSpinBox = QDoubleSpinBox()
  55. fromSpinBox.setRange(0.01, 100000000.00)
  56. fromSpinBox.setValue(1.00)
  57. toComboBox = QComboBox()
  58. toComboBox.addItems(rates)
  59. toLabel = QLabel("1.00")
  60.  
  61. grid = QGridLayout()
  62. grid.addWidget(dateLabel, 0, 0)
  63. grid.addWidget(fromComboBox, 1, 0)
  64. grid.addWidget(fromSpinBox, 1, 1)
  65. grid.addWidget(toComboBox, 2, 0)
  66. grid.addWidget(toLabel, 2, 1)
  67. win.setLayout(grid)
  68.  
  69. fromSpinBox.valueChanged.connect(update_ui)
  70.  
  71. win.show()
  72. app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement