Advertising (This ad goes away for registered users. You can Login or Register)

[Tutorial] Python GUI Programming Part II

Discuss about your favorite (gaming...or not) devices here. The most popular ones will end up getting their own categories
Programming discussions for your favorite Device
Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
Post Reply
User avatar
Acid_Snake
Retired Mod
Posts: 3099
Joined: Tue May 01, 2012 11:32 am
Location: Behind you!

[Tutorial] Python GUI Programming Part II

Post by Acid_Snake » Sun Aug 12, 2012 11:05 pm

Now that you know the basics of windows and widgets will see some more useful Tkinter widgets.


Button:
The button widget is used for creating a button that triggers something when the user clicks on it.

Code: Select all

button = Tkinter.Button(main_window, text="This is a button\nClick Me!", command=main)
The three values passed to the button are:
- main_window: the variable of the main window where the button will be placed
- text: the text to be displayed in the button
- command: the function to call when the button is pressed. Be careful because main and main() are very different, when you give a function value it has to be just the name of the function without brackets, otherwise it will autoexecute.


Checkbutton:
A checkbutton is used to allow the user to select a true/false option, it requires the use of IntVar() to keep track of its state:

Code: Select all

from Tkinter import IntVar
checkbutton_variable = IntVar()
checkbutton = Tkinter.Checkbutton(main_window, text="This is a checkbutton", variable=checkbutton_variable)
The only difference with the button widget is the variable value, used to assign a variable that keeps track of the state of the checkbutton (ticked or not), to find the state of the checkbutton we would use:

Code: Select all

checkbutton_variable.get()
This will return 0 if not ticked, 1 if ticked:

Code: Select all

if checkbutton_variable.get() == 0: print "Not Ticked"
elif checkbutton_variable.get() == 1: print "Ticked"
Take good note of the use of IntVar() because you're going to use it in other widgets as well.


Entry:
This widget is used to receive a string from the user.

Code: Select all

entry = Tkinter.Entry(main_window)
As you can see only one value is needed to be passed to this widget.
To get the text inputted by the user:

Code: Select all

entry.get()
Pretty simple right? lets move on...


Radiobutton:
Did you take note about IntVar()? Good cause you're gonna need it for this widget. The radiobutton widget is used to allow the user to choose between two or more values:

Code: Select all

radiobutton_var = IntVar()
radiobutton1 = Tkinter.Radiobutton(main_window, text="Option 1", variable=radiobutton_var, value=1)
radiobutton2 = Tkinter.Radiobutton(main_window, text="Option 2", variable=radiobutton_var, value=2)
radiobutton3 = Tkinter.Radiobutton(main_window, text="Option 3", variable=radiobutton_var, value=3)
The only change here is value, it is important that you give different values to different radiobuttons to be able to identify what the user has selected.


Listbox:
The listbox widget is similar to the radiobutton but the visual is different. This also allows the user to select different values inside a box:

Code: Select all

listbox = Tkinter.Listbox(main_window)
listbox.insert(1, "Option 1")
listbox.insert(2, "Option 2")
listbox.insert(3, "Option 3")
You can also use a list and the for statement:

Code: Select all

list_ = ["option 1", "option 2", "option 3"]
listbox = Tkinter.Listbox(main_window)
for i in list_:
    listbox.insert(1, i)
To get the value used by the user:

Code: Select all

listbox.get()
But I prefer this:

Code: Select all

listbox.get(listbox.curselection()[0])

Spinbox:
This one is used to select a fix amount (i.e. from 0 to 9, from 10 to 100, etc), it is fairly simple:

Code: Select all

spinbox = Tkinter.Spinbox(main_window, from_=0, to=10)
and of couse:

Code: Select all

spinbox.get()
to get the value selected by the user.
Notice how the value is from_ instead of from, this is to distinguish it from python's built-in from found in code like:

Code: Select all

from Tkinter import IntVar

This are the most used widgets but there are more, you can refer to this great site for more widget and examples:
http://www.tutorialspoint.com/python/py ... amming.htm

Now lets put all the above into practice with the following code:

Code: Select all

#! /usr/bin/env python
import Tkinter
from Tkinter import IntVar

def main():

	if checkbutton_variable.get() == 0: print "Checkbutton: not ticked"
	elif checkbutton_variable.get() == 1: print "Checkbutton: ticked"

	print "Entry:", entry.get()

	if radiobutton_var.get() == 0: print "Radiobutton: None selected"
	elif radiobutton_var.get() == 1: print "Radionbutton: Option 1 selected"
	elif radiobutton_var.get() == 2: print "Radionbutton: Option 2 selected"
	elif radiobutton_var.get() == 3: print "Radionbutton: Option 3 selected"

	try: print "Listbox:", listbox.get(listbox.curselection()[0])
	except: print "Listbox: nothing selected"

	print "Spinbox value:", spinbox.get()

main_window = Tkinter.Tk()

checkbutton_variable = IntVar()
checkbutton = Tkinter.Checkbutton(main_window, text="This is a checkbutton", variable=checkbutton_variable)

entry = Tkinter.Entry(main_window)

radiobutton_var = IntVar()
radiobutton1 = Tkinter.Radiobutton(main_window, text="Option 1", variable=radiobutton_var, value=1)
radiobutton2 = Tkinter.Radiobutton(main_window, text="Option 2", variable=radiobutton_var, value=2)
radiobutton3 = Tkinter.Radiobutton(main_window, text="Option 3", variable=radiobutton_var, value=3)

listbox = Tkinter.Listbox(main_window)
list_ = ["Option 1", "Option 2", "Option 3"]
for i in list_:
    listbox.insert(1, i)
#listbox.insert(1, "Option 1")
#listbox.insert(2, "Option 2")
#listbox.insert(3, "Option 3")

spinbox = Tkinter.Spinbox(main_window, from_=0, to=10)

button = Tkinter.Button(main_window, text="This is a button\nClick Me!", command=main)

checkbutton.pack()
entry.pack()
radiobutton1.pack()
radiobutton2.pack()
radiobutton3.pack()
listbox.pack()
spinbox.pack()
button.pack()

main_window.mainloop()
Which will give the following result:
tkinter.png
tkinter
tkinter.png (60.07 KiB) Viewed 2168 times
As you can see pressing the button will call the main function, which will give you details about the state of the various widgets.

See you soon...

Previous: viewtopic.php?f=37&t=13056
Next: viewtopic.php?f=37&t=13112
Advertising

Post Reply

Return to “Programming and Security”