Which Graphics User Interface (GUI) would you recommend for a beginner? I have seen some examples of Tkinter, wxPython and GTK.

Recommended Answers

All 4 Replies

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.

I will check Tkinter programming first. Any examples that show the difference between console and GUI programming?

Here are simple programs. The console version:

# 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, '%')

Now the Tkinter GUI version:

# 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()

Here is the Tkinter sales tax program without the dialog popups ...

# 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()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.