1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
   | from tkinter import *
from PIL import ImageTk
 
root = Tk()
 
option = input("option [d, o, h] ?")
 
# option directe ------------------------------------------------------------------------
if option == 'd':
    frame = Frame(root, width=400, height=400, bg='red')
    frame.grid(row=0, column=0, sticky=E+W)
 
    photo = ImageTk.PhotoImage(file="color.png")
 
    canvas = Canvas(frame, width=photo.width(), height=photo.height(), bg='pink')
    canvas.grid(row=0, column=0, sticky=E+W)
 
    canvas.create_image(5, 5, anchor=NW, image=photo)
    canvas.create_line(0, 100, 200, 0, fill="red")
 
# option OO sans héritage ------------------------------------------------------------------
elif option == 'o':
    class maFrame():
        def __init__(self, root):
            frame = Frame(root, width=400, height=400, bg='red')
            frame.grid(row=0, column=0, sticky=E+W)
 
            photo = ImageTk.PhotoImage(file="color.png")
 
            canvas = Canvas(frame, width=photo.width(), height=photo.height(), bg='pink')
            canvas.grid(row=0, column=0, sticky=E+W)
 
            canvas.create_image(5, 5, anchor=NW, image=photo)
            canvas.create_line(0, 100, 200, 0, fill="red")
 
    maFrame(root)
 
# option OO avec héritage  ------------------------------------------------------------------------
elif option == 'h':
    class maFrame(Frame):
        def __init__(self, root):
            super().__init__(root, width=400, height=400, bg='red')
            self.grid(row=0, column=0, sticky=E + W)
 
            photo = ImageTk.PhotoImage(file="color.png")
 
            canvas = Canvas(self, width=photo.width(), height=photo.height(), bg='pink')
            canvas.grid(row=0, column=0, sticky=E+W)
 
            canvas.create_image(5, 5, anchor=NW, image=photo)
            canvas.create_line(0, 100, 200, 0, fill="red")
 
    mf = maFrame(root)
 
root.mainloop() | 
Partager