View Single Post
Join Date: Jul 2005
Posts: 1,205
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 130
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Graphics User Interface for Beginners

 
0
  #4
Nov 12th, 2006
Here are simple programs. The console version:
  1. # console sales tax program
  2.  
  3. while True:
  4. price = float(raw_input("Enter purchase price: "))
  5. # prevent division by zero error
  6. if price != 0:
  7. break
  8. else:
  9. print "price can not be zero!"
  10.  
  11. tax = float(raw_input("Enter sales tax paid: "))
  12.  
  13. percent = 100 * tax / price
  14. print "You paid %0.3f%s sales tax" % (percent, '%')
Now the Tkinter GUI version:
  1. # Tkinter sales tax program
  2.  
  3. from Tkinter import *
  4. import tkSimpleDialog
  5.  
  6. # the basic window
  7. root = Tk()
  8. # create label for result
  9. label1 = Label(root)
  10. # position the label in window
  11. label1.grid(row=0, column=0)
  12. # ask for needed data with dialog window
  13. # the askfloat dialog window makes certain you entered floating point value
  14. # and also prevents you from entering zero which would give divide by zero error
  15. price = tkSimpleDialog.askfloat("Price", "Enter purchase price:", parent=root, minvalue=0.01)
  16. tax = tkSimpleDialog.askfloat("Tax", "Enter sales tax paid:", parent=root)
  17. percent = 100 * tax / price
  18. result = "You paid %0.3f%s sales tax" % (percent, '%')
  19. # display result string in label1
  20. label1.config(text=result)
  21. # event loop needed by GUI, checks for mouse and key events
  22. root.mainloop()
Last edited by vegaseat; Aug 13th, 2009 at 4:11 pm. Reason: newer code tags
Reply With Quote