Skip to content

Instantly share code, notes, and snippets.

@JohnReeves
Last active August 29, 2015 14:00
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 JohnReeves/48dad4016cb52f3d4b8a to your computer and use it in GitHub Desktop.
Save JohnReeves/48dad4016cb52f3d4b8a to your computer and use it in GitHub Desktop.
# import the libraries
from tkinter import *
from random import *
# Step 1: create the top level Tk object
window=Tk()
c_wd,c_ht=800,600
col=["red","orange","yellow","green","blue","purple","white","black"]
# Step 2: attach the canvas for drawing on
canvas=Canvas(width=c_wd,height=c_ht,bg=col[len(col)-1])
# Step 3: draw stuff on the canvas
# uncomment create_ methods for different art stylies
for n in range(50):
x0,y0=randint(0,c_wd),randint(0,c_ht)
x1,y1=randint(0,c_wd),randint(0,c_ht)
index=randint(0,len(col)-2)
#canvas.create_line(x0,y0,x1,y1,width=15,fill=col[index])
canvas.create_arc(x0,y0,x1,y1,width=5,fill=col[index])
#canvas.create_rectangle(x0,y0,x1,y1,width=15,fill=col[index])
#canvas.create_oval(x0,y0,x1,y1,width=15,fill=col[index])
# Step 4: pack the canvas and run the TK mainloop
canvas.pack()
window.mainloop()
from tkinter import *
# Step 1: create the top level object
# could call this anything, needs to match up with mainloop
#
window=Tk()
c_wd,c_ht=600,400
c_col="black"
x0,y0=c_wd/2,c_ht
l_len,l_wd=150,5
l_col="green"
# Step 2: attach the canvas
canvas=Canvas(width=c_wd,height=c_ht,bg=c_col)
# Step 3: draw something using the canvas objects' methods
canvas.create_line(x0,y0,x0,y0-l_len,width=l_wd,fill=l_col)
# Step 4: pack the canvas and run the mainloop
canvas.pack()
window.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment