0% found this document useful (0 votes)
26 views

Tkinter GUI Lesson4

Lesson 4 of my PDF written tutorials. Building GUI's in Python - using Tkinter - Python's built in toolkit for GUI development

Uploaded by

Ben Woodfield
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Tkinter GUI Lesson4

Lesson 4 of my PDF written tutorials. Building GUI's in Python - using Tkinter - Python's built in toolkit for GUI development

Uploaded by

Ben Woodfield
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

BenWoodfieldraserppsprograms@gmail.

comTkinterGUIBasics

Example4:TkinterGUIWindow/TextEntry
Inthisexample,wewillseehowtoaddatextentryboxintoourGUIwindow.Asidefromafewsmallvisual
detailseverythingelseisthesame.Thedetailsareuptoyou,Irecommendchangingthecolours,orfontsizes
andvalueswhrereyouwantto.Learnhowtosetupawindowthatsuitswhatyouwantittobeusedfor.
InthisPythonscriptThereisatextentrybox.Itallowstheusertotypeanytextintoit,butwehavegonea
stepfurther.Thetextentryboxhasabuttonlinkedtoit,andwhenthebuttonispressedthewrittentextgets
printedintheShellwindow.
Heresthenewcode:
from tkinter import *
top = Tk()
top.title('Tkinter GUI - Example 4')
top.minsize(400, 400)
top.configure(bg='orange')
lbl = Label(top, text='Assigning Functions To Buttons', bg='yellow', fg='blue', font='14')
lbl.pack()
def hello_callback(): print('Button Clicked')
def callback():
print(ent.get())
btn = Button(top, text='Click Here', font='freesansbold, 14', command=hello_callback)
btn.pack()
exit_button=Button(top, text='Exit', fg='red', bg='black', font='freesansbold, 14', command=quit)
exit_button.pack(side=BOTTOM)
ent = Entry(top)
ent.pack()
btn2 = Button(top, text='Print Text', fg='blue', font='bold, 14', width=15, command=callback)
btn2.pack()
top.mainloop()

Sowehaveaddedanewfunction:

def callback()

Thenewfunctionhasalinereading print(ent.get()) WhichtellsPythonto'Get'thestringthatis


enteredintheentrybox,and'print'tellsPythontosendittotheShellasaPrintedmessage.Bymakingita
functionlikethis,wecanassignitasacommandtoabutton(ourclickherebutton)
Againafewvisualdetailshavemayhavebeenalteredbutbynowyoushouldbeabletochangethese
yourselfasit'snothingwehaven'tcoveredalready.
Inthenextexamplewewillrewriteanexistingprogramofminethattakesuserinput,performsacalculation
withit,thengivesitbacktotheuserbydisplayingitintheGUIItisaFeettoMetersconverterapp/
program.

You might also like