View Single Post
Join Date: Oct 2004
Posts: 3,872
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Graphics User Interface for Beginners

 
0
  #5
Nov 12th, 2006
Here is the Tkinter sales tax program without the dialog popups ...
  1. # a rather generic Tkinter GUI template for calculations
  2. # uses Entry() for data input and Button() to start calculation
  3.  
  4. from Tkinter import *
  5.  
  6. def calculate():
  7. try:
  8. # get the enter1 and enter2 values
  9. price = float(enter1.get())
  10. tax = float(enter2.get())
  11. # do the calculation
  12. percent = 100 * tax / price
  13. result = "You paid %0.3f%s sales tax" % (percent, '%')
  14. # display the result string
  15. label3.config(text=result)
  16. except ValueError:
  17. label3.config(text='Enter numeric values!')
  18. except ZeroDivisionError:
  19. label3.config(text='Price can not be zero!')
  20. enter1.focus_set()
  21.  
  22. root = Tk()
  23. # window geometry is width x height + x_offset + y_offset
  24. root.geometry("200x150+30+30")
  25.  
  26. # first entry with label
  27. label1 = Label(root, text='Enter purchase price:')
  28. label1.grid(row=0, column=0)
  29. enter1 = Entry(root, bg='yellow')
  30. enter1.grid(row=1, column=0)
  31.  
  32. # second entry with label
  33. label2 = Label(root, text='Enter sales tax paid:')
  34. label2.grid(row=2, column=0)
  35. enter2 = Entry(root, bg='yellow')
  36. enter2.grid(row=3, column=0)
  37.  
  38. # do the calculation by clicking the button
  39. btn1 = Button(root, text='Calculate Percent', command=calculate)
  40. btn1.grid(row=4, column=0)
  41.  
  42. # display the result in this label
  43. label3 = Label(root)
  44. label3.grid(row=5, column=0)
  45.  
  46. # start cursor in enter1
  47. enter1.focus()
  48. # value has been entered in enter1 now switch focus to enter2
  49. enter1.bind('<Return>', func=lambda e: enter2.focus_set())
  50.  
  51. root.mainloop()
May 'the Google' be with you!
Reply With Quote