Wednesday, August 19, 2009

I created this Blog to help me keep track of my progress as I learn the Python programing language. For me, it has been a slow process. I wrote a simple gas mileage calculator, and for me it is a mile stone. I like GUI's, so I added some Tkinter code. I also added a button that will save the data to a text file object. I learn a lot from watching youtube videos, but I have some good books too. I program for a hobby, and it helps me understand how computers work, plus I think that its good for my brian. I am currently using version 2.6, mainly because Tkinter works different in 3.

#Simple gas mileage calculator by Steven Atchison 8/18/09

from Tkinter import *

def showMPG():
print(mpg)


def MilageCalc():
theGals = input("Gallons? ")
Odometer = input("Miles? ")
mpg= (float(Odometer) / float(theGals))

print('Miles per gallon = %f'%mpg )

#Open a textfile (a file object) and save
def Saveit():
aFileObject = open('AveoMPG.txt','a')
aFileObject.writelines(str(mpg)+ '\n')
aFileObject.close()


root = Tk()
aButton = Button(root,text = 'Gas mileage calculator',command = MilageCalc).pack()
saveBtn = Button(root,text = 'Save', command = Saveit).pack()
stopBtn = Button(root,text = 'Close program ', command = quit).pack()
testbtn = Button(root,text = 'print mpg', command = showMPG).pack()
root.mainloop()

1 comment:

  1. FYI- When I pasted this into my blog, it removed the indents.

    ReplyDelete

Followers

Blog Archive

About Me

My photo
Biking helps me to cope with life.