Riešenie úloh 3. cvičenia


  1. Riešenie:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    x, y = 50, 50
    canvas.create_rectangle(x, y, x+100, y+100, fill='red')
    canvas.create_rectangle(x+110, y, x+210, y+100, fill='blue')
    
    canvas.create_text(x+50, y+50, text='červený', fill='yellow', font='arial 20')
    canvas.create_text(x+160, y+50, text='modrý', fill='yellow', font='arial 20')
    

  1. Riešenie:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    x, y = 50, 50
    a1, a2, a3 = 180, 150, 50
    canvas.create_rectangle(x, y, x+a1, y+a1, fill='indian red')
    canvas.create_rectangle(x+a1/2-a2/2, y+a1/2-a2/2, x+a1/2+a2/2, y+a1/2+a2/2, fill='light blue')
    canvas.create_rectangle(x+a1/2-a3/2, y+a1/2-a3/2, x+a1/2+a3/2, y+a1/2+a3/2, fill='pink')
    
    canvas.create_text(x-5, y-5, text='A')
    canvas.create_text(x+a1+5, y-5, text='B')
    canvas.create_text(x+a1+5, y+a1+5, text='C')
    canvas.create_text(x-5, y+a1+5, text='D')
    

  1. Riešenie:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    # Nemecko
    x, y = 20, 20
    canvas.create_rectangle(x, y, x+135, y+30, fill='black', width=0)
    canvas.create_rectangle(x, y+30, x+135, y+60, fill='red', width=0)
    canvas.create_rectangle(x, y+60, x+135, y+90, fill='yellow', width=0)
    canvas.create_rectangle(x, y, x+135, y+90)
    
    # Taliansko
    x, y = 200, 20
    canvas.create_rectangle(x, y, x+45, y+90, fill='green', width=0)
    canvas.create_rectangle(x+45, y, x+90, y+90, fill='white', width=0)
    canvas.create_rectangle(x+90, y, x+135, y+90, fill='red', width=0)
    canvas.create_rectangle(x, y, x+135, y+90)
    
    # Francuzko
    x, y = 20, 150
    canvas.create_rectangle(x, y, x+45, y+90, fill='blue', width=0)
    canvas.create_rectangle(x+45, y, x+90, y+90, fill='white', width=0)
    canvas.create_rectangle(x+90, y, x+135, y+90, fill='red', width=0)
    canvas.create_rectangle(x, y, x+135, y+90)
    
    # Rusko
    x, y = 200, 150
    canvas.create_rectangle(x, y, x+135, y+30, fill='white', width=0)
    canvas.create_rectangle(x, y+30, x+135, y+60, fill='blue', width=0)
    canvas.create_rectangle(x, y+60, x+135, y+90, fill='red', width=0)
    canvas.create_rectangle(x, y, x+135, y+90)
    

  1. Riešenie:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    x, y = 30, 20
    sirka, vyska = 300, 200
    canvas.create_rectangle(x, y, x+sirka, y+vyska/2, fill='white', width=0)
    canvas.create_rectangle(x, y+vyska/2, x+sirka, y+vyska, fill='red', width=0)
    canvas.create_polygon(x, y, x+sirka/2, y+vyska/2, x, y+vyska, fill='navy')
    canvas.create_rectangle(x, y, x+sirka, y+vyska)
    

  1. Riešenie:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    x, y = 80, 100
    r = 50
    dx, dy = 120, 60
    canvas.create_oval(x-r, y-r, x+r, y+r, outline='blue', width=10)
    canvas.create_oval(x-r+dx/2, y-r+dy, x+r+dx/2, y+r+dy, outline='yellow', width=10)
    canvas.create_oval(x-r+dx, y-r, x+r+dx, y+r, outline='black', width=10)
    canvas.create_oval(x-r+dx+dx/2, y-r+dy, x+r+dx+dx/2, y+r+dy, outline='limegreen', width=10)
    canvas.create_oval(x-r+2*dx, y-r, x+r+2*dx, y+r, outline='red', width=10)
    

  1. Riešenie:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    x, y = 180, 250
    sirka = 100
    for farba in 'darkgreen', 'green', 'yellowgreen', 'limegreen':
        canvas.create_rectangle(x-sirka, y, x+sirka, y-50, fill=farba)
        y -= 50
        sirka -= 25
    

  1. Riešenie:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    n = int(input('zadaj n: '))
    
    x, y = 10, 100
    d = 20
    for i in range(n):
        x1, y1 = x+abs(d), y+d
        canvas.create_line(x, y, x1, y1, width=5, fill='blue')
        x, y = x1, y1
        d = -d
    

  1. Riešenie:

    import tkinter
    from random import randrange
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    n = int(input('zadaj n: '))
    a = 370 // n - 5
    x, y = 10, 50
    for i in range(n):
        canvas.create_rectangle(x, y, x+a, y+a, fill=f'#{randrange(256**3):06x}')
        x += a+5
    

  1. Riešenie:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    x, y = 50, 250
    a = 280
    canvas.create_polygon(x, y, x+a, y, x+a/2, y-a*(3**.5/2), fill='blue')
    

  1. Riešenie:

    import tkinter
    import random
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    n = int(input('zadaj n: '))
    for i in range(n):
        x = random.randint(10, 330)
        y = random.randint(50, 210)
        a = random.randint(10, 50)
        f1 = f'#{random.randrange(256**3):06x}'
        f2 = f'#{random.randrange(256**3):06x}'
        canvas.create_polygon(x, y, x+a, y, x+a/2, y-a*(3**.5/2), fill=f1)
        canvas.create_rectangle(x, y, x+a, y+a, fill=f2, width=0)
    

  1. Riešenie:

    import tkinter
    import random
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    n = int(input('zadaj počet: '))
    sucet = 0
    x, y = 100, 20
    for i in range(n):
        canvas.create_rectangle(x, y, x+50, y+20)
        cislo = random.choice((1, 2, 5, 10, 20, 50))
        canvas.create_text(x+25, y+10, text=f'{cislo:2} €', font='arial 16')
        sucet += cislo
        y += 22
    canvas.create_text(250, 50, text=f'spolu = {sucet} €', font='arial 16')
    

  1. Riešenie:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    n = int(input('zadaj počet stĺpcov: '))
    m = int(input('zadaj počet riadkov: '))
    
    vel = 30
    farba1, farba2 = 'maroon', 'gold'
    x, y = 10, 10
    for i in range(m):
        f1, f2 = farba1, farba2
        for j in range(n):
            canvas.create_rectangle(x+(vel+3)*j, y+(vel+3)*i, x+(vel+3)*j+vel, y+(vel+3)*i+vel, fill=f1)
            f1, f2 = f2, f1
        farba1, farba2 = farba2, farba1
    

  1. Riešenie:

    import tkinter
    
    canvas = tkinter.Canvas()
    canvas.pack()
    
    x, y = 30, 30
    sir, vys = 325, 216
    modra, cervena = '#0b4ea2', '#ee1c25'
    canvas.create_rectangle(x, y, x+sir, y+vys/3, fill='white', width=0)
    canvas.create_rectangle(x, y+vys/3, x+sir, y+vys*2/3, fill=modra, width=0)
    canvas.create_rectangle(x, y+vys*2/3, x+sir, y+vys, fill=cervena, width=0)
    obr = tkinter.PhotoImage(file='sk.png')
    canvas.create_image(x+100, y+vys/2, image=obr)