Riešenie 10. cvičenia


  1. Riešenie:

    import tkinter
    
    def klik(event):
        canvas.delete('all')
    
    def tahaj(event):
        x, y = event.x, event.y
        if x < 190:
            farba = 'red'
        else:
            farba = 'green'
        canvas.create_oval(x-10, y-10, x+10, y+10, fill=farba)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<B1-Motion>', tahaj)
    canvas.bind('<ButtonPress-3>', klik)
    

  1. Riešenie:

    import tkinter
    
    def klik(event):
        canvas.delete('all')
    
    def tahaj(event):
        x, y = event.x, event.y
        canvas.create_line(x, y, x, y-50)
        canvas.create_oval(x-5, y-55, x+5, y-45, fill='red')
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<B1-Motion>', tahaj)
    canvas.bind('<ButtonPress-3>', klik)
    

  1. Riešenie:

    import tkinter
    
    def klik(event):
        canvas.delete('all')
    
    def tahaj(event):
        x, y = event.x, event.y
        canvas.create_oval(x-5, y-5, x+5, y+5, fill='blue')
        x = 380 - x
        canvas.create_oval(x-5, y-5, x+5, y+5, fill='red')
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<B1-Motion>', tahaj)
    canvas.bind('<ButtonPress-3>', klik)
    

  1. Riešenie:

    import tkinter
    import random
    
    def tahaj(event):
        x, y = event.x, event.y
        if 0 <= x < 256 and 0 <= y < 256:
            farba = f'#{x:02x}{y:02x}00'
            canvas.create_rectangle(x-8, y-8, x+8, y+8, fill=farba, width=0)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<ButtonPress-1>', tahaj)
    canvas.bind('<B1-Motion>', tahaj)
    

  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
    
    zoznam = list('PYTHON')
    #zoznam = 'Python Java Pascal C++'.split()
    
    def klik(event):
        canvas.create_text(event.x, event.y, text=zoznam[0], font='Arial 25')
        zoznam.append(zoznam.pop(0))
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<ButtonPress>', klik)
    

  1. Riešenie:

    import tkinter
    
    def klik(event):
        x = canvas.coords(ciara)[2]
        canvas.coords(ciara, 10, 100, x+5, 100)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    ciara = canvas.create_line(10, 100, 20, 100, fill='blue', width=20, capstyle='round')
    canvas.bind('<ButtonPress>', klik)
    

  1. Riešenie:

    import tkinter
    import random
    
    r = 0
    
    def klik(event):
        global x, y, r, kruh
        if r <= 5:
            x, y, r = event.x, event.y, 50
            farba = f'#{random.randrange(256**3):06x}'
            kruh = canvas.create_oval(x-r, y-r, x+r, y+r, fill=farba)
        else:
            r -= 5
            canvas.coords(kruh, x-r, y-r, x+r, y+r)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<ButtonPress>', klik)
    

  1. Riešenie:

    import tkinter
    import random
    
    def klik(event):
        i = (event.x-x0) // 45
        if 0 <= i < 8 and y0 <= event.y < y0+45:
            canvas.itemconfig(zoznam[i], fill=f'#{random.randrange(256**3):06x}')
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    zoznam = []
    x0, y0 = 10, 50
    for i in range(8):
        id = canvas.create_rectangle(x0+45*i, y0, x0+45*i+45, y0+45, fill='white')
        zoznam.append(id)
    
    canvas.bind('<ButtonPress>', klik)
    

  1. Riešenie:

    import tkinter
    import random
    
    def klik(event):
        x, y = event.x//50 * 50, event.y//50 * 50
        farba = random.choice(('white', 'blue', 'red'))
        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
    import random
    
    bezi = False
    x = 0
    
    def klik(event):
        global bezi
        bezi = not bezi
        if bezi:
            casomiera()
    
    def casomiera():
        global x
        x += 1
        canvas.itemconfig(cislo, text=x)
        if bezi:
            canvas.after(100, casomiera)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    cislo = canvas.create_text(190, 130, text=x, font='arial 50', fill='navy')
    canvas.bind('<ButtonPress>', klik)
    

  1. Riešenie:

    import tkinter
    
    poz = []
    
    def klik(event):
        poz[:] = event.x, event.y
    
    def klaves(event):
        if poz != []:
            znak = event.keysym
            print(znak)
            if znak == 'space':
                znak = ' '
            if len(znak) == 1:
                canvas.create_text(poz, text=znak, font='arial 30')
                poz[0] += 30
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.bind('<ButtonPress>', klik)
    canvas.bind_all('<Key>', klaves)
    

  1. Riešenie:

    import tkinter
    
    def kresli():
        x, y = poz
        canvas.create_oval(x-5, y-5, x+5, y+5, fill='green')
        x += [5, 0, -5, 0][smer]
        y += [0, 5, 0, -5][smer]
        poz[:] = x, y
        canvas.after(100, kresli)
    
    def klaves(event):
        global smer
        if event.keysym == 'Right':
            smer = 0
        elif event.keysym == 'Down':
            smer = 1
        elif event.keysym == 'Left':
            smer = 2
        elif event.keysym == 'Up':
            smer = 3
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    poz = [100, 100]
    smer = 0
    
    canvas.bind_all('<Key>', klaves)
    kresli()
    

  1. Riešenie:

    import tkinter
    import random
    from math import cos, sin, radians
    
    bezi = False
    uhol = -90
    
    def klik(event):
        global bezi
        bezi = not bezi
        if bezi:
            casomiera()
    
    def casomiera():
        global uhol
        uhol += 1
        x = 150 + 100*cos(radians(uhol))
        y = 120 + 100*sin(radians(uhol))
        canvas.coords(rucicka, 150, 120, x, y)
        if bezi:
            canvas.after(100, casomiera)
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    canvas.create_oval(150-100, 120-100, 150+100, 120+100, fill='white')
    rucicka = canvas.create_line(150, 120, 150, 120-100, fill='navy', width=3)
    canvas.bind('<ButtonPress>', klik)