Lardmeister 461 Posting Virtuoso

This little experiment with Tkinter and pixel drawing shows some strange effects (look at line 27 comments!!!):

'''tk_put_pixel.py
place a pixel at pos=(x,y) with color=(r,g,b) on an image area
note:
one pixel might be hard to see, so create a series of pixels (line)
'''

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

def put_pixel(image, pos, color):
    """
    place a pixel at pos=(x,y) with color=(r,g,b) on an image area
    """
    r, g, b = color
    x, y = pos
    # color given in HTML format "#rrggbb"
    # blue is (0,0,255) --> #0000ff
    ph_image.put("#%02x%02x%02x" % (r, g, b), (x, y))

root = tk.Tk()

# create a blank image area
w = 32*2   # smaller values than 128 go goofy, line too short!!!!
h = 32
ph_image = tk.PhotoImage(width=w, height=h)

# create a series of pixels on the image area
y = 16
r = 0
g = 0
b = 255  # full blue
for x in range(10, w):
    print(x, y)  # test
    put_pixel(ph_image, (x,y), (r,g,b))

label = tk.Label(root, image=ph_image)
label.grid()
root.mainloop()

Does anyone know why?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.