#--------------------------------------------------------------------- # Esempio uso metodi put e get PhotoImage #--------------------------------------------------------------------- try: import Tkinter as tk # Py2 except ImportError: import tkinter as tk # Py3 from random import randrange as rnd #--------------------------------------------------------------------- SHAPE_DATA = [ "01100110", "00011000", "01111110", "11011011", "11111111", "11100111", "01111110", "10011001" ] SCALE = 4 BGCOLOR = 0, 0, 170 #--------------------------------------------------------------------- def create_udg(shape_data): image = tk.PhotoImage(width=8*SCALE, height=8*SCALE) background_color = "{#%02x%02x%02x}" % BGCOLOR for y, row_data in enumerate(shape_data): y_scaled = y * SCALE for x, pixel in enumerate(row_data): x_scaled = x * SCALE color = "{#00FFFF}" if pixel == "1" else background_color for z in range(SCALE): y_pixel = y_scaled + z for w in range(SCALE): x_pixel = x_scaled + w image.put(color, (x_pixel, y_pixel)) return image #--------------------------------------------------------------------- def modify_udg_color(image): new_color = "{#%02x%02x%02x}" % (rnd(256), rnd(256), rnd(256)) bgcolor = "%d %d %d" % BGCOLOR for y in range(image.height()): for x in range(image.width()): if image.get(x, y) != bgcolor: image.put(new_color, (x, y)) form1.after(100, modify_udg_color, image) #--------------------------------------------------------------------- form1 = tk.Tk() form1.resizable(False, False) canv1 = tk.Canvas( form1, width=300, height=200, bg="#%02x%02x%02x" % BGCOLOR, highlightthickness=0) canv1.grid() image = create_udg(SHAPE_DATA) canv1.create_image(150, 100, image=image, anchor="center") modify_udg_color(image) form1.mainloop()