obrazky
-

Graphics again

  1. Write a program semafor.py that draws a traffic light with three lights using colored circles, as shown in the picture:

semafor

  1. Now we would like to improve the traffic light program so that either the red or the green light is on. Assign the number of seconds to the variable čas. If this number is less than 30, draw the red light; otherwise, draw the green light.

semafor

  1. Make a functioning traffic light, that switches from green through yellow to red and vice versa.

  2. Create a program that simulates a functional traffic intersection using AI.

Working with the mouse

  1. Create a new program mouse.py and copy the following code into it. Then run the program.

     import tkinter
     canvas = tkinter.Canvas()
     canvas.pack()
    
     def click(mouse):
         print(mouse.x, mouse.y)
    
     canvas.bind('<B1-Motion>', click)
    

A new command canvas.bind has been added. Thanks to it, the drawing area will now know what to do when you press the left mouse button over it and then move the mouse. Numbers will start appearing in the text window. What do these numbers mean? How must we change the program if we want it to react to pressing the middle or right mouse button?

  1. Instead of the print command in the previous function click, use the command canvas.create_text to draw the character ‘*’. Draw the character at the position where the mouse is. Change the size, font, and color of the printed sign.

  2. Modify the function click in the program mys.py so that it draws a colored circle. Choose the color using a conditional statement so that the program draws red circles to the left of x = 150 and green circles to the right. The radius of the circles will be 5. For example:

klik

  1. Add new lines of code to the previous program:

     import tkinter
     canvas = tkinter.Canvas()
     canvas.pack()
     def click(mouse):
     …
     …
     def erase(mouse):
         canvas.delete('all')
    
     canvas.bind('<B1-Motion>', click)
     canvas.bind('<ButtonPress-3>', erase)
    

Now we have added functionality to the program that is triggered by clicking the right mouse button. Try it out. We used canvas.delete(‘all’), which clears the drawing area.

  1. Let’s learn how to use a new command canvas.create_line(x1, y1, x2, y2). With this command, we can draw a line segment from point [x1, y1] to point [x2, y2]. Create a new program lines.py and copy the following code into it.

     import tkinter
     canvas = tkinter.Canvas()
     canvas.pack()
    
     def click(mouse):
         canvas.create_line(150, 100, mouse.x, mouse.y)
    
     def erase(mouse):
         canvas.delete('all')
    
     canvas.bind('<B1-Motion>', click)
     canvas.bind('<ButtonPress-3>', erase)
    

Set the color of the line to red.

  1. *Create a new program sprej.py, and copy the code from example 4 into it. The program will draw on the canvas to create a spray effect.

First, store the color you want the program to draw with in a variable, for example: color = ‘blue’

We will modify the function click:

  • First, randomly choose two numbers dx and dy from the interval ⟨-30, 30⟩ (using the command random.randint(-30, 30)).
  • This pair of numbers represents the shift of the drawn colored dot relative to the mouse position, i.e. [mys.x + dx, mys.y + dy].
  • At this shifted position, draw the character ‘+’ using the command canvas.create_text. Use the color from the variable color.
  • Repeat this procedure 50 times using a loop, which will draw 50 small ‘+’ characters.

sprej

-
Copyright © 2008-2026 Miroslava Valíková