Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Python 3 version of Bo Milanovich's Currency Converter Python/Qt tutorial (https://www.youtube.com/watch?v=8D_aEYiBU2c)
- import sys
- import urllib.request
- import csv
- import codecs
- from PySide.QtCore import *
- from PySide.QtGui import *
- class Form(QDialog):
- def __init__(self, parent=None):
- super(Form,self).__init__(parent)
- # I had to move this from the 'getdate' function to the __init__. Otherwise got error:
- # AttributeError: 'Form' object has no attribute 'rates'
- self.rates = {}
- # Has to be 'getdate()', not 'getdate' otherwise it returns the object reference, like this:
- # <bound method Form.getdate of <__main__.Form object at 0x0000000004026BC8>>.
- #
- # This results in the following error when trying to initialize the QLabel widget below:
- # TypeError: 'PySide.QtGui.QLabel' called with wrong argument types:
- # PySide.QtGui.QLabel(method)
- date = self.getdate
- rates = sorted(self.rates.keys())
- dateLabel = QLabel(date)
- self.fromComboBox = QComboBox()
- self.fromComboBox.addItems(rates)
- self.fromSpinBox = QDoubleSpinBox() # Takes Double data type, not just integers.
- self.fromSpinBox.setRange(0.01,10000000.00) # Ranges allowed for spinbox
- self.fromSpinBox.setValue(1.00) # Default value
- self.toComboBox = QComboBox()
- self.toComboBox.addItems(rates)
- self.toLabel = QLabel("1.00") # Default value
- grid = QGridLayout() # Instead of vertical layout QVBoxLayout()
- # Works like the Tkinter grid geometry manager!
- grid.addWidget(dateLabel, 0, 0) # row,column
- grid.addWidget(self.fromComboBox, 1, 0)
- grid.addWidget(self.fromSpinBox, 1, 1)
- grid.addWidget(self.toComboBox, 2, 0)
- grid.addWidget(self.toLabel, 2, 1)
- self.setLayout(grid)
- # The signal currentIndexChanged is emitted when the selected item in a combo box changes.
- self.connect(self.fromComboBox, SIGNAL("currentIndexChanged(int)"), self.updateUi)
- self.connect(self.toComboBox, SIGNAL("currentIndexChanged(int)"), self.updateUi)
- # The signal valueChanged is emitted when the value in the spinbox changes.
- self.connect(self.fromSpinBox, SIGNAL("valueChanged(double)"), self.updateUi)
- def updateUi(self):
- to = self.toComboBox.currentText()
- from_ = self.fromComboBox.currentText() # Underscore is used because 'from' is a reserved word in Python.
- amount = (self.rates[from_] / self.rates[to]) * self.fromSpinBox.value()
- self.toLabel.setText("%0.2f" % amount)
- def getdate(self):
- try:
- date = "Unknown"
- url = "http://www.bankofcanada.ca/en/markets/csv/exchange_eng.csv"
- httpstream = urllib.request.urlopen(url)
- # Because urllib returns bytes, they have to be decoded.
- fh = csv.reader(codecs.iterdecode(httpstream, 'utf-8'))
- # fh is already a list, so we don't have to use 'rstrip' or 'split'
- for line in fh:
- if not line or line[0].startswith("#"):
- continue
- if line[0].startswith('Date'):
- # The 'Date' line contains several dates in ascending order from left to right.
- # This grabs the right most date which is the most recent.
- date = line[8]
- else:
- try:
- # The value at position 8 in the currency lines correspond to the most recent date.
- value = float(line[8])
- self.rates[line[0]] = value
- except ValueError:
- pass
- return "Exchange rates date: " + date
- except Exception as e:
- return "Failed to download:\n%s" % e
- app = QApplication(sys.argv)
- form = Form()
- form.show()
- app.exec_()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement