So this program creates a circle randomly on the screen and then you can use the slider to increase or decrease the size of the circle. I also got it to print the coordinates of the circle. I need to make it so that a user can choose a circle and drag and drop to a new location and still use the slider for it.
I am new to events and using the canvas methods and i am still learning from the web. I would appreciate any help that i can get. Thanks guys you have really helped as i have began to learn python
The program code is written on a template given by my teacher so i am trying to follow that as well.
'''
Created on March 18, 2012
Assignment for Grade 11 ICS3UA
'''
from Tkinter import *
import random
def drawCircles():
r = sizeScale.get()
colourChoices = ["Red", "Blue", "Green", "Yellow", "White", "Pink","#FF8C00","#FF00FF"]
color = random.choice(colourChoices)
x = random.randint(0, w-r)
y = random.randint(0, h-r)
circle = cv.create_oval(x, y, x+r, y+r, fill=color)
cv.update()
def chooseShape(event):
global currentShape
currentShape = cv.find_closest(event.x, event.y)
print cv.coords(currentShape)
def resizeShape(event):
pass
def moveShape(event):
newShape = cv.coords(currentShape, event.x, event.y )
print cv.coords(newShape)
h = 400
w = 600
##########
###MAIN###
##########
global currentShape
#generate holding frames
root = Tk()
root.title("Random Circle Generator")
mainframe = Frame(root)
#create the widgets
titleLabel = Label(mainframe, text = "Random Circle Generator", font=("Arial", 30, "bold"))
cv = Canvas(mainframe, width = w, height = h, bg = "black")
sizeCircleLabelFrame = LabelFrame(mainframe,borderwidth=5)
chooseSizeLabel = Label (sizeCircleLabelFrame, text="Choose Size",font = ("Arial", 16, "bold"), fg = "blue")
sizeScale = Scale (sizeCircleLabelFrame, from_=0, to=100, orient = HORIZONTAL, width = 20,bd=3, length = 250, relief = RAISED,tickinterval=25,activebackground="red",highlightbackground="yellow",highlightcolor="red")
sizeScale.set(20)
drawCircleButton = Button (sizeCircleLabelFrame, text = "Draw Circle(s) Now", font = ("Arial", 12, "bold"), relief = RAISED, bd=5, justify = CENTER,activebackground="orange", highlightbackground = "blue", bg = "green", command = drawCircles)
#grid the widgets
mainframe.grid()
titleLabel.grid(row = 0, column = 0, pady = 20, columnspan = 5)
sizeCircleLabelFrame.grid(row = 1, column = 2, columnspan = 2)
chooseSizeLabel.grid(row =2, column=1, pady = 6)
sizeScale.grid(row = 3, column = 1, pady = 10)
drawCircleButton.grid(row = 4, column = 1,pady = 10)
cv.grid(row = 5, column = 1, columnspan = 4,sticky = N+E, rowspan = 3, padx = 20, pady = 20)
cv.bind("<Button-1>", chooseShape)
cv.bind("<B1-ButtonRelease>", moveShape)
root.mainloop()

New Topic/Question
Reply




MultiQuote


|