You are on page 1of 40

Python - GUI Programming (Tkinter)

Python provides various options for developing graphical user interfaces (GUIs). Most important are listed elo!" # # # Tkinter" Tkinter is the Python interface to the Tk GUI toolkit shipped !ith Python. $e !ould look this option in this tutorial. !%Python" This is an open-source Python interface for !%$indo!s http"&&!%python.org. 'Python" 'Python is a Python port for 'ava( !hich gives Python scripts seamless access to 'ava class li raries on the local machine http"&&!!!.)ython.org.

Tkinter Programming"
# Tkinter is the standard GUI li rary for Python. Python !hen com ined !ith Tkinter provides a fast and easy !ay to create GUI applications. Tkinter provides a po!erful o )ect-oriented interface to the Tk GUI toolkit. *reating a GUI application using Tkinter is an easy task. +ll you need to do is perform the follo!ing steps" , -%ample"Import the Tkinter module. , *reate the GUI application main !indo!. , +dd one or more of the a ove mentioned !idgets to the GUI application. -nter the main event loop to take action against each event triggered y the user.

Python - Tkinter .utton The .utton !idget is used to add uttons in a Python application. These uttons can display te%t or images that convey the purpose of the uttons. /ou can attach a function or a method to a utton( !hich is called automatically !hen you click the utton. 0ynta%" w = Button ( master, option=value, ... ) Parameters" , master" This represents the parent !indo!. , options" 1ere is the list of most commonly used options for this !idget. These options can e used as key-value pairs separated y commas.

Try" tkMessage.o%.askokcancel(..)

Try"
tkMessage.o%.ask2uestion tkMessage.o%.askretrycancel tkMessage.o%.askyesno tkMessage.o%.sho!error tkMessage.o%.sho!!arning

Python - Tkinter *anvas The *anvas is a rectangular area intended for dra!ing pictures or other comple% layouts. /ou can place graphics( te%t( !idgets( or frames on a *anvas. 0ynta%" w = Canvas ( master, option=value, ... ) # Parameters" , master" This represents the parent !indo!. , options" 1ere is the list of most commonly used options for this !idget. These options can e used as key-value pairs separated y commas.

The *anvas !idget can support the follo!ing standard items" # arc . *reates an arc item. coord = 10, 50, 240, 210 arc = canvas.create_arc(coord, start=0, extent=150, ill=!"lue!) # image . *reates an image item( !hich can e an instance of either the .itmapImage or the PhotoImage classes. ilename = #$oto%ma&e( ile = !suns$ine.&i !) ima&e = canvas.create_ima&e(50, 50, anc$or='(, ima&e= ilename) # line . *reates a line item. line = canvas.create_line(x0, )0, x1, )1, ..., xn, )n, options) # oval . *reates a circle or an ellipse at the given coordinates. oval = canvas.create_oval(x0, )0, x1, )1, options) # polygon . *reates a polygon item that must have at least three vertices. oval = canvas.create_pol)&on(x0, )0, x1, )1,...xn, )n, options)

Python - Tkinter *heck utton The *heck utton !idget is used to display a num er of options to a user as toggle uttons. The user can then select one or more options y clicking the utton corresponding to each option. /ou can also display images in place of te%t. 0ynta%" w = C$ec*"utton ( master, option, ... ) # Parameters" , master" This represents the parent !indo!. , options" 1ere is the list of most commonly used options for this !idget. These options can e used as key-value pairs separated y commas.

Python - Tkinter -ntry" # The -ntry !idget is used to accept single-line te%t strings from a user. # If you !ant to display multiple lines of te%t that can e edited( then you should usethe Te%t !idget. # If you !ant to display one or more lines of te%t that cannot e modified y the user then you should use the 3a el !idget. 0ynta%" 1ere is the simple synta% to create this !idget" w = (ntr)( master, option, ... ) Parameters" , master" This represents the parent !indo!. , options" 1ere is the list of most commonly used options for this !idget. These options can e used as key-value pairs separated y commas.

Python - Tkinter 4rame # The 4rame !idget is very important for the process of grouping and organi5ing other !idgets in a someho! friendly !ay. It !orks like a container( !hich is responsi le for arranging the position of other !idgets. # It uses rectangular areas in the screen to organi5e the layout and to provide padding of these !idgets. + frame can also e used as a foundation class to implement comple% !idgets. 0ynta%" 1ere is the simple synta% to create this !idget" w = +rame ( master, option, ... ) Parameters" , master" This represents the parent !indo!. , options" 1ere is the list of most commonly used options for this !idget. These options can e used as key-value pairs separated y commas.

Python - Tkinter 3a el # This !idget implements a display o% !here you can place te%t or images. The te%t displayed y this !idget can e updated at any time you !ant. # It is also possi le to underline part of the te%t (like to identify a key oard shortcut)( and span the te%t across multiple lines. 0ynta%" 1ere is the simple synta% to create this !idget" w = ,a"el ( master, option, ... ) Parameters" , master" This represents the parent !indo!. , options" 1ere is the list of most commonly used options for this !idget. These options can e used as key-value pairs separated y commas.

Python - Tkinter 3ist o% # The 3ist o% !idget is used to display a list of items from !hich a user can select a num er of items 0ynta%" # 1ere is the simple synta% to create this !idget" w = ,ist"ox ( master, option, ... ) Parameters" , master" This represents the parent !indo!. , options" 1ere is the list of most commonly used options for this !idget. These options can e used as key-value pairs separated y commas.

Python - Tkinter Menu utton # + menu utton is the part of a drop-do!n menu that stays on the screen all the time. -very menu utton is associated !ith a Menu !idget that can display the choices for that menu utton !hen the user clicks on it. 0ynta%" 1ere is the simple synta% to create this !idget" w = -enu"utton ( master, option, ... ) Parameters" , master" This represents the parent !indo!. , options" 1ere is the list of most commonly used options for this !idget. These options can e used as key-value pairs separated y commas.

-%ample" rom .*inter import / import t*-essa&eBox import .*inter top = .*() m"= -enu"utton ( top, text=!condiments!, relie =01%2(3 ) m".&rid() m".menu = -enu ( m", tearo = 0 ) m"4!menu!5 = m".menu ma)o6ar = %nt6ar() *etc$6ar = %nt6ar() m".menu.add_c$ec*"utton ( la"el=!ma)o7, varia"le=ma)o6ar ) m".menu.add_c$ec*"utton ( la"el=!*etc$up7, varia"le=*etc$6ar ) m".pac*() top.mainloop()

Python - Tkinter Message # This !idget provides a multiline and nonedita le o )ect that displays te%ts( automatically reaking lines and )ustifying their contents. # Its functionality is very similar to the one provided y the 3a el !idget( e%cept that it can also automatically !rap the te%t( maintaining a given !idth or aspect ratio. 0ynta%" 1ere is the simple synta% to create this !idget" w = -essa&e ( master, option, ... ) Parameters" , master" This represents the parent !indo!. , options" 1ere is the list of most commonly used options for this !idget. These options can e used as key-value pairs separated y commas.

-%ample" rom .*inter import / root = .*() var = 2trin&6ar() la"el = -essa&e( root, textvaria"le=var, relie =01%2(3 ) var.set(!8e)9: 8ow are )ou doin&:!) la"el.pac*() root.mainloop()

Python - Tkinter 6adio utton # This !idget implements a multiple-choice utton( !hich is a !ay to offer many possi le selections to the user( and let user choose only one of them. # In order to implement this functionality( each group of radio uttons must e associated to the same varia le( and each one of the uttons must sym oli5e a single value. /ou can use the Ta key to s!itch from one radion utton to another. 0ynta%" 1ere is the simple synta% to create this !idget" w = 0adio"utton ( master, option, ... ) Parameters" , master" This represents the parent !indo!. , options" 1ere is the list of most commonly used options for this !idget. These options can e used as key-value pairs separated y commas.

-%ample" rom .*inter import / de sel(); selection = !<ou selected t$e option ! = str(var.&et()) la"el.con i&(text = selection) root = .*() var = %nt6ar() 01 = 0adio"utton(root, text=!>ption 1!, varia"le=var, value=1, command=sel) 01.pac*( anc$or = ? ) 02 = 0adio"utton(root, text=!>ption 2!, varia"le=var, value=2,command=sel) 02.pac*( anc$or = ? ) 0@ = 0adio"utton(root, text=!>ption @!, varia"le=var, value=@,command=sel) 0@.pac*( anc$or = ?) la"el = ,a"el(root) la"el.pac*() root.mainloop()

Python - Tkinter 0cale # The 0cale !idget provides a graphical slider o )ect that allo!s you to select values from a specific scale. 0ynta%" 1ere is the simple synta% to create this !idget" w = 2cale ( master, option, ... ) Parameters" , master" This represents the parent !indo!. , options" 1ere is the list of most commonly used options for this !idget. These options can e used as key-value pairs separated y commas.

-%ample" rom .*inter import / de sel(); selection = !6alue = ! = str(var.&et()) la"el.con i&(text = selection) root = .*() var = 3ou"le6ar() scale = 2cale( root, varia"le = var ) scale.pac*(anc$or=C('.(0) "utton = Button(root, text=!Aet 2cale 6alue!, command=sel) "utton.pac*(anc$or=C('.(0) la"el = ,a"el(root) la"el.pac*() root.mainloop()

Python - Tkinter 0croll ar # This !idget provides a slide controller that is used to implement vertical scrolled !idgets( such as 3ist o%( Te%t( and *anvas. 7ote that you can also create hori5ontal scroll ars on -ntry !idgets. 0ynta%" 1ere is the simple synta% to create this !idget" w = 2croll"ar ( master, option, ... ) Parameters" , master" This represents the parent !indo!. , options" 1ere is the list of most commonly used options for this !idget. These options can e used as key-value pairs separated y commas.

-%ample" rom .*inter import / root = .*() scroll"ar = 2croll"ar(root) scroll"ar.pac*( side = 0%A8.,

ill=< )

m)list = ,ist"ox(root, )scrollcommand = scroll"ar.set ) or line in ran&e(100); m)list.insert(('3, !.$is is line num"er ! = str(line)) m)list.pac*( side = ,(+., ill = B>.8 ) scroll"ar.con i&( command = m)list.)view ) mainloop()

Python - Tkinter Te%t # Te%t !idgets provide advanced capa ilities that allo! you to edit a multiline te%t and format the !ay it has to e displayed( such as changing its color and font. # /ou can also use elegant structures like ta s and marks to locate specific sections of the te%t( and apply changes to those areas. Moreover( you can em ed !indo!s and images in the te%t ecause this !idget !as designed to handle oth plain and formatted te%t. 0ynta%" 1ere is the simple synta% to create this !idget" w = .ext ( master, option, ... ) Parameters" , master" This represents the parent !indo!. , options" 1ere is the list of most commonly used options for this !idget. These options can e used as key-value pairs separated y commas.

-%ample" rom .*inter import / de onclic*(); pass root = .*() text = .ext(root) text.insert(%'2(0., !8ello.....!) text.insert(('3, !B)e B)e.....!) text.pac*() text.ta&_add(!$ere!, !1.0!, !1.4!) text.ta&_add(!start!, !1.B!, !1.1@!) text.ta&_con i&(!$ere!, "ac*&round=!)ellow!, ore&round=!"lue!) text.ta&_con i&(!start!, "ac*&round=!"lac*!, ore&round=!&reen!) root.mainloop()

Python - Tkinter Toplevel # Toplevel !idgets !ork as !indo!s that are directly managed y the !indo! manager. They do not necessarily have a parent !idget on top of them. # /our application can use any num er of top-level !indo!s. 0ynta%" 1ere is the simple synta% to create this !idget" w = .oplevel ( option, ... ) Parameters" , options" 1ere is the list of most commonly used options for this !idget. These options can e used as key-value pairs separated y commas.

-%ample" rom .*inter import / root = .*() top = .oplevel() top.mainloop()

Python - Tkinter 0pin o% # The 0pin o% !idget is a variant of the standard Tkinter -ntry !idget( !hich can e used to select from a fi%ed num er of values. 0ynta%" 1ere is the simple synta% to create this !idget" w = 2pin"ox( master, option, ... ) Parameters" , master" This represents the parent !indo!. , options" 1ere is the list of most commonly used options for this !idget. These options can e used as key-value pairs separated y commas.

-%ample" rom .*inter import / master = .*() w = 2pin"ox(master, w.pac*() mainloop() rom_=0, to=10)

Python - Tkinter Paned$indo! # + Paned$indo! is a container !idget that may contain any num er of panes( arranged hori5ontally or vertically. # -ach pane contains one !idget( and each pair of panes is separated y a movea le (via mouse movements) sash. Moving a sash causes the !idgets on either side of the sash to e resi5ed. 0ynta%" 1ere is the simple synta% to create this !idget" w = #aned?indow( master, option, ... ) Parameters" , master" This represents the parent !indo!. , options" 1ere is the list of most commonly used options for this !idget. These options can e used as key-value pairs separated y commas.

-%ample" rom .*inter import / m1 = #aned?indow() m1.pac*( ill=B>.8, expand=1) le t = ,a"el(m1, text=!le t pane!) m1.add(le t) m2 = #aned?indow(m1, orient=6(0.%C1,) m1.add(m2) top = ,a"el(m2, text=!top pane!) m2.add(top) "ottom = ,a"el(m2, text=!"ottom pane!) m2.add("ottom) mainloop()

Python - Tkinter 3a el4rame # + la elframe is a simple container !idget. Its primary purpose is to act as a spacer or container for comple% !indo! layouts. # This !idget has the features of a frame plus the a ility to display a la el. 0ynta%" 1ere is the simple synta% to create this !idget" w = ,a"el+rame( master, option, ... ) Parameters" , master" This represents the parent !indo!. , options" 1ere is the list of most commonly used options for this !idget. These options can e used as key-value pairs separated y commas.

-%ample" rom .*inter import / root = .*() la"el rame = ,a"el+rame(root, text=!.$is is a ,a"el+rame!) la"el rame.pac*( ill=!"ot$!, expand=!)es!) le t = ,a"el(la"el rame, text=!%nside t$e ,a"el+rame!) le t.pac*() root.mainloop()

You might also like