Advertisement
bluegator4

Bo's Currency Converter - Python 3.3.4

May 14th, 2014
573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.96 KB | None | 0 0
  1. # Python 3 version of Bo Milanovich's Currency Converter Python/Qt tutorial (https://www.youtube.com/watch?v=8D_aEYiBU2c)
  2. import sys
  3. import urllib.request
  4. import csv
  5. import codecs
  6. from PySide.QtCore import *
  7. from PySide.QtGui import *
  8.  
  9.  
  10. class Form(QDialog):
  11.     def __init__(self, parent=None):
  12.         super(Form,self).__init__(parent)
  13.  
  14.         # I had to move this from the 'getdate' function to the __init__. Otherwise got error:
  15.         #  AttributeError: 'Form' object has no attribute 'rates'
  16.         self.rates = {}
  17.  
  18.         # Has to be 'getdate()', not 'getdate' otherwise it returns the object reference, like this:
  19.         # <bound method Form.getdate of <__main__.Form object at 0x0000000004026BC8>>.
  20.         #
  21.         # This results in the following error when trying to initialize the QLabel widget below:
  22.         #   TypeError: 'PySide.QtGui.QLabel' called with wrong argument types:
  23.         #      PySide.QtGui.QLabel(method)
  24.         date = self.getdate
  25.  
  26.         rates = sorted(self.rates.keys())
  27.         dateLabel = QLabel(date)
  28.         self.fromComboBox = QComboBox()
  29.         self.fromComboBox.addItems(rates)
  30.         self.fromSpinBox = QDoubleSpinBox() # Takes Double data type, not just integers.
  31.         self.fromSpinBox.setRange(0.01,10000000.00) # Ranges allowed for spinbox
  32.         self.fromSpinBox.setValue(1.00) # Default value
  33.         self.toComboBox = QComboBox()
  34.         self.toComboBox.addItems(rates)
  35.         self.toLabel = QLabel("1.00") # Default value
  36.  
  37.         grid = QGridLayout() # Instead of vertical layout QVBoxLayout()
  38.  
  39.         # Works like the Tkinter grid geometry manager!
  40.         grid.addWidget(dateLabel, 0, 0) # row,column
  41.         grid.addWidget(self.fromComboBox, 1, 0)
  42.         grid.addWidget(self.fromSpinBox, 1, 1)
  43.         grid.addWidget(self.toComboBox, 2, 0)
  44.         grid.addWidget(self.toLabel, 2, 1)
  45.         self.setLayout(grid)
  46.  
  47.         # The signal currentIndexChanged is emitted when the selected item in a combo box changes.
  48.         self.connect(self.fromComboBox, SIGNAL("currentIndexChanged(int)"), self.updateUi)
  49.         self.connect(self.toComboBox, SIGNAL("currentIndexChanged(int)"), self.updateUi)
  50.  
  51.         # The signal valueChanged is emitted when the value in the spinbox changes.
  52.         self.connect(self.fromSpinBox, SIGNAL("valueChanged(double)"), self.updateUi)
  53.  
  54.     def updateUi(self):
  55.         to = self.toComboBox.currentText()
  56.         from_ = self.fromComboBox.currentText() # Underscore is used because 'from' is a reserved word in Python.
  57.  
  58.         amount = (self.rates[from_] / self.rates[to]) * self.fromSpinBox.value()
  59.         self.toLabel.setText("%0.2f" % amount)
  60.  
  61.     def getdate(self):
  62.         try:
  63.             date = "Unknown"
  64.             url = "http://www.bankofcanada.ca/en/markets/csv/exchange_eng.csv"
  65.             httpstream = urllib.request.urlopen(url)
  66.  
  67.             # Because urllib returns bytes, they have to be decoded.
  68.             fh = csv.reader(codecs.iterdecode(httpstream, 'utf-8'))
  69.  
  70.             # fh is already a list, so we don't have to use 'rstrip' or 'split'
  71.             for line in fh:
  72.  
  73.                 if not line or line[0].startswith("#"):
  74.                     continue
  75.  
  76.                 if line[0].startswith('Date'):
  77.                     # The 'Date' line contains several dates in ascending order from left to right.
  78.                     # This grabs the right most date which is the most recent.
  79.                     date = line[8]
  80.                 else:
  81.                     try:
  82.                         # The value at position 8 in the currency lines correspond to the most recent date.
  83.                         value = float(line[8])
  84.                         self.rates[line[0]] = value
  85.                     except ValueError:
  86.                         pass
  87.             return "Exchange rates date: " + date
  88.  
  89.         except Exception as e:
  90.             return "Failed to download:\n%s" % e
  91.  
  92. app = QApplication(sys.argv)
  93. form = Form()
  94. form.show()
  95. app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement