[Tutor] How do we create a GUI to run a simple calculation program in Python?

Sri Kavi gvmcmt at gmail.com
Sat Apr 15 03:03:48 EDT 2017


On Wed, Apr 5, 2017 at 12:42 AM, Alan Gauld via Tutor <tutor at python.org>
wrote:

> On 04/04/17 17:55, Lisa Hasler Waters wrote:
>
> > A middle school student of mine created a program to calculate simple and
> > compound interest. He built it in PyCharm EDU using a Mac running
> 10.11.6.
> >
> > He would like to create a GUI to run this program. Please, can you advise
> > on how he could build this?
>
> He could use Tkinter, or he could create an HTML screen and
> write a small server using a Framework like Flask.
>
> Whatever he does he will need to dsepsarate his UI from his
> logic - a good programming skill regardless of UI.
>
> So his first step should be to create a CLI UI on top of
> his existing code such that none of hi functions contain
> print() or input() statements, they should all be in
> new UI code.
>
> The next step is to convert it to event driven style.
> For this code that should almost be a done deal.
>
> Finally decide on his GUI/Web framework and do a tutorial
> to get up to speed and fit his new event-driven backend
> code into that.
>
> > Here is his code:
> >
> > def simple(m, t, r):
> >     r = r/100
> >     print("The interest is {} and the total is {} ".format(r*m*t,
> m+r*m*t))
>
> Should return a value not print a message
>
> > def compound(m, t, r):
> >     morg = m
> >     r = r/100
> >     for x in range(0, t):
> >         m = m*r+m
> >     print("The interest is {} and the total is {} if compounded
> > yearly.".format(m-morg, m))
> >     m = morg
> >     r = r/12
> >     for x in range(0, t*12):
> >         m = m*r+m
> >     print("The interest is {} and the total is {} if compounded
> > monthly.".format(m-morg, m))
> >
>
> Possiobly should be two separate methods, and definitely
> should be returning values not printing stuff.
>
>
> > choice = str(input("Would you like to use simple or compound interest?
> "))
> > m = int(input("Input the amount of money you would like to deposit
> > (don't use the $ symbol): "))
> > t = int(input("Input the amount of time you will be keeping your money
> > in the bank (in years): "))
> > r = int(input("Input the interest rate the bank offers (don't use the
> > % symbol): "))
> >
> > if choice == 'simple':
> >     simple(m, t, r)
> > elif choice == 'compound':
> >     compound(m, t, r)
> > else:
> >     print("Your input is invalid")
>
> This needs to turn into a UI event loop which it almost is
> but with no loop and no exit option.
>
>

I’m feeling inspired by Alan Gauld’s reply and using this as an opportunity
to learn Flask.  Thank you, Alan. I have written a similar calculator
program and created two separate versions of CLI UI using argparse and
Google’s Python Fire. I’m trying to create an HTML screen and use my Python
script that imports the Flask module which will pass data to the HTML file.
When the web page is loaded, it should run the code associated with the
page.


Once I can make this work, I will submit it for your kind comments :)

Sri


More information about the Tutor mailing list