Skip to content

Instantly share code, notes, and snippets.

@PM2Ring
Created May 13, 2017 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PM2Ring/2b9210d52b96dc711e3ee5ae2415c8ac to your computer and use it in GitHub Desktop.
Save PM2Ring/2b9210d52b96dc711e3ee5ae2415c8ac to your computer and use it in GitHub Desktop.
Pixel plotting with Tkinter
#!/usr/bin/env python3
''' Use a PhotoImage to plot single pixels in a Tkinter Label
For some strange reason, it starts out fast but slows down...
Written by PM 2Ring 2017.05.13
'''
import tkinter as tk
width, height = 301, 211
root = tk.Tk()
photo = tk.PhotoImage(width=width, height=height)
label = tk.Label(root, bg='#000', image=photo)
label.pack()
x, y = 10, 80
dx = dy = 1
def draw():
''' Linear motion, with bouncing '''
global x, y, dx, dy
photo.put('#cf9', (x, y))
x += dx
y += dy
if not 0 <= x < width:
dx = -dx
x += dx
if not 0 <= y < height:
dy = -dy
y += dy
root.after(1, draw)
draw()
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment