Advertisement
Guest User

Untitled

a guest
Aug 30th, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. from PySide.QtGui import *
  2. import urllib.request
  3. import sys
  4.  
  5.  
  6. class Form(QDialog):
  7.     def __init__(self, parent=None):
  8.         super(Form, self).__init__(parent)
  9.  
  10.         self.rates = {}
  11.         date = self.get_date()
  12.         rates = sorted(self.rates.keys())
  13.  
  14.         self.dateLabel = QLabel(date)
  15.         self.fromComboBox = QComboBox()
  16.         self.fromComboBox.addItems(rates)
  17.         self.fromSpinBox = QDoubleSpinBox()
  18.         self.fromSpinBox.setRange(0.01, 100000000.00)
  19.         self.fromSpinBox.setValue(1.00)
  20.         self.toComboBox = QComboBox()
  21.         self.toComboBox.addItems(rates)
  22.         self.toLabel = QLabel("1.00")
  23.  
  24.         grid = QGridLayout()
  25.         grid.addWidget(self.dateLabel, 0, 0)
  26.         grid.addWidget(self.fromComboBox, 1, 0)
  27.         grid.addWidget(self.fromSpinBox, 1, 1)
  28.         grid.addWidget(self.toComboBox, 2, 0)
  29.         grid.addWidget(self.toLabel, 2, 1)
  30.         self.setLayout(grid)
  31.  
  32.         self.fromComboBox.currentIndexChanged().connect(self.update_ui)
  33.         self.toComboBox.currentIndexChanged().connect(self.update_ui)
  34.         self.fromSpinBox.valueChanged().connect(self.update_ui)
  35.  
  36.     def update_ui(self):
  37.         to = self.toComboBox.currentText()
  38.         from_ = self.fromComboBox.currentText()
  39.  
  40.         amount = (self.rates[from_] / self.rates[to]) * self.fromSpinBox.value()
  41.         self.toLabel.setText("%0.2f" % amount)
  42.  
  43.     def get_date(self):
  44.         try:
  45.             date = "Unknown"
  46.  
  47.             fh = urllib.request.urlopen("http://www.bankofcanada.ca/en/markets/csv/exchange_eng.csv")
  48.  
  49.             for line in fh:
  50.                 line = line.rstrip()
  51.  
  52.                 if not line or line.startsWith(("#", "Closing")):
  53.                     continue
  54.  
  55.                 fields = line.split(",")
  56.                 if line.startsWith("Date"):
  57.                     date = fields[-1]
  58.                 else:
  59.                     try:
  60.                         value = float(fields[-1])
  61.                         self.rates[fields[0]] = value
  62.                     except ValueError:
  63.                         pass
  64.             return "Exchange rates date: " + date
  65.         except Exception as e:
  66.             return "Failed to download:\n%s" % e
  67.  
  68.  
  69. app = QApplication(sys.argv)
  70. form = Form()
  71. form.show()
  72. app.exec_()
  73.  
  74.  
  75. =========================
  76.  
  77.  
  78. Traceback (most recent call last):
  79.   File "C:xxx/main.py", line 70, in <module>
  80.     form = Form()
  81.   File "C:xxx/main.py", line 32, in __init__
  82.     self.fromComboBox.currentIndexChanged().connect(self.update_ui)
  83. TypeError: native Qt signal is not callable
  84.  
  85. Process finished with exit code 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement