soar, you were nroduced to tractive mde and se
nal
jpbosed programming.
ptr shows youhow to create programs thet apearin windows nd
utes sch 9 butons 0nd text bows, The Cambridge GCSE and O Level Computer
Slabs does not equire that you pode appication ith GU; however you
"vant ts produce move visually iteestingana profesional ooking saitions to
ns, Doings wil also ma
nto GU appistions.
exble programmer
yourefermatyour
own, norma script based solution ate golngo be eee toa text-based
ns and programs that appeainwindows ae gongtobe called Gul solutions. Aer
Spt youvaloen be asked ta produce two solitons, esta tert based one and then
elution, Producing the GUI programs canbe conser aptional estersons
myring the iter module, iseas to produce visually ch Gus nd attach your
rim to buttons in windows
ines n example of GUI ook nds provided as part ofthe standard ibrary when you
sod Peron. Therefore you aveodhave acces tothe objects and method
feu applstions, and you jst need taco these entra tasks
ort the trtermodvle
“edo
2 iene themain unter window.
Chapter 5
14 Exerthe min event loop hich stent and acts upon evant triggered bythe we.
GUI Applications (Optional) Po
eee
Psbuton cn calor particu action to happen by refesngto function by name ater
; fmm nthe button cefnton code. Tocrestethe aplication shown i Figute 01
ec you willunderstand:
by the end ofthis chapter you wil under op he cos ita saw scp nd save tashello-gut py nto yurPython
tow to produce and save GU applications ize: elder:
fom te progam windowed applaonsusingte bun hiner GUI ma
{nowt add widgetsto.a Ul apbeation
‘Dhow aout widgets nan application window
1 oo ger function alls with buttons,
—from tkinter import *
punetion called by clicking =
ef change tex)
tny_1abel config (text="Hello Worta*)
seindow = K()
window. tele (My Application")
nad an onpty ekinter label widget and place it in a grid
‘ny tabel = Label window, widthe25, heighte1, text="')
wnyclabel grid zow-0, colum=0)
# nad a ckinter button widget, place it in the grid layout
fond attach the change_text() function
ny button = Sutton(window, text="Say Hit, widt!
fny_button.gria(row-1, column=0)
0, conmand-change_text)
window mainloop ()
sayH
Figure.o1 AGU application ith abuton
Watton to ze how this small appctionwers
ter unningte code, pressthe’Say Hi button to see hot
Notcehow the button sirkedtothe change_text (function by command inthe
baton detniion.
unre ronurion
‘hettetr mel prodescst objects and mata hayoucan essa seo
aa crn as wtenuangobjecoerted pogamnirg OP hichis
re erent beslabes nOOP oyare et mehesafe aces ge
aa ecanbese abo nvechange exe) clon whee te cones)
trethod sappled ote eb
fyouwattolenmer soa OP oun etonokrouh Aes Bulire
eee Ttet boca te codne Cubes opens vy rredctonPogranmia wt
Pereity vce ong chines iv prog angie wert. ge
se ktncen cen tate yabstocsesonsckingpobiens ELEY 2
"eSleaspnprocendissvedin tain chapters 00> saqacd eampeathowts
‘Drepoblens though Bator ese.
{pen singout GU applications, youcan use thea) metho, which organises as
phony cel asyou requ in your window using a cootinate system Notehou the nombers
Ur fomasointhetop le comer athe window
soysl, colwm=d | Fowl, columci | row, column?
‘ova, columns) | Fowe2, colume1 | row2, columns?
iujspossibeto further range thine widgets by grouping them infames
5.03 Other Tkinter Widgets You Can Use in Your Applications
Below rea few other useful widget examples you might wantto include in your
opiatons. Tze code nipetszhauld alle aced ater window = Tk() and above
‘rindow,mainioop() 35inccated by thecommentinthefollowing cle for an empty
lhster window
Frou tktnter isport *
window = Te()
Window title ("My AppLication’)
—— ea
window. mainioop()
Austentboswithalabe
abel window, texts ‘Mane: ) grid {zowe0, coluane0}
ny text box.gridirow-0, colume1)
otis.
Exanet ~ Frano (window, height=20, widthei00,bge'green')
franet grid(row=0, colum=0)
‘frane? = Frane(window, height=20, width=100,bge"red!)
‘frame? gria(zow-1, colum=1)
Adon down mens.
options = (2,2,3)
Imyvariabie object = IntVar() ¥ access ene vatus wien get)
Im variable object set |'chocse:*)
Rtopdow = optionsens (window, my_variable_ebject, voptions)
ropdom.gria()
ine prong sophia npleneratonsof asst ne chapters ese
‘sites ccs wes okTask 1 -Tkinter Widgets
Peace otangaodecateframtteenpy nodonrecpecn pages. sathis
Sa lcci wg nest tine s#eR0W E09
erry sbutou seit dorgonting eae
Gender GUI Application
a tel shodenensntenow oxo hevaeseecteantherage
Sara astute Sectngvar() object ard dpaythe crlce rade Fine.02
SF
we )
ven tating applcatins ged prac separate ror he des To
Fer rag nkorso tncorsttheto oer serotand et
Meayeurcucou te ttm oyu
eretsthe Python codethat demonsttes how to producea Gitar thisverysimpe one
function progam.
4 gender-gul-py
vom timer import +
Get change_text (Vt
mny_lnbel .config(textegender.get (0)
A Gor code goes here: 7
window = TEO
Vindow.citLe (My Application’)
1 widget and place it in a grid layout
4 naa an empty tkinter labe
‘ oa
‘Yilaver = Label window, widthe25, eight-1, texts
y_iabel grid(zowe0, colun=0)
tad a ekinter button widget, place tt in the grid Layout
$ tna serach the change text() function
fry button = Button (window, text="Supmit’
my button.grid(rowe1, column~O)
hrange_text)
viathe10, commané
f create 8 tkinter string variable object for the radio burtons
‘gender = stringvar()
PS two radio purcon wldgece
pif opticnat sticky asqunent co align lett
fasiot ~ Radiobutton window, vext-' Female!
fadiol.grid(row=2, colum=D, sticky=W)
Gesiot cclect () # pre-selects thir radio button for the use
Gisice ~ Radiobutton window, texta'¥ale', variable=gonder, value='male")
Ghai) geid(row-3, coluansD, seicky-i)
variablesgender, velue~'fonale')
insow.asntoap()
Fpire5.02 ACUI application wih tworado buttons
‘Task 2~ Drop-down Menu
Feunete odio baton applatonbutrepacetterado buttonswitha simple
Sopdownmen
Choosing a Text-based or GUI Application
om Chapter crwards answers to problems ae gen as iter text base appliations oF
Jappcatonsand oten bath,
extbased programs ofthe beneit of mae accurately electing the prograningstle of
hesylabus andl help to prepareyeu forthe Level sylabus Tex-basee applications do
tia the ational cormleny of having to reference hne's GUI widgets
loner GUl applications wil oe acne sual experienc and poducesystemssimlarto
hose commercial aval
Issigeested that you make use of bth typescf application. This wiles support ine
Bevslopment of your crmputationl thinking A text based ansnerisalnaysprovided
rcélers presented inti book in the final chapter, Chaper 4 Slutors fa GUL
ese solution makes sense, hese are aso provided inthe code repostary found atthe
panjonwebstetothistextooakat caning TaLns8i0828Geapter 6:
Selection
SF tHen. ase. eiser
NESTED Fstatement=
BB se...of..oThexmse...2
owt use lia operator when