SlideShare une entreprise Scribd logo
1  sur  10
INTRODUCTION TO TKINTER
• UP TO THIS POINT WE HAVE BEEN CREATING CONSOLE BASED APPLICATIONS.
• NOW WE ARE GOING TO COMBINE THAT KNOWLEDGE WITH THE OBJECTS WE
STUDIED IN APPINVENTOR.
• WE WILL BE USING THE MODULE TKINTER
• THE TKINTER MODULE IS COMPRISED OF NUMEROUS CLASSES TO CREATE GUI
WIDGETS.
• CODE: FROM TKINTER IMPORT *
CREATING A GRAPHICAL ENVIRONMENT
• THERE ARE 2 MAIN COMPONENTS TO A GUI.
• 1. THE WINDOW/CONTAINER
• 2. THE WIDGETS
• LABELS, BUTTONS, TEXT BOXES, RADIO BUTTONS, IMAGES, ETC.
CREATING A WINDOW AND LABEL
• LET’S BEGIN BY CREATING THE WINDOW, WHICH IS NOTHING MORE THAN A
CONTAINER FOR OUR WIDGETS.
• THE EQUIVALENT TO A SCREEN IN APPINVENTOR
• CODE: WINDOWNAME = TK()
• HOWEVER, THIS WINDOW WILL NOT DO MUCH FOR US UNLESS WE PACK IT WITH
SOME CONTENT.
CREATING A LABEL
• A LABEL IS A WIDGET THAT DISPLAYS INFORMATION. JUST LIKE IN APPINVENTOR.
• CODE: LABELNAME = LABEL(WINDOWNAME, OPTIONS,…)
• OPTIONS INCLUDE:
• TEXT, IMAGES, COLORS, FONT, JUSTIFY, ETC…
• EX.
LBLPROMPT = LABEL(WIND, TEXT =“ HEY”, BG = “RED”)
• AFTER YOU CREATE THE LABEL YOU HAVE TO PUT/PACK IT IN THE WINDOW.
• CODE: LABELNAME.PACK()
• NOTE: THERE ARE NUMEROUS OPTIONS TO EXPLORE
THE EVENT LOOP AND PROCESSING EVENTS
• GUI’S ARE EVENT DRIVEN, MEANING THEY RUN WHEN SOME INTERACTION FROM THE
USER OCCURS.
• TO DETERMINE WHEN AN INTERACTION OCCURS YOU HAVE TO CREATE A LISTENER
FOR EVERY PROGRAMMABLE ACTION THERE IS.
• PYTHON MAKES THIS A BIT EASIER BY USING AN EVENT LOOP TO LISTEN FOR ANY
INTERACTION.
• CODE: WINDOWNAME.MAINLOOP()
EXAMPLE
• QUESTION: HOW CAN WE MODIFY THE BELOW PROGRAM TO PROMPT THE USER TO
ENTER A NAME AND THEN DISPLAY A WELCOME MESSAGE.
IMPORT TKINTER
WIND = TK()
LBLNAME = LABEL(WIND, TEXT = “HEY YOU!”, FG = “BLUE”, FONT =(“HELVETICA, 16))
LBLNAME.PACK()
WIND.MAINLOOP()
THE WIDGET CLASSES
• WHEN CREATING A WIDGET THERE ARE
SPECIFIC PARAMETERS FOR EACH TO SPECIFY.
• WHERE TO PLACE IT
• PROPERTIES
• VARIABLES TO SET, ETC.
Widget Class Description
Button A simple button, used to execute a command.
Checkbutton Clicking a check button toggles between the values.
Radiobutton Clicking a radio button sets the variable to that value,
and clears all other radio buttons associated with the
same variable.
Entry A text entry field, a.k.a a text field or a text box.
Frame A container widget for containing other widgets.
Menu A menu pane, used to implement pull down and popup menus.
Menubutton A menu button, used to implement pull down menus.
Label Displays a text or an image.
Message Displays a text. Similar to the label widget, but can
automatically wrap text to a given width or aspect ratio.
Text Formatted text display. Allows you to display and edit
text with various styles and attributes. Also supports
embedded images and windows.
Scale Allows you to set a numerical value by dragging a
"slider".
Canvas Structured graphics, used to draw graphs and plots,
create graphics editors, and to implement custom widgets.
Toplevel A container widget displayed as a separate, top-level
window.
CHANGING PROPERTIES OF WIDGETS
• COLOR AND FONT
• TO SPECIFY A COLOR, YOU CAN EITHER USE A COLOR NAME SUCH AS RED, YELLOW, GREEN, BLUE, WHITE, BLACK, PURPLE, ETC, OR EXPLICITLY SPECIFY THE RED, GREEN, AND BLUE
(RGB) COLOR COMPONENTS USING A STRING #RRGGBB, WHERE RR, GG, BB ARE HEXADECIMAL REPRESENTATIONS OF THE RED, GREEN AND BLUE VALUES, RESPECTIVELY.
• "TIMES 10 BOLD"
• "HELVETICA 10 BOLD ITALIC"
• "COURIER NEW 20 BOLD ITALIC"
• "COURIER NEW 20 BOLD ITALIC OVER STRIKE UNDERLINE"
• TEXT PROPERTIES
• THE TEXT IN A LABEL AND A BUTTON IS CENTERED
BY DEFAULT. YOU CAN CHANGE IT BY USING THE JUSTIFY
OPTION WITH VALUES LEFT, CENTER, OR RIGHT. YOU
CAN ALSO DISPLAY THE TEXT IN MULTIPLE LINES BY
INSERTING THE NEWLINE CHARACTER N TO SEPARATE TEXTS.
• MOUSE CURSOR
• YOU CAN SET A MOUSE CURSOR BY USING THE CURSOR OPTION WITH VALUES SUCH AS "ARROW" (DEFAULT), "CIRCLE", "CROSS" "PLUS", ETC.
• CODE: WIDGETNAME["PROPERTYNAME] = NEWPROPERTYVALUE
EXAMPLE
import tkinter
def btnNameProcess(name):
lblName["text"]= "Hey " + name
wind = tkinter.Tk()
wind.title("GUI Test Program")
wind.minsize(200,200)
lblName = tkinter.Label(wind, text ="Hey You", fg ="blue", bg = "#fff")
enterName = tkinter.StringVar() entName = tkinter.Entry(wind,text = "Me")
btnName = tkinter.Button(wind, text = "Press Me", command = lambda: btnNameProcess(entName.get()))
lblName.pack()
entName.pack()
btnName.pack()
wind.mainloop()

Contenu connexe

Similaire à Introduction to Graphics

CodeStock :: Introduction To MacRuby and HotCocoa
CodeStock :: Introduction To MacRuby and HotCocoaCodeStock :: Introduction To MacRuby and HotCocoa
CodeStock :: Introduction To MacRuby and HotCocoaDoc Norton
 
Visual Basic IDE Introduction
Visual Basic IDE IntroductionVisual Basic IDE Introduction
Visual Basic IDE IntroductionAhllen Javier
 
Visual Basic IDE Intro.pdf
Visual Basic IDE Intro.pdfVisual Basic IDE Intro.pdf
Visual Basic IDE Intro.pdfsheenmarie0212
 
Cassandra and materialized views
Cassandra and materialized viewsCassandra and materialized views
Cassandra and materialized viewsGrzegorz Duda
 
Algorithm Design and Problem Solving [Autosaved].pptx
Algorithm Design and Problem Solving [Autosaved].pptxAlgorithm Design and Problem Solving [Autosaved].pptx
Algorithm Design and Problem Solving [Autosaved].pptxKaavyaGaur1
 
Feature Engineering
Feature EngineeringFeature Engineering
Feature EngineeringHJ van Veen
 
Oh the compilers you'll build
Oh the compilers you'll buildOh the compilers you'll build
Oh the compilers you'll buildMark Stoodley
 
Unreal Engine Basics 05 - User Interface
Unreal Engine Basics 05 - User InterfaceUnreal Engine Basics 05 - User Interface
Unreal Engine Basics 05 - User InterfaceNick Pruehs
 
autocadppt.pptx
autocadppt.pptxautocadppt.pptx
autocadppt.pptxNannuJoshi
 
microbit Microbit programming Microbit programming
microbit Microbit programming Microbit programmingmicrobit Microbit programming Microbit programming
microbit Microbit programming Microbit programmingssuser5feb2c1
 
Digital Calipers Software Mitutoyo / Logiciel pour Pied à coulisse Mitutoyo (...
Digital Calipers Software Mitutoyo / Logiciel pour Pied à coulisse Mitutoyo (...Digital Calipers Software Mitutoyo / Logiciel pour Pied à coulisse Mitutoyo (...
Digital Calipers Software Mitutoyo / Logiciel pour Pied à coulisse Mitutoyo (...topomax
 
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...Having fun power apps components HandsOn - Power Platform World Tour Copenhag...
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...Rebekka Aalbers-de Jong
 
iBeacons - the new low-powered way of location awareness
iBeacons - the new low-powered way of location awarenessiBeacons - the new low-powered way of location awareness
iBeacons - the new low-powered way of location awarenessStefano Zanetti
 
Windows Forms For Beginners Part - 1
Windows Forms For Beginners Part - 1Windows Forms For Beginners Part - 1
Windows Forms For Beginners Part - 1Bhushan Mulmule
 

Similaire à Introduction to Graphics (20)

CodeStock :: Introduction To MacRuby and HotCocoa
CodeStock :: Introduction To MacRuby and HotCocoaCodeStock :: Introduction To MacRuby and HotCocoa
CodeStock :: Introduction To MacRuby and HotCocoa
 
Visual Basic IDE Introduction
Visual Basic IDE IntroductionVisual Basic IDE Introduction
Visual Basic IDE Introduction
 
Visual Basic IDE Intro.pdf
Visual Basic IDE Intro.pdfVisual Basic IDE Intro.pdf
Visual Basic IDE Intro.pdf
 
Cassandra and materialized views
Cassandra and materialized viewsCassandra and materialized views
Cassandra and materialized views
 
Algorithm Design and Problem Solving [Autosaved].pptx
Algorithm Design and Problem Solving [Autosaved].pptxAlgorithm Design and Problem Solving [Autosaved].pptx
Algorithm Design and Problem Solving [Autosaved].pptx
 
Visual basic
Visual basicVisual basic
Visual basic
 
QGIS Tutorial 1
QGIS Tutorial 1QGIS Tutorial 1
QGIS Tutorial 1
 
Visual Basic.pptx
Visual Basic.pptxVisual Basic.pptx
Visual Basic.pptx
 
Feature Engineering
Feature EngineeringFeature Engineering
Feature Engineering
 
Oh the compilers you'll build
Oh the compilers you'll buildOh the compilers you'll build
Oh the compilers you'll build
 
Unreal Engine Basics 05 - User Interface
Unreal Engine Basics 05 - User InterfaceUnreal Engine Basics 05 - User Interface
Unreal Engine Basics 05 - User Interface
 
autocadppt.pptx
autocadppt.pptxautocadppt.pptx
autocadppt.pptx
 
microbit Microbit programming Microbit programming
microbit Microbit programming Microbit programmingmicrobit Microbit programming Microbit programming
microbit Microbit programming Microbit programming
 
Adobe pagemaker
Adobe pagemakerAdobe pagemaker
Adobe pagemaker
 
Digital Calipers Software Mitutoyo / Logiciel pour Pied à coulisse Mitutoyo (...
Digital Calipers Software Mitutoyo / Logiciel pour Pied à coulisse Mitutoyo (...Digital Calipers Software Mitutoyo / Logiciel pour Pied à coulisse Mitutoyo (...
Digital Calipers Software Mitutoyo / Logiciel pour Pied à coulisse Mitutoyo (...
 
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...Having fun power apps components HandsOn - Power Platform World Tour Copenhag...
Having fun power apps components HandsOn - Power Platform World Tour Copenhag...
 
iBeacons - the new low-powered way of location awareness
iBeacons - the new low-powered way of location awarenessiBeacons - the new low-powered way of location awareness
iBeacons - the new low-powered way of location awareness
 
Windows Forms For Beginners Part - 1
Windows Forms For Beginners Part - 1Windows Forms For Beginners Part - 1
Windows Forms For Beginners Part - 1
 
Week 1
Week 1Week 1
Week 1
 
Windows mobile programming
Windows mobile programmingWindows mobile programming
Windows mobile programming
 

Plus de primeteacher32

Plus de primeteacher32 (20)

Software Development Life Cycle
Software Development Life CycleSoftware Development Life Cycle
Software Development Life Cycle
 
Variable Scope
Variable ScopeVariable Scope
Variable Scope
 
Returning Data
Returning DataReturning Data
Returning Data
 
Intro to Functions
Intro to FunctionsIntro to Functions
Intro to Functions
 
Introduction to GUIs with guizero
Introduction to GUIs with guizeroIntroduction to GUIs with guizero
Introduction to GUIs with guizero
 
Function Parameters
Function ParametersFunction Parameters
Function Parameters
 
Nested Loops
Nested LoopsNested Loops
Nested Loops
 
Conditional Loops
Conditional LoopsConditional Loops
Conditional Loops
 
Introduction to Repetition Structures
Introduction to Repetition StructuresIntroduction to Repetition Structures
Introduction to Repetition Structures
 
Input Validation
Input ValidationInput Validation
Input Validation
 
Windows File Systems
Windows File SystemsWindows File Systems
Windows File Systems
 
Nesting Conditionals
Nesting ConditionalsNesting Conditionals
Nesting Conditionals
 
Conditionals
ConditionalsConditionals
Conditionals
 
Intro to Python with GPIO
Intro to Python with GPIOIntro to Python with GPIO
Intro to Python with GPIO
 
Variables and Statements
Variables and StatementsVariables and Statements
Variables and Statements
 
Variables and User Input
Variables and User InputVariables and User Input
Variables and User Input
 
Intro to Python
Intro to PythonIntro to Python
Intro to Python
 
Raspberry Pi
Raspberry PiRaspberry Pi
Raspberry Pi
 
Hardware vs. Software Presentations
Hardware vs. Software PresentationsHardware vs. Software Presentations
Hardware vs. Software Presentations
 
Block chain security
Block chain securityBlock chain security
Block chain security
 

Dernier

Training for Deaconess, biblical qualifications.ppt
Training for Deaconess, biblical qualifications.pptTraining for Deaconess, biblical qualifications.ppt
Training for Deaconess, biblical qualifications.pptVidalMendoza5
 
Design, Desire and Demand Presentation.pptx
Design, Desire and Demand Presentation.pptxDesign, Desire and Demand Presentation.pptx
Design, Desire and Demand Presentation.pptxaaronbasko1
 
Abanoub Ghobrial, Planning Team Leader.pdf
Abanoub Ghobrial, Planning Team Leader.pdfAbanoub Ghobrial, Planning Team Leader.pdf
Abanoub Ghobrial, Planning Team Leader.pdfAbanoubGhobrial1
 
Human Rights are notes and helping material
Human Rights are notes and helping materialHuman Rights are notes and helping material
Human Rights are notes and helping materialnadeemcollege26
 
加拿大MUN学位证,纽芬兰纪念大学毕业证书1:1制作
加拿大MUN学位证,纽芬兰纪念大学毕业证书1:1制作加拿大MUN学位证,纽芬兰纪念大学毕业证书1:1制作
加拿大MUN学位证,纽芬兰纪念大学毕业证书1:1制作rpb5qxou
 
Jumark Morit Diezmo- Career portfolio- BPED 3A
Jumark Morit Diezmo- Career portfolio- BPED 3AJumark Morit Diezmo- Career portfolio- BPED 3A
Jumark Morit Diezmo- Career portfolio- BPED 3Ajumarkdiezmo1
 
Abanoub Ghobrial, Planning Team Leader.pdf
Abanoub Ghobrial, Planning Team Leader.pdfAbanoub Ghobrial, Planning Team Leader.pdf
Abanoub Ghobrial, Planning Team Leader.pdfAbanoubGhobrial1
 
LinkedIn for Your Job Search in April 2024
LinkedIn for Your Job Search in April 2024LinkedIn for Your Job Search in April 2024
LinkedIn for Your Job Search in April 2024Bruce Bennett
 
Bobby singh - Digital Marketing Service
Bobby singh -  Digital Marketing ServiceBobby singh -  Digital Marketing Service
Bobby singh - Digital Marketing ServiceBobby singh
 
The Next Things To Immediately Do About Mating Press
The Next Things To Immediately Do About Mating PressThe Next Things To Immediately Do About Mating Press
The Next Things To Immediately Do About Mating Pressmatingpress170
 
Complete Benefits of career counseling in India
Complete Benefits of career counseling in IndiaComplete Benefits of career counseling in India
Complete Benefits of career counseling in IndiaMere Mentor
 
Thomas Calculus 12th Edition Textbook and helping material
Thomas Calculus 12th Edition Textbook and helping materialThomas Calculus 12th Edition Textbook and helping material
Thomas Calculus 12th Edition Textbook and helping materialsafdarhussainbhutta4
 
怎么办理美国UCLA毕业证加州大学洛杉矶分校学位证书一手渠道
怎么办理美国UCLA毕业证加州大学洛杉矶分校学位证书一手渠道怎么办理美国UCLA毕业证加州大学洛杉矶分校学位证书一手渠道
怎么办理美国UCLA毕业证加州大学洛杉矶分校学位证书一手渠道7283h7lh
 
401799841-Increasing-Crimes-and-Suicides-Among-Youth.pptx
401799841-Increasing-Crimes-and-Suicides-Among-Youth.pptx401799841-Increasing-Crimes-and-Suicides-Among-Youth.pptx
401799841-Increasing-Crimes-and-Suicides-Among-Youth.pptxwaghmare9860lavin
 
APSC Motor Vechile Inspector 18 Posts.pdf
APSC Motor Vechile Inspector 18 Posts.pdfAPSC Motor Vechile Inspector 18 Posts.pdf
APSC Motor Vechile Inspector 18 Posts.pdfsoumita869
 
Network to Success - Using Social Media in Job Search
Network to Success - Using Social Media in Job SearchNetwork to Success - Using Social Media in Job Search
Network to Success - Using Social Media in Job SearchBruce Bennett
 
How to prepare yourself for a job interview.pptx
How to prepare yourself for a job interview.pptxHow to prepare yourself for a job interview.pptx
How to prepare yourself for a job interview.pptxJohnreyFalsarioBasid
 
Nathan_Baughman_Resume_copywriter_and_editor
Nathan_Baughman_Resume_copywriter_and_editorNathan_Baughman_Resume_copywriter_and_editor
Nathan_Baughman_Resume_copywriter_and_editorNathanBaughman3
 
How To Land Your Next PM Dream Job - PMISSC Meeting - April 2024
How To Land Your Next PM Dream Job - PMISSC Meeting - April 2024How To Land Your Next PM Dream Job - PMISSC Meeting - April 2024
How To Land Your Next PM Dream Job - PMISSC Meeting - April 2024Hector Del Castillo, CPM, CPMM
 
Senior IT Professional with Master’s Degree with 21+ years of experience is...
Senior IT Professional with Master’s Degree with 21+ years of experience   is...Senior IT Professional with Master’s Degree with 21+ years of experience   is...
Senior IT Professional with Master’s Degree with 21+ years of experience is...Anas Acharath Parakat
 

Dernier (20)

Training for Deaconess, biblical qualifications.ppt
Training for Deaconess, biblical qualifications.pptTraining for Deaconess, biblical qualifications.ppt
Training for Deaconess, biblical qualifications.ppt
 
Design, Desire and Demand Presentation.pptx
Design, Desire and Demand Presentation.pptxDesign, Desire and Demand Presentation.pptx
Design, Desire and Demand Presentation.pptx
 
Abanoub Ghobrial, Planning Team Leader.pdf
Abanoub Ghobrial, Planning Team Leader.pdfAbanoub Ghobrial, Planning Team Leader.pdf
Abanoub Ghobrial, Planning Team Leader.pdf
 
Human Rights are notes and helping material
Human Rights are notes and helping materialHuman Rights are notes and helping material
Human Rights are notes and helping material
 
加拿大MUN学位证,纽芬兰纪念大学毕业证书1:1制作
加拿大MUN学位证,纽芬兰纪念大学毕业证书1:1制作加拿大MUN学位证,纽芬兰纪念大学毕业证书1:1制作
加拿大MUN学位证,纽芬兰纪念大学毕业证书1:1制作
 
Jumark Morit Diezmo- Career portfolio- BPED 3A
Jumark Morit Diezmo- Career portfolio- BPED 3AJumark Morit Diezmo- Career portfolio- BPED 3A
Jumark Morit Diezmo- Career portfolio- BPED 3A
 
Abanoub Ghobrial, Planning Team Leader.pdf
Abanoub Ghobrial, Planning Team Leader.pdfAbanoub Ghobrial, Planning Team Leader.pdf
Abanoub Ghobrial, Planning Team Leader.pdf
 
LinkedIn for Your Job Search in April 2024
LinkedIn for Your Job Search in April 2024LinkedIn for Your Job Search in April 2024
LinkedIn for Your Job Search in April 2024
 
Bobby singh - Digital Marketing Service
Bobby singh -  Digital Marketing ServiceBobby singh -  Digital Marketing Service
Bobby singh - Digital Marketing Service
 
The Next Things To Immediately Do About Mating Press
The Next Things To Immediately Do About Mating PressThe Next Things To Immediately Do About Mating Press
The Next Things To Immediately Do About Mating Press
 
Complete Benefits of career counseling in India
Complete Benefits of career counseling in IndiaComplete Benefits of career counseling in India
Complete Benefits of career counseling in India
 
Thomas Calculus 12th Edition Textbook and helping material
Thomas Calculus 12th Edition Textbook and helping materialThomas Calculus 12th Edition Textbook and helping material
Thomas Calculus 12th Edition Textbook and helping material
 
怎么办理美国UCLA毕业证加州大学洛杉矶分校学位证书一手渠道
怎么办理美国UCLA毕业证加州大学洛杉矶分校学位证书一手渠道怎么办理美国UCLA毕业证加州大学洛杉矶分校学位证书一手渠道
怎么办理美国UCLA毕业证加州大学洛杉矶分校学位证书一手渠道
 
401799841-Increasing-Crimes-and-Suicides-Among-Youth.pptx
401799841-Increasing-Crimes-and-Suicides-Among-Youth.pptx401799841-Increasing-Crimes-and-Suicides-Among-Youth.pptx
401799841-Increasing-Crimes-and-Suicides-Among-Youth.pptx
 
APSC Motor Vechile Inspector 18 Posts.pdf
APSC Motor Vechile Inspector 18 Posts.pdfAPSC Motor Vechile Inspector 18 Posts.pdf
APSC Motor Vechile Inspector 18 Posts.pdf
 
Network to Success - Using Social Media in Job Search
Network to Success - Using Social Media in Job SearchNetwork to Success - Using Social Media in Job Search
Network to Success - Using Social Media in Job Search
 
How to prepare yourself for a job interview.pptx
How to prepare yourself for a job interview.pptxHow to prepare yourself for a job interview.pptx
How to prepare yourself for a job interview.pptx
 
Nathan_Baughman_Resume_copywriter_and_editor
Nathan_Baughman_Resume_copywriter_and_editorNathan_Baughman_Resume_copywriter_and_editor
Nathan_Baughman_Resume_copywriter_and_editor
 
How To Land Your Next PM Dream Job - PMISSC Meeting - April 2024
How To Land Your Next PM Dream Job - PMISSC Meeting - April 2024How To Land Your Next PM Dream Job - PMISSC Meeting - April 2024
How To Land Your Next PM Dream Job - PMISSC Meeting - April 2024
 
Senior IT Professional with Master’s Degree with 21+ years of experience is...
Senior IT Professional with Master’s Degree with 21+ years of experience   is...Senior IT Professional with Master’s Degree with 21+ years of experience   is...
Senior IT Professional with Master’s Degree with 21+ years of experience is...
 

Introduction to Graphics

  • 1.
  • 2. INTRODUCTION TO TKINTER • UP TO THIS POINT WE HAVE BEEN CREATING CONSOLE BASED APPLICATIONS. • NOW WE ARE GOING TO COMBINE THAT KNOWLEDGE WITH THE OBJECTS WE STUDIED IN APPINVENTOR. • WE WILL BE USING THE MODULE TKINTER • THE TKINTER MODULE IS COMPRISED OF NUMEROUS CLASSES TO CREATE GUI WIDGETS. • CODE: FROM TKINTER IMPORT *
  • 3. CREATING A GRAPHICAL ENVIRONMENT • THERE ARE 2 MAIN COMPONENTS TO A GUI. • 1. THE WINDOW/CONTAINER • 2. THE WIDGETS • LABELS, BUTTONS, TEXT BOXES, RADIO BUTTONS, IMAGES, ETC.
  • 4. CREATING A WINDOW AND LABEL • LET’S BEGIN BY CREATING THE WINDOW, WHICH IS NOTHING MORE THAN A CONTAINER FOR OUR WIDGETS. • THE EQUIVALENT TO A SCREEN IN APPINVENTOR • CODE: WINDOWNAME = TK() • HOWEVER, THIS WINDOW WILL NOT DO MUCH FOR US UNLESS WE PACK IT WITH SOME CONTENT.
  • 5. CREATING A LABEL • A LABEL IS A WIDGET THAT DISPLAYS INFORMATION. JUST LIKE IN APPINVENTOR. • CODE: LABELNAME = LABEL(WINDOWNAME, OPTIONS,…) • OPTIONS INCLUDE: • TEXT, IMAGES, COLORS, FONT, JUSTIFY, ETC… • EX. LBLPROMPT = LABEL(WIND, TEXT =“ HEY”, BG = “RED”) • AFTER YOU CREATE THE LABEL YOU HAVE TO PUT/PACK IT IN THE WINDOW. • CODE: LABELNAME.PACK() • NOTE: THERE ARE NUMEROUS OPTIONS TO EXPLORE
  • 6. THE EVENT LOOP AND PROCESSING EVENTS • GUI’S ARE EVENT DRIVEN, MEANING THEY RUN WHEN SOME INTERACTION FROM THE USER OCCURS. • TO DETERMINE WHEN AN INTERACTION OCCURS YOU HAVE TO CREATE A LISTENER FOR EVERY PROGRAMMABLE ACTION THERE IS. • PYTHON MAKES THIS A BIT EASIER BY USING AN EVENT LOOP TO LISTEN FOR ANY INTERACTION. • CODE: WINDOWNAME.MAINLOOP()
  • 7. EXAMPLE • QUESTION: HOW CAN WE MODIFY THE BELOW PROGRAM TO PROMPT THE USER TO ENTER A NAME AND THEN DISPLAY A WELCOME MESSAGE. IMPORT TKINTER WIND = TK() LBLNAME = LABEL(WIND, TEXT = “HEY YOU!”, FG = “BLUE”, FONT =(“HELVETICA, 16)) LBLNAME.PACK() WIND.MAINLOOP()
  • 8. THE WIDGET CLASSES • WHEN CREATING A WIDGET THERE ARE SPECIFIC PARAMETERS FOR EACH TO SPECIFY. • WHERE TO PLACE IT • PROPERTIES • VARIABLES TO SET, ETC. Widget Class Description Button A simple button, used to execute a command. Checkbutton Clicking a check button toggles between the values. Radiobutton Clicking a radio button sets the variable to that value, and clears all other radio buttons associated with the same variable. Entry A text entry field, a.k.a a text field or a text box. Frame A container widget for containing other widgets. Menu A menu pane, used to implement pull down and popup menus. Menubutton A menu button, used to implement pull down menus. Label Displays a text or an image. Message Displays a text. Similar to the label widget, but can automatically wrap text to a given width or aspect ratio. Text Formatted text display. Allows you to display and edit text with various styles and attributes. Also supports embedded images and windows. Scale Allows you to set a numerical value by dragging a "slider". Canvas Structured graphics, used to draw graphs and plots, create graphics editors, and to implement custom widgets. Toplevel A container widget displayed as a separate, top-level window.
  • 9. CHANGING PROPERTIES OF WIDGETS • COLOR AND FONT • TO SPECIFY A COLOR, YOU CAN EITHER USE A COLOR NAME SUCH AS RED, YELLOW, GREEN, BLUE, WHITE, BLACK, PURPLE, ETC, OR EXPLICITLY SPECIFY THE RED, GREEN, AND BLUE (RGB) COLOR COMPONENTS USING A STRING #RRGGBB, WHERE RR, GG, BB ARE HEXADECIMAL REPRESENTATIONS OF THE RED, GREEN AND BLUE VALUES, RESPECTIVELY. • "TIMES 10 BOLD" • "HELVETICA 10 BOLD ITALIC" • "COURIER NEW 20 BOLD ITALIC" • "COURIER NEW 20 BOLD ITALIC OVER STRIKE UNDERLINE" • TEXT PROPERTIES • THE TEXT IN A LABEL AND A BUTTON IS CENTERED BY DEFAULT. YOU CAN CHANGE IT BY USING THE JUSTIFY OPTION WITH VALUES LEFT, CENTER, OR RIGHT. YOU CAN ALSO DISPLAY THE TEXT IN MULTIPLE LINES BY INSERTING THE NEWLINE CHARACTER N TO SEPARATE TEXTS. • MOUSE CURSOR • YOU CAN SET A MOUSE CURSOR BY USING THE CURSOR OPTION WITH VALUES SUCH AS "ARROW" (DEFAULT), "CIRCLE", "CROSS" "PLUS", ETC. • CODE: WIDGETNAME["PROPERTYNAME] = NEWPROPERTYVALUE
  • 10. EXAMPLE import tkinter def btnNameProcess(name): lblName["text"]= "Hey " + name wind = tkinter.Tk() wind.title("GUI Test Program") wind.minsize(200,200) lblName = tkinter.Label(wind, text ="Hey You", fg ="blue", bg = "#fff") enterName = tkinter.StringVar() entName = tkinter.Entry(wind,text = "Me") btnName = tkinter.Button(wind, text = "Press Me", command = lambda: btnNameProcess(entName.get())) lblName.pack() entName.pack() btnName.pack() wind.mainloop()