Riešenie 15. cvičenia


  1. Riešenie:

    class Obdlznik:
    
        def __init__(self, a, b):
            self.a, self.b = a, b
    
        def __str__(self):
            return f'Obdlznik({self.a}, {self.b})'
    
        def obsah(self):
            return self.a * self.b
    
        def obvod(self):
            return (self.a + self.b) * 2
    
        def zmen_velkost(self, pomer):
            self.a *= pomer
            self.b *= pomer
    
        def kopia(self):
            return Obdlznik(self.a, self.b)
    

  1. Riešenie:

    class TelefonnyZoznam:
        def __init__(self, meno_suboru):
            self.zoznam = []
            self.meno = meno_suboru
    
        def vypis(self):
            for meno, telefon in self.zoznam:
                print(f'{meno}: {telefon}')
    
        def pridaj(self, meno, telefon):
            for i in range(len(self.zoznam)):
                if self.zoznam[i][0] == meno:
                    self.zoznam[i] = meno, telefon
                    return
            self.zoznam.append((meno, telefon))
    
        def zapis(self):
            with open(self.meno, 'w') as subor:
                for meno, telefon in self.zoznam:
                    print(f'{meno};{telefon}', file=subor)
    
        def citaj(self):
            self.zoznam = []
            with open(self.meno) as subor:
                for riadok in subor:
                    meno, telefon = riadok.strip().split(';')
                    self.zoznam.append((meno, telefon))
    

  1. Riešenie:

    import tkinter
    import random
    
    class Stv:
        canvas = None
    
        def __init__(self, x, y, a=20, farba=''):
            if farba == '':
                farba = f'#{random.randrange(256**3):06x}'
            self.farba = farba
            self.id = self.canvas.create_rectangle(x-a/2, y-a/2, x+a/2, y+a/2, fill=farba)
    
        def posun(self, dx, dy):
            self.canvas.move(self.id, dx, dy)
    
        def zmen_farbu(self, farba):
            self.farba = farba
            self.canvas.itemconfig(self.id, fill=farba)
    
    Stv.canvas = tkinter.Canvas()
    Stv.canvas.pack()
    
    vsetky = []
    for i in range(30):
       vsetky.append(Stv(random.randint(50, 300), random.randint(50, 200)))
    Stv.canvas.update()
    input('enter')
    for i, s in enumerate(vsetky):
        if i % 2:
            s.posun(50, 10)
        else:
            s.zmen_farbu('red')
    

  1. Riešenie:

    class Dvojica:
        def __init__(self, x, y, a=20):
            self.s1 = Stv(x, y, a)
            self.s2 = Stv(x+a, y, a)
    
        def posun(self, dx, dy):
            self.s1.posun(dx, dy)
            self.s2.posun(dx, dy)
    
        def vymen(self):
            f1, f2 = self.s1.farba, self.s2.farba
            self.s1.zmen_farbu(f2)
            self.s2.zmen_farbu(f1)
    
    dvojice = []
    for i in range(20):
        dvojice.append(Dvojica(random.randint(50, 300), random.randint(50, 200), random.randint(20, 50)))
    Stv.canvas.update()
    input('enter')
    for d in dvojice:
        d.posun(50, 10)
    Stv.canvas.update()
    input('enter')
    for d in dvojice:
        d.vymen()
    

  1. Riešenie:

    import tkinter
    
    class Klikanie:
        def __init__(self):
            self.canvas = tkinter.Canvas()
            self.canvas.pack()
            self.canvas.bind('<ButtonPress>', self.klik)
    
        def klik(self, event):
            x, y = event.x, event.y
            self.canvas.create_oval(x-5, y-5, x+5, y+5)
    
    k = Klikanie()
    

  1. Riešenie:

    import tkinter
    
    class Klikanie:
        def __init__(self):
            self.canvas = tkinter.Canvas()
            self.canvas.pack()
            self.canvas.bind('<ButtonPress>', self.klik)
            self.zoznam = []
    
        def klik(self, event):
            x, y = event.x, event.y
            self.canvas.create_oval(x-5, y-5, x+5, y+5)
            self.zoznam.append((x, y))
    
        def vypis(self):
            for xy in self.zoznam:
                print(xy)
    
    k = Klikanie()
    

  1. Riešenie:

    import tkinter
    
    class Klikanie:
        def __init__(self):
            self.canvas = tkinter.Canvas()
            self.canvas.pack()
            self.canvas.bind('<ButtonPress>', self.klik)
            self.zoznam = []
    
        def klik(self, event):
            x, y = event.x, event.y
            self.canvas.create_oval(x-5, y-5, x+5, y+5)
            self.zoznam.append((x, y))
            if len(self.zoznam) > 1:
                self.canvas.create_line(self.zoznam[-2:])
    
        def vypis(self):
            for xy in self.zoznam:
                print(xy)
    
    k = Klikanie()
    

  1. Riešenie:

    import tkinter
    
    class Tahanie:
        def __init__(self):
            self.canvas = tkinter.Canvas()
            self.canvas.pack()
            self.canvas.bind('<ButtonPress>', self.klik)
            self.canvas.bind('<B1-Motion>', self.tahanie)
    
        def klik(self, event):
            self.xy = x, y = event.x, event.y
            self.canvas.create_oval(x-5, y-5, x+5, y+5)
    
        def tahanie(self, event):
            xy = event.x, event.y
            self.canvas.create_line(self.xy, xy)
            self.xy = xy
    
    Tahanie()
    

  1. Riešenie:

    import tkinter
    
    class VyrobPolygon:
        def __init__(self, meno_suboru):
            self.meno = meno_suboru
            with open(meno_suboru, 'w'):
                pass
            self.canvas = tkinter.Canvas()
            self.canvas.pack()
            self.poly = self.canvas.create_polygon(0, 0, outline='black', fill='white')
            self.canvas.bind('<ButtonPress>', self.klik)
            self.canvas.bind_all('<Return>', self.enter)
            self.zoznam = []
    
        def klik(self, event):
            self.zoznam.extend((event.x, event.y))
            self.canvas.coords(self.poly, self.zoznam)
    
        def enter(self, event):
            with open(self.meno, 'a') as subor:
                for i in self.zoznam:
                    print(i, end=' ', file=subor)
                print(file=subor)
            self.poly = self.canvas.create_polygon(0, 0, outline='black', fill='white')
            self.zoznam = []
    
    VyrobPolygon('poly.txt')
    

  1. Riešenie:

    import tkinter
    import random
    
    class CitajPolygon:
        def __init__(self, meno_suboru):
            self.canvas = tkinter.Canvas()
            self.canvas.pack()
            self.vsetky = []
            with open(meno_suboru) as subor:
                for riadok in subor:
                    zoznam = riadok.split()
                    for i in range(len(zoznam)):
                        zoznam[i] = int(zoznam[i])
                    poly = self.canvas.create_polygon(zoznam, outline='black', fill='white')
                    self.vsetky.append(poly)
            self.canvas.bind('<ButtonPress>', self.prefarbi)
    
        def prefarbi(self, event):
            for poly in self.vsetky:
                self.canvas.itemconfig(poly, fill=f'#{random.randrange(256**3):06x}')
    
    CitajPolygon('poly.txt')
    

  1. Riešenie:

    import tkinter
    import random
    
    class MojaGrafika:
        def __init__(self):
            self.canvas = tkinter.Canvas(width=400, height=300)
            self.canvas.pack()
            self.utvary = []
    
        def kruh(self, r, x, y, farba=None):
            if farba is None:
                farba = f'#{random.randrange(256**3):06x}'
            self.canvas.create_oval(x - r, y - r, x + r, y + r, fill=farba)
            self.utvary.append(f'kruh {r} {x} {y} {farba}')
    
        def stvorec(self, a, x, y, farba=None):
            if farba is None:
                farba = f'#{random.randrange(256**3):06x}'
            r = a / 2
            self.canvas.create_rectangle(x - r, y - r, x + r, y + r, fill=farba)
            self.utvary.append(f'stvorec {a} {x} {y} {farba}')
    
        def text(self, text, x, y, farba=None):
            if farba is None:
                farba = f'#{random.randrange(256**3):06x}'
            self.canvas.create_text(x, y, text=text, fill=farba, font='arial 30')
            self.utvary.append(f'text {text} {x} {y} {farba}')
    
        def zapis(self, meno_suboru):
            with open(meno_suboru, 'w') as subor:
                print('\n'.join(self.utvary), file=subor)
    
        def citaj(self, meno_suboru):
            self.canvas.delete('all')
            with open(meno_suboru) as subor:
                for riadok in subor:
                    typ, param, x, y, farba = riadok.split()
                    x, y = int(x), int(y)
                    if typ == 'kruh':
                        self.kruh(int(param), x, y, farba)
                    elif typ == 'stvorec':
                        self.stvorec(int(param), x, y, farba)
                    elif typ == 'text':
                        self.text(param, x, y, farba)