Write simple GUI for Python!

Introduction

In previous article
I have explained how to build simple currency converter, now I will make simple GUI for our program. Just to be clear this is not another "Hello World" tutorial, our program will be able to take user input, find data on internet for conversion rate, and then show it to us in simple window format. If you have read article about currency converter, I have used two internet sources to get conversion rate this time I will use only one http://rate-exchange.appspot.com , but you will be able to add other source alone.

Prerequisites

This tutorial will be about "Canopy Express", https://www.enthought.com/canopy-express/ . I strongly suggest you to install this software, it is free and has everything you need, python 2.7, PythonIDE, and all that custom libraries for python development. What I also find great about it, its easy way to install, just download installer for windows or setup scripts for Linux and Mac. Of course when you build programs this way you don't have to worry about cross platform compatibility since it's going to work on all major platforms.

If you don't wont to install Canopy, you can still follow this tutorial if you have:

Currency Converter

I'm not going to go into details about currency converter, you have that in other article on this site. You can find code for currency converter without GUI on github. First lets start with importing all we are going to need for this project:

import requests
from traits.api import *
from traitsui.api import *

In case you have installed Canopy Express and have wxPython and PyQT4, and don't know witch to use you can add following on top of your program:

from traits.etsconfig.api import ETSConfig
ETSConfig.toolkit = 'wx'
import requests
from traits.api import *
from traitsui.api import *

for qt you will change wx line, note that this line must be first line of code before anything else is imported. Now lets build our class:

import requests
from traits.api import *
from traitsui.api import *

class Currency_Converter(HasTraits):
        a_from = Str()
        b_to = Str()
        rate_ab = Property(Float, depends_on = 'a_from, b_to')

Notice that string(str) in traits is Str and float is Float, int is Int and so on. I have left "a" and "b" variables from latest article so you can track this more easily. Now lets define function to return rate_ab witch is variable for currency exchange rate:

def _get_rate_ab(self):
        self.a_from = self.a_from.upper()
        self.b_to = self.b_to.upper()
        return requests.get(('http://rate-exchange.appspot.com/currency?from=%s&to=%s&q=1') % (self.a, self.b)).json[('v')]

This function takes user input's a_from and b_to as strings and returns value from internet for rate_ab. Look code from last article and notice bit change or shortened way to return value, its all same except it executes in one line. There is one more thing we need to think about, when we start program it will try to return rate value from non existing web page so we will change out variables a_from and b_to so they have some default values when program start's:

a_from = Str('USD')
b_to = Str('GBP')

And now all together:

import requests
from traits.api import *
from traitsui.api import *

class Currency_Converter(HasTraits):
        a_from = Str('USD')
        b_to = Str('GBP')
        rate_ab = Property(Float, depends_on = 'a_from, b_to')

        def _get_rate_ab(self):
                self.a_from = self.a_from.upper()
                self.b_to = self.b_to.upper()
                return requests.get(('http://rate-exchange.appspot.com/currency?from=%s&to=%s&q=1') % (self.a_from, self.b_to)).json[('v')]

Currency_Converter().configure_traits()

If you did everything right you should have your cross platform currency converter, for more information make sure you visit and read canopy doc's.

Comments !

social