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
Eco-friendly approach of bio-indigo synthesis and developing purification methods towards isolation of indigo from indirubin and bacterial fragments by Ramalingam Manivannan & Kaliyan Prabakaran & Young-A Son(205732)
Personalized inhaled bacteriophage therapy for treatment of multidrug-resistant Pseudomonas aeruginosa in cystic fibrosis by unknow(174176)
CONSORT 2025 statement: updated guideline for reporting randomized trials by unknow(82627)
Critical evaluation of the ProfiLER-02 study design and outcomes by Vivek Subbiah & Razelle Kurzrock(82228)
Cardiac gene therapy makes a comeback by Oliver J. Müller & Susanne Hille & Anca Kliesow Remes(82073)
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(74433)
Unveiling the design rules for tunable emission in graphene quantum dots: A high-throughput TDDFT and machine learning perspective by Şener Özönder & Mustafa Coşkun Özdemir & Caner Ünlü(50889)
A yeast-based oral therapeutic delivers immune checkpoint inhibitors to reduce intestinal tumor burden by unknow(40258)
Covalent hitchhikers guide proteins to the nucleus by Alexander F. Russell & Madeline F. Currie & Champak Chatterjee(40215)
Meet the Authors: Christopher R. Mansfield and Emily R. Derbyshire by Christopher R. Mansfield & Emily R. Derbyshire(40092)
Alkaline-earth metals promote propane dehydrogenation with carbon dioxide through geometric effects: Altering the reaction pathway by unknow(32729)
Induced iron vacancies boosting FeOOH loaded on sustainable Fenton-like collagen fiber membrane for efficient removal of emerging contaminants by unknow(32504)
Efficient electric-field-assisted photochemical conversion of methane to n-propanol exclusively over penetrated TiO2Ti hollow fibers by Guanghui Feng(32452)
Bi2SiO5 nanosheets as piezo-photocatalyst for efficient degradation of 2,4-Dichlorophenol by Hangyu Shi & Yifu Li & Lishan Zhang & Guoguan Liu & Qian Zhang & Xuan Ru & Shan Zhong(32383)
A novel NDIPTA organic heterojunction photocatalyst with built-in electric field for efficient hydrogen production by Jiahui Yang & Baojun Ma & Yongfa Zhu(32360)
Enhanced conversion of methane to liquid-phase oxygenates via hollow ferrite nanotube@horseradish peroxidase based photoenzymatic catalysis by Jun Duan & Shiying Fan & Xinyong Li & Shaomin Liu(32330)
Ordered macroporous superstructure of defective carbon adorned with tiny cobalt sulfide for selective electrocatalytic hydrogenation of cinnamaldehyde by Xiao-Shi Yuan & Sheng-Hua Zhou & San-Mei Wang & Wenbo Wei & Xiaofang Li & Xin-Tao Wu & Qi-Long Zhu(32256)
What's Done in Darkness by Kayla Perrin(27144)
Topological analysis of non-conjugated ethylene oxide cored dendrimers decorated with tetraphenylethylene: Insights from degree-based descriptors using the polynomial approach by A Theertha Nair & D Antony Xavier & Annmaria Baby & S Akhila(26521)
Investigation of mechanical and self-healing properties of hydroxyl-terminated polybutadiene functionalized with 2-ureido-4-pyrimidinone by Mohsen Kazazi & Mehran Hayaty & Ali Mousaviazar(26457)