Skip to content

Instantly share code, notes, and snippets.

@calebmadrigal
Created June 20, 2015 04:40
Show Gist options
  • Save calebmadrigal/81f3b9de14f54ac355f7 to your computer and use it in GitHub Desktop.
Save calebmadrigal/81f3b9de14f54ac355f7 to your computer and use it in GitHub Desktop.
Drawing individual pixels with Tkinter.
import random
import math
from tkinter import Tk, Canvas, PhotoImage, mainloop
width = 1000
height = 600
window = Tk()
canvas = Canvas(window, width=width, height=height, bg="#000000")
canvas.pack()
img = PhotoImage(width=width, height=height)
canvas.create_image((width//2, height//2), image=img, state="normal")
def center_and_invert(y, height):
return int(height/2 - y)
def f(x):
num_cycles = 4
amplitude = 200
return amplitude * math.sin(2 * math.pi * (num_cycles / width) * x)
def graph(f, x_range, height):
for x in x_range:
y = center_and_invert(f(x), height)
img.put("#ffffff", (x, y))
graph(f, range(width), height)
mainloop()
@gweg
Copy link

gweg commented Aug 31, 2020

Just a remark, If you want to get faster your drawing,
replace : img.put("#ffffff", (x, y))
by
canvas.create_rectangle(x,y,x,y,outline="white")
To see the difference make set width = 1000

@kraksy
Copy link

kraksy commented Nov 10, 2022

mm this looks cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment