Skip to content

Instantly share code, notes, and snippets.

@blakebjorn
Created April 17, 2016 16:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blakebjorn/95d8f54dc4055d9e6457b2cdbfb9b65f to your computer and use it in GitHub Desktop.
Save blakebjorn/95d8f54dc4055d9e6457b2cdbfb9b65f to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 17 11:45:45 2016
@author: Blake Anderson
"""
from PySide import QtGui
from PySide import QtCore
import requests
import sys
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
response = requests.get('http://api.fixer.io/latest?base=USD')
self.currencyDict = response.json()['rates']
self.currencyDict['USD']=1.0
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Currency Converter')
self.mainLayout = QtGui.QVBoxLayout()
self.inputValueLabel = QtGui.QLabel("Starting Amount:")
self.inputValueField = QtGui.QDoubleSpinBox()
self.inputCurrencyLabel = QtGui.QLabel("Input Currency:")
self.outputCurrencyLabel = QtGui.QLabel("Output Currency:")
self.inputCurrency = QtGui.QComboBox()
self.outputCurrency = QtGui.QComboBox()
for currency in self.currencyDict.keys():
self.inputCurrency.addItem(currency)
self.outputCurrency.addItem(currency)
self.calculateButton = QtGui.QPushButton("Calculate!")
self.calculateButton.clicked.connect(self.calculate_operation)
self.mainLayout.addWidget(self.inputValueLabel)
self.mainLayout.addWidget(self.inputValueField)
self.mainLayout.addWidget(self.inputCurrencyLabel)
self.mainLayout.addWidget(self.inputCurrency)
self.mainLayout.addWidget(self.outputCurrencyLabel)
self.mainLayout.addWidget(self.outputCurrency)
self.mainLayout.addWidget(self.calculateButton)
self.setLayout(self.mainLayout)
self.show()
def calculate_operation(self):
try:
startingCurrency = self.inputCurrency.currentText()
targetCurrency = self.outputCurrency.currentText()
startingAmount = float(self.inputValueField.text())
valUSD = startingAmount if startingCurrency == "USD" else startingAmount / self.currencyDict[startingCurrency]
finalValue = valUSD * self.currencyDict[targetCurrency]
QtGui.QMessageBox.information(self,"Calculation Result",str(startingAmount)+" "+startingCurrency+" is equal to "+str(round(finalValue,2))+" "+targetCurrency)
except Exception as e:
QtGui.QMessageBox.warning(self,"Error","Not all fields contain acceptable values\nError: "+str(e))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = MainWindow()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment