Riešenie úloh 10. cvičenia


  1. Riešenie:

    import tkinter
    
    def kresli(event):
        x, y = event.x, event.y
        canvas.create_line(x, y, x+100, y, width=2)
    
    def zmaz(event):
        canvas.delete('all')
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<B1-Motion>', kresli)
    canvas.bind('<ButtonPress-3>', zmaz)
    

  1. Riešenie:

    import tkinter
    
    r = 1
    
    def kresli(event):
        global r
        x, y = event.x, event.y
        canvas.create_oval(x-r, y-r, x+r, y+r, fill='yellow')
        r += 1
    
    def zmaz(event):
        global r
        r = 1
        canvas.delete('all')
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<B1-Motion>', kresli)
    canvas.bind('<ButtonPress-3>', zmaz)
    

  1. Riešenie:

    import tkinter
    import random
    
    zoznam = []
    
    def klik(event):
        x, y = event.x, event.y
        zoznam.append((x, y))
        if len(zoznam) == 2:
            canvas.create_rectangle(zoznam, fill=f'#{random.randrange(256**3):06x}')
            zoznam.clear()
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<ButtonPress>', klik)
    

  1. Riešenie:

    import tkinter
    import random
    
    zoznam = []
    
    def klik(event):
        x, y = event.x, event.y
        zoznam.append((x, y))
        canvas.create_text(x, y, text='+')
        if len(zoznam) == 3:
            canvas.create_polygon(zoznam, fill=f'#{random.randrange(256**3):06x}')
            zoznam.pop(0)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<ButtonPress>', klik)
    

  1. Riešenie:

    import tkinter
    import random
    
    zoznam = []
    
    def klik(event):
        x, y = event.x, event.y
        zoznam.append((x, y))
        canvas.create_text(x, y, text='+')
        x0, y0 = zoznam[0]
        if len(zoznam) > 2 and (x-x0)**2+(y-y0)**2 < 25:
            canvas.create_polygon(zoznam, fill=f'#{random.randrange(256**3):06x}')
            zoznam.clear()
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<ButtonPress>', klik)
    

  1. Riešenie:

    import tkinter
    import random
    
    zoznam = []
    
    def klik(event):
        x, y = event.x, event.y
        zoznam.append((x, y))
        canvas.create_text(x, y, text='+', tag='+')
        x0, y0 = zoznam[0]
        if len(zoznam) > 2 and (x-x0)**2+(y-y0)**2 < 25:
            canvas.create_polygon(zoznam, fill=f'#{random.randrange(256**3):06x}')
            zoznam.clear()
            canvas.delete('+')
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<ButtonPress>', klik)
    

  1. Riešenie:

    import tkinter
    
    def klik(event):
        x, y = event.x, event.y
        if x1 <= x <= x2 and y1 <= y <= y2:
            farba = farby.pop(0)
            farby.append(farba)
            canvas.itemconfig(obd, fill=farba)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    x1, y1, x2, y2 = 100, 50, 200, 100
    farby = ['blue', 'red', 'green', 'yellow']
    obd = canvas.create_rectangle(x1, y1, x2, y2, fill='yellow')
    
    canvas.bind('<ButtonPress>', klik)
    

    alebo:

    import tkinter
    
    def klik(event):
        farba = farby.pop(0)
        farby.append(farba)
        canvas.itemconfig(obd, fill=farba)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    x1, y1, x2, y2 = 100, 50, 200, 100
    farby = ['blue', 'red', 'green', 'yellow']
    obd = canvas.create_rectangle(x1, y1, x2, y2, fill='yellow')
    
    canvas.tag_bind(obd, '<ButtonPress>', klik)
    

  1. Riešenie:

    import tkinter
    
    r = [1]
    
    def klik(event):
        x, y = event.x, event.y
        canvas.create_text(x, y, text=r)
        r[0] += 1
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<ButtonPress>', klik)
    

  1. Riešenie:

    import tkinter
    import random
    
    def klik(event):
        x = event.x // 50 * 50
        y = event.y // 50 * 50
        farba = f'#{random.randrange(256**3):06x}'
        canvas.create_rectangle(x, y, x + 50, y + 50, fill=farba)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<ButtonPress>', klik)
    

  1. Riešenie:

    import tkinter
    
    def klik(event):
        x, y = event.x, event.y
        vzd = ((x-x0) ** 2 + (y-y0) ** 2) ** 0.5
        if vzd < r:
            f = int(255 * vzd / r)
            farba = f'#{f:02x}{f:02x}{f:02x}'
            canvas.itemconfig(kruh, fill=farba)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    r, x0, y0 = 120, 150, 130
    kruh = canvas.create_oval(x0 - r, y0 - r, x0 + r, y0 + r)
    canvas.bind('<ButtonPress>', klik)
    canvas.bind('<Motion>', klik)
    

  1. Riešenie:

    import tkinter
    import random
    
    farba = 'blue'
    
    def klik(event):
        global farba
        farba = f'#{random.randrange(256**3):06x}'
    
    def tahaj(event):
        for i in range(20):
            x = event.x + random.randint(-30, 30)
            y = event.y + random.randint(-30, 30)
            canvas.create_oval(x-2, y-2, x+2, y+2, fill=farba, width=0)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<ButtonPress-1>', klik)
    canvas.bind('<B1-Motion>', tahaj)
    

  1. Riešenie:

    import tkinter
    
    xy = []
    
    def klik(event):
        global usecka, xy
        xy = [event.x, event.y] * 2
        usecka = canvas.create_line(xy)
    
    def tahaj(event):
        xy[2:4] = [event.x, event.y]
        canvas.coords(usecka, xy)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<B1-Motion>', tahaj)
    canvas.bind('<ButtonPress>', klik)
    

  1. Riešenie:

    import tkinter
    import random
    
    xy = []
    
    def klik(event):
        global obdlz, xy
        xy = [event.x, event.y] * 2
        farba = f'#{random.randrange(256**3):06x}'
        obdlz = canvas.create_rectangle(xy, fill=farba)
    
    def tahaj(event):
        xy[2:4] = [event.x, event.y]
        canvas.coords(obdlz, xy)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<B1-Motion>', tahaj)
    canvas.bind('<ButtonPress>', klik)
    

  1. Riešenie:

    import tkinter
    
    dx = dy = None
    
    def klik(event):
        global dx, dy
        dx, dy = event.x - x1, event.y - y1
        if dx < 0 or dx > 50 or dy < 0 or dy > 50:
            dx = dy = None
    
    def tahaj(event):
        if dx == None:
            return
        global x1, y1
        x1, y1 = event.x - dx, event.y - dy
        canvas.coords(stv, x1, y1, x1 + 50, y1 + 50)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    x1, y1 = 100, 50
    stv = canvas.create_rectangle(x1, y1, x1 + 50, y1 + 50, fill='red')
    canvas.bind('<B1-Motion>', tahaj)
    canvas.bind('<ButtonPress>', klik)
    

  1. Riešenie:

    import tkinter
    
    def klik(event):
        global dx, dy, ktory
        for utvar in stv2, stv1:          # zoznam vsetkych obdlznikov v opacnom poradi
            x1, y1, x2, y2 = canvas.coords(utvar)
            dx, dy = event.x - x1, event.y - y1
            if 0 <= dx < x2 - x1 and 0 <= dy < y2 - y1:
                ktory = utvar
                canvas.bind('<B1-Motion>', tahaj)
                return
    
    def tahaj(event):
        x1, y1, x2, y2 = canvas.coords(ktory)
        sir, vys = x2 - x1, y2 - y1
        x1, y1 = event.x - dx, event.y - dy
        canvas.coords(ktory, x1, y1, x1 + sir, y1 + vys)
    
    def pusti(event):
        canvas.unbind('<B1-Motion>')
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    stv1 = canvas.create_rectangle(100, 50, 200, 100, fill='red')
    stv2 = canvas.create_rectangle(250, 100, 350, 200, fill='blue')
    canvas.bind('<ButtonPress>', klik)
    canvas.bind('<ButtonRelease>', pusti)
    

  1. Riešenie:

    import tkinter
    
    def klaves(event):
        global text
        if 'a' <= event.char.lower() <= 'z' or event.char in ' \r': # return
            text += event.char
            canvas.itemconfig(riadky, text=text)
        elif event.char == '\b':    # backspace
            text = text[:-1]
            canvas.itemconfig(riadky, text=text)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    text = ''
    riadky = canvas.create_text(190, 130, fill='blue', font='arial 30')
    canvas.bind_all('<KeyPress>', klaves)
    

  1. Riešenie:

    import tkinter
    from math import sin, cos, radians
    
    def timer():
        global uhol
        uhol += 10
        x = x0 + r * cos(radians(uhol))
        y = y0 + r * sin(radians(uhol))
        canvas.coords(bodka, x - 5, y - 5, x + 5, y + 5)
        canvas.after(100, timer)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    r, x0, y0 = 100, 150, 120
    canvas.create_oval(x0 - r, y0 - r, x0 + r, y0 + r)
    bodka = canvas.create_oval(x0 + r - 5, y0 - 5, x0 + r + 5, y0 + 5,
                               width=0, fill='red')
    uhol = 0
    timer()
    

  1. Riešenie:

    import tkinter
    from math import sin, cos, radians
    
    def timer():
        global uhol1, uhol2
        uhol1 += 10
        x = x0 + r * cos(radians(uhol1))
        y = y0 + r * sin(radians(uhol1))
        canvas.coords(bodka1, x - 5, y - 5, x + 5, y + 5)
        uhol2 += 15
        x = x0 + r * cos(radians(uhol2))
        y = y0 + r * sin(radians(uhol2))
        canvas.coords(bodka2, x - 7, y - 7, x + 7, y + 7)
        canvas.after(100, timer)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    r, x0, y0 = 100, 150, 120
    canvas.create_oval(x0 - r, y0 - r, x0 + r, y0 + r)
    bodka1 = canvas.create_oval(x0 + r - 5, y0 - 5, x0 + r + 5, y0 + 5,
                                width=0, fill='red')
    bodka2 = canvas.create_oval(x0 + r - 7, y0 - 7, x0 + r + 7, y0 + 7,
                                width=0, fill='blue')
    uhol1 = uhol2 = 0
    timer()
    

  1. Riešenie:

    import tkinter
    import time
    
    def timer():
        global cas
        cas += 1
        dsek, sek = cas % 10, cas // 10
        sek, min = sek % 60, sek // 60
        min, hod = min % 60, min // 60
        canvas.itemconfig(text, text=f'{hod}:{min:02}:{sek:02}.{dsek}')
        canvas.after(100, timer)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    text = canvas.create_text(190, 130, fill='blue', font='consolas 40')
    h, m, s = time.localtime()[3:6]
    cas = h*36000 + m*600 + s*10
    timer()
    

  1. Riešenie:

    import tkinter
    import random
    
    interval, r = 1000, 20
    
    def timer():
        global x0, y0
        x0 = random.randrange(r, 380 - r)
        y0 = random.randrange(r, 260 - r)
        canvas.coords(kruh, x0 - r, y0 - r, x0 + r, y0 + r)
        canvas.after(interval, timer)
    
    def klik(event):
        global skore
        x, y = event.x, event.y
        if (x-x0) ** 2 + (y-y0) ** 2 <= r ** 2:
            skore += 10
        else:
            skore -= 1
        canvas.itemconfig(text, text=f'skore = {skore}')
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    kruh = canvas.create_oval(0, 0, 0, 0, fill='light blue')
    text = canvas.create_text(50, 10, text='skore = 0')
    skore = 0
    timer()
    canvas.bind('<ButtonPress>', klik)
    

  1. Riešenie:

    import tkinter
    import random
    
    def timer():
        i = 0
        while i < len(kruhy):
            kruh = kruhy[i]
            x1, y1, x2, y2 = canvas.coords(kruh)
            r, x, y = (x2-x1) / 2, (x1+x2) / 2, (y1+y2) / 2
            if r >= 1:
                r -= 1
                canvas.coords(kruh, x-r, y-r, x+r, y+r)
                i += 1
            else:
                canvas.delete(kruh)
                del kruhy[i]
        canvas.after(100, timer)
    
    def klik(event):
        r, x, y = 50, event.x, event.y
        farba = f'#{random.randrange(256**3):06x}'
        kruh = canvas.create_oval(x-r, y-r, x+r, y+r, fill=farba)
        kruhy.append(kruh)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    kruhy = []
    timer()
    canvas.bind('<ButtonPress>', klik)