943,536 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 4030
  • Python RSS
Nov 10th, 2006
0

Graphics User Interface for Beginners

Expand Post »
Which Graphics User Interface (GUI) would you recommend for a beginner? I have seen some examples of Tkinter, wxPython and GTK.
Similar Threads
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Nov 12th, 2006
0

Re: Graphics User Interface for Beginners

As a beginner, start working with Tkinter to get a feeling what GUI programming is all about. Tkinter is already present in the normal Python installation. If you need fancier widgets later, download wxPython and experiment with that. PyGTK/GTK works best with Linux, the Windows version needs a lot of additional downloads and the installation process is cumbersome.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 12th, 2006
0

Re: Graphics User Interface for Beginners

I will check Tkinter programming first. Any examples that show the difference between console and GUI programming?
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Nov 12th, 2006
0

Re: Graphics User Interface for Beginners

Here are simple programs. The console version:
python Syntax (Toggle Plain Text)
  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:
python Syntax (Toggle Plain Text)
  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
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Nov 12th, 2006
0

Re: Graphics User Interface for Beginners

Here is the Tkinter sales tax program without the dialog popups ...
python Syntax (Toggle Plain Text)
  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()
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Color Dictionary
Next Thread in Python Forum Timeline: Qestion on Encoding and Decoding.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC