Advertisement
Guest User

Untitled

a guest
Aug 30th, 2014
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. import sys
  2. import urllib.request
  3.  
  4. from PySide.QtCore import Slot
  5. from PySide.QtGui import *
  6.  
  7.  
  8. app = QApplication(sys.argv)
  9. win = QWidget()
  10. win.setWindowTitle('Test Window')
  11.  
  12.  
  13. @Slot()
  14. def update_ui():
  15.     to = toComboBox.currentText()
  16.     from_ = fromComboBox.currentText()
  17.  
  18.     amount = (rates[from_] / rates[to]) * fromSpinBox.value()
  19.     toLabel.setText("%0.2f" % amount)
  20.  
  21.  
  22. def get_date():
  23.     try:
  24.         d = "Unknown"
  25.         fh = urllib.request.urlopen("http://www.bankofcanada.ca/en/markets/csv/exchange_eng.csv").read()
  26.  
  27.         for line in fh:
  28.             line = line.strip()
  29.  
  30.             if not line or line.startsWith(("#", "Closing")):
  31.                 continue
  32.  
  33.             fields = line.split(",")
  34.             if line.startsWith("Date"):
  35.                 d = fields[-1]
  36.             else:
  37.                 try:
  38.                     value = float(fields[-1])
  39.                     rates[fields[0]] = value
  40.                 except ValueError:
  41.                     pass
  42.         return "Exchange rates date: " + d
  43.     except Exception as e:
  44.         return "Failed to download:\n%s" % e
  45.  
  46.  
  47. rates = {}
  48. date = get_date()
  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. win.show()
  70. app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement