Simple GUI Display

This is a simple utility which displays and updates an image from the Python Image library.

from PIL import Image, ImageTk
import Tkinter as tk
from random import randint

class Chip8Screen(tk.Frame):
        def __init__(self, parent, resolution):
                tk.Frame.__init__(self, parent);
                self.parent = parent;
                #self.pack();
                self.screen_data = Image.new('RGB', resolution);
                self.screen = ImageTk.PhotoImage(self.screen_data);
                self.label = tk.Label(parent, image=self.screen);
                self.label.grid(row=0, column=0);
                parent.bind('w', self.draw_random_pixel);

        def redraw_image(self):
                self.screen = ImageTk.PhotoImage(self.screen_data);
                self.label.configure(image=self.screen);

        def draw_random_pixel(self, event=None):
                print("Draw random pixel.");
                x = randint(0, 100);
                y = randint(0, 100);
                r = randint(0, 255);
                g = randint(0, 255);
                b = randint(0, 255);
                self.screen_data.putpixel((x, y), (r, g, b));
                self.redraw_image();

def main():
        root = tk.Tk();
        c8 = Chip8Screen(root, (320, 240));
        root.mainloop();

if __name__=="__main__":
        main();

Comments are closed.