Python for Kids: A Playful Introduction to Programming, 2nd Edition by Jason R. Briggs

Python for Kids: A Playful Introduction to Programming, 2nd Edition by Jason R. Briggs

Author:Jason R. Briggs
Language: eng
Format: epub
Publisher: No Starch Press, Inc.
Published: 2023-08-15T00:00:00+00:00


More Ways to Use the Identifier

Whenever we use a create_function from the canvas, such as create_polygon or create_rectangle , an identifier is returned. This identifying number can be used with other canvas functions, as we did earlier with the move function:

>>> from tkinter import * >>> tk = Tk() >>> canvas = Canvas(tk, width=400, height=400) >>> canvas.pack() >>> canvas.create_polygon(10, 10, 10, 60, 50, 35) 1 >>> canvas.move(1, 5, 0)

The problem with this example is that create_polygon won’t always return 1. For example, if you’ve created other shapes, it might return 2, 3, or even 100 (depending on the number of shapes you’ve created). If we change the code to store the value returned as a variable, and then use the variable (rather than just referring to the number 1), the code will work no matter what number is returned.

>>> mytriangle = canvas.create_polygon(10, 10, 10, 60, 50, 35) >>> canvas.move(mytriangle, 10, 0)

The move function allows us to move objects around the screen by using their identifier. But other canvas functions can also change something we’ve drawn. For example, the itemconfig function can change some parameters of a shape, such as its fill and outline colors.

Say we create a red triangle:

>>> from tkinter import * >>> tk = Tk() >>> canvas = Canvas(tk, width=400, height=400) >>> canvas.pack() >>> mytriangle = canvas.create_polygon(10, 10, 10, 60, 50, 35, fill='red')

We can change the triangle to another color with itemconfig and use the identifier as the first parameter. The following code says, “Change the fill color of the object identified by the number in variable mytriangle to blue”:

>>> canvas.itemconfig(mytriangle, fill='blue')

We could also give the triangle a different-colored outline, again using the identifier as the first parameter:

>>> canvas.itemconfig(mytriangle, outline='red')

Later, we’ll learn how to make other changes to a drawing, like hiding it and making it visible again. You’ll find the ability to change your drawings useful when we start writing games in the next chapter.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Popular ebooks
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(55550)
What's Done in Darkness by Kayla Perrin(26297)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(18822)
Shot Through the Heart by Mercy Celeste(18701)
The Fifty Shades Trilogy & Grey by E L James(18621)
The Subtle Art of Not Giving a F*ck by Mark Manson(13932)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(13870)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(12853)
Scorched Earth by Nick Kyme(12523)
Drei Generationen auf dem Jakobsweg by Stein Pia(10754)
Suna by Ziefle Pia(10687)
Scythe by Neal Shusterman(10043)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9302)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9114)
This is Going to Hurt by Adam Kay(8765)
Dirty Filthy Fix: A Fixed Trilogy Novella by Laurelin Paige(7393)
How to Make Love to a Negro Without Getting Tired by Dany LaFerrière(6786)
He Loves Me...KNOT by RC Boldt(6672)
Unleashing the Power of UX Analytics: Proven techniques and strategies for uncovering user insights [Team-IRA] [True PDF] by Jeff Hendrickson(6453)
Interdimensional Brothel by F4U(6114)