ATTEMPT 3 - I swear the last ...
I believe this is the fastest TK way to do this. Creates 10,000 RGB values ββin a list of lists, creates Tkinter.PhotoImage, and then puts pixel values ββin it.
import Tkinter, random class App: def __init__(self, t): self.i = Tkinter.PhotoImage(width=100,height=100) colors = [[random.randint(0,255) for i in range(0,3)] for j in range(0,10000)] row = 0; col = 0 for color in colors: self.i.put('#%02x%02x%02x' % tuple(color),(row,col)) col += 1 if col == 100: row +=1; col = 0 c = Tkinter.Canvas(t, width=100, height=100); c.pack() c.create_image(0, 0, image = self.i, anchor=Tkinter.NW) t = Tkinter.Tk() a = App(t) t.mainloop()
ATTEMPT 1 - using the create_rectangle method
I wrote this as a test. On my 2.67 GHz Intel Core 2 Duo processor, it will be around 5,000 pixels in 0.6 seconds, including the time it took to generate my random RGB values:
from Tkinter import * import random def RGBs(num): # random list of list RGBs return [[random.randint(0,255) for i in range(0,3)] for j in range(0,num)] def rgb2Hex(rgb_tuple): return '#%02x%02x%02x' % tuple(rgb_tuple) def drawGrid(w,colors): col = 0; row = 0 colors = [rgb2Hex(color) for color in colors] for color in colors: w.create_rectangle(col, row, col+1, row+1, fill=color, outline=color) col+=1 if col == 100: row += 1; col = 0 root = Tk() w = Canvas(root) w.grid() colors = RGBs(5000) drawGrid(w,colors) root.mainloop()
ATTEMPT 2 - Using PIL
I know that you said TK only, but PIL makes it very easy and fast.
def rgb2Hex(rgb_tuple): return '#%02x%02x%02x' % tuple(rgb_tuple) num = 10000