Write a programElementary20 XPExercise 2/8

A colour-changing square

Task

Make a program with a square on the canvas that changes colour: at least once a second it gets a new, random colour (within a few seconds at least 3 different colours should show).

Use a timer - either a function that reschedules itself with canvas.after(ms, funkcia), or a loop with canvas.update() and canvas.after(ms).

The program runs in a tkinter simulation right in your browser - after Run you see the window and can click it. The tests click and check what was drawn on the canvas.

Examples

  • Call

    from tkinter import _mente as okno
    
    okno.wait(5000)
    stvorce = [r for r in okno.created("rectangle")
               if abs(abs(r.coords[2] - r.coords[0]) - abs(r.coords[3] - r.coords[1])) < 1 and abs(r.coords[2] - r.coords[0]) >= 10]
    okno.check("na platne je stvorec", len(stvorce) > 0)
    zmeny = [(t, farba) for t, farba in okno.fills_over_time() if farba]
    farby = {farba for t, farba in zmeny}
    casy = sorted({t for t, farba in zmeny})
    okno.check("stvorec meni farby", len(farby) >= 3, f"roznych farieb: {len(farby)}")
    okno.check("farby sa menia postupne", len(casy) >= 3 and casy[-1] - casy[0] >= 1000, f"casy zmien: {casy[:4]}")

    Expected output

    na platne je stvorec: OK
    stvorec meni farby: OK
    farby sa menia postupne: OK

Python starts the first time you run code.

Ctrl+Enter runs the program. Tab indents. Press Esc, then Tab, to leave the editor.

Input (stdin)

Each line = one call to input().

Output

Hints

Try it yourself first. Each hint reveals a little more - and lowers the reward a little.

Signed out, hints are free - but no XP is awarded either.

Solution

The solution unlocks after the last hint.