Introduction to Python  
 
 

Lesson 7
Lesson 7: GUIs Graphics
Lesson Goals:
  • Properly add pictures
  • Change graphics based on user input
  • Drawing your own basic shapes

Readings:
Labels
PhotoImage
Canvas

Lesson Text:
In this lesson, you will continue to study the graphics of GUIs, in order to make them more aesthetically appealing and interesting to interact with. You will start by focusing on the Label class. It is very useful because it can display either text or graphics.

Exercise:
Create a window that contains two Labels and a Quit button. The first Label will be a .GIF picture of anything (feel free to download one from online, provided you have permission). Place the photo in the same directory as your code and specify the Label is to display that image. The next Label will be a text phrase describing what the picture shown in the Label is. Example: “It’s a kitty!!!”
There is a lot more that you can do with the Labels that involve user interaction. In this next exercise, you will focus on the removal and addition of Labels while the program is running based off of user input.
Exercise:
For this exercise, you will be creating a picture quiz for the user of the GUI. There will be two Labels, a text field, and a submit button. One Label will be a picture you have found online of some animal, please find at least five such pictures and please ensure that they are nothing obscene. The second Label should display a question, “What is this picture showing?” and both of these should be directly above a text field. The submit button should be next to or below the text field and should be labeled “Check Answer”. When it is pressed, you should check what has been typed into the field against the correct answer to the question, which should be stored somewhere in your code. If it is correct, then you should change the Label into the next picture, and its corresponding answer. It should run through all the pictures you have found online.
The Canvas class is another class in Tkinter that allows you to draw your own graphics. With canvas, you should be able to draw ovals, squares, lines, arcs, polygons and other primitive graphics shapes. The class has a multitude of built in functions that you will need to familiarize yourself with in order to properly place, color, and draw the objects you may need. This will be the focus of the next exercise.
Exercise:
Create a window that contains a 300x300 canvas. Add a text field and submit button beneath the canvas. Set up your text field so that the user may enter all the necessary information for drawing different shapes. When submit is clicked, the information in the text field should be used to draw whatever was specified. You will need to make use of much of what you have learned previously, particularly of “if-else” statements, to get this done properly. Add a Quit button if you wish.


Files to Be Downloaded