Created
May 13, 2017 15:28
-
-
Save PM2Ring/2b9210d52b96dc711e3ee5ae2415c8ac to your computer and use it in GitHub Desktop.
Pixel plotting with Tkinter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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