| | |
Graphics User Interface for Beginners
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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.
May 'the Google' be with you!
Here are simple programs. The console version:
Now the Tkinter GUI version:
python Syntax (Toggle Plain Text)
# console sales tax program while True: price = float(raw_input("Enter purchase price: ")) # prevent division by zero error if price != 0: break else: print "price can not be zero!" tax = float(raw_input("Enter sales tax paid: ")) percent = 100 * tax / price print "You paid %0.3f%s sales tax" % (percent, '%')
python Syntax (Toggle Plain Text)
# Tkinter sales tax program from Tkinter import * import tkSimpleDialog # the basic window root = Tk() # create label for result label1 = Label(root) # position the label in window label1.grid(row=0, column=0) # ask for needed data with dialog window # the askfloat dialog window makes certain you entered floating point value # and also prevents you from entering zero which would give divide by zero error price = tkSimpleDialog.askfloat("Price", "Enter purchase price:", parent=root, minvalue=0.01) tax = tkSimpleDialog.askfloat("Tax", "Enter sales tax paid:", parent=root) percent = 100 * tax / price result = "You paid %0.3f%s sales tax" % (percent, '%') # display result string in label1 label1.config(text=result) # event loop needed by GUI, checks for mouse and key events root.mainloop()
Last edited by vegaseat; Aug 13th, 2009 at 4:11 pm. Reason: newer code tags
Here is the Tkinter sales tax program without the dialog popups ...
python Syntax (Toggle Plain Text)
# a rather generic Tkinter GUI template for calculations # uses Entry() for data input and Button() to start calculation from Tkinter import * def calculate(): try: # get the enter1 and enter2 values price = float(enter1.get()) tax = float(enter2.get()) # do the calculation percent = 100 * tax / price result = "You paid %0.3f%s sales tax" % (percent, '%') # display the result string label3.config(text=result) except ValueError: label3.config(text='Enter numeric values!') except ZeroDivisionError: label3.config(text='Price can not be zero!') enter1.focus_set() root = Tk() # window geometry is width x height + x_offset + y_offset root.geometry("200x150+30+30") # first entry with label label1 = Label(root, text='Enter purchase price:') label1.grid(row=0, column=0) enter1 = Entry(root, bg='yellow') enter1.grid(row=1, column=0) # second entry with label label2 = Label(root, text='Enter sales tax paid:') label2.grid(row=2, column=0) enter2 = Entry(root, bg='yellow') enter2.grid(row=3, column=0) # do the calculation by clicking the button btn1 = Button(root, text='Calculate Percent', command=calculate) btn1.grid(row=4, column=0) # display the result in this label label3 = Label(root) label3.grid(row=5, column=0) # start cursor in enter1 enter1.focus() # value has been entered in enter1 now switch focus to enter2 enter1.bind('<Return>', func=lambda e: enter2.focus_set()) root.mainloop()
May 'the Google' be with you!
![]() |
Similar Threads
- Java using BlueJ- GUI (Graphic User Interface) (Java)
- Help (Python)
- BlueJ problem (Java)
- How do you build the dot in calculator. (C++)
- Bgi Graphics Don't Work On Windows... Why??? (Game Development)
Other Threads in the Python Forum
- Previous Thread: Color Dictionary
- Next Thread: Qestion on Encoding and Decoding.
| Thread Tools | Search this Thread |
address alarm anydbm app beginner cipher conversion coordinates curves cx-freeze data development dictionary directory dynamic examples excel feet file float format function generator getvalue gui halp handling homework images import input ip itunes java keycontrol line linux list lists loop maintain maze millimeter mouse mysqldb number numbers output parsing path port prime programming projects py2exe pygame pyglet pymailer python queue random recursion recursive screensaverloopinactive script scrolledtext searchingfile slicenotation split ssh string strings table terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice windows wx.wizard wxpython xlwt






