Write a Python program that draws a circle with radius 50 pixels and centered at the center of the canvas. Give the canvas white background color and make it expand with its frame whenever the user resizes the frame. The program will respond to some mouse and keyboard events as described below.
Whenever the left mouse button is pressed, display a message indicating whether the mouse pointer is inside the circle as shown in the figures below. The center of the message's baseline should be at the center of the circle. Moreover, the message should be displayed using a random color. You can use the following method to generate random colors:
Your program also uses the arrow keys to move the circle left, right, up, or down when the Left arrow key, Right arrow key, Up arrow key, or Down arrow key is clicked. Every arrow-key click should move the circle 5 pixels in the appropriate direction. Delete any displayed text when the circle is moved. To bind the Up arrow-key with the event handler self.moveCircle(), you can use the code:
using the arrow keys to move the circle, then clicking the left mouse button will always display the message at the center of circle.
This is what i have so far.
from Tkinter import *
class Circle(Frame):
def __init__(self):
Frame.__init__(self, bg = "white")
self.master.title("circle")
self.grid()
self.radius = 50
self.canvas = Canvas(self, width = 400, height = 400, bg = "white")
self.canvas.grid(row = 0, column = 0)
self.canvas.create_oval(200 - self.radius, 200 - self.radius,
200 + self.radius, 200 + self.radius, tags = "circle")
self.canvas.create_text(200, 200, text = "", tags = "text")
self.master.bind('<Configure>', self.resize)
self.canvas.bind('<Button-1>', self.getWords)
self.canvas.bind('<Up>', self.moveCircle)
self.canvas.bind('<Down>', self.moveCircle)
self.canvas.bind('<Left>', self.moveCircle)
self.canvas.bind('<Right>', self.moveCircle)
self.canvas.focus_set()
def resize(self, event):
self.canvas.configure(width = event.width-4, height = event.height-4)
def getWords(self, event):
color = self.getRandomColor()
print event.x, event.y
x = 200
y = 200
if (150, 150) < (event.x, event.y) < (250, 250):
self.canvas.delete("text")
self.canvas.create_text(x, y, text = "Mouse pointer is inside the circle",
fill = color, tags = "text")
else:
self.canvas.delete("text")
self.canvas.create_text(x, y, text = "Mouse pointer is outside the circle",
fill = color, tags = "text")
def getRandomColor(self):
""" Generates and returns a random color string such as '#c2f74a'"""
digits = "0123456789abcdef"
color = "#"
from random import randint
for i in range(6):
randomIndex = randint(0, 15)
color += digits[randomIndex]
return color
def moveCircle(self, event):
"""move circle up, down, left or right when user clicks an arrow key"""
if event.keysym == "Up":
self.canvas.move("circle", 0, -5)
self.canvas.move("text",0, -5)
self.canvas.update()
elif event.keysym == "Down":
self.canvas.move("circle", 0, 5)
self.canvas.move("text",0, 5)
self.canvas.update()
elif event.keysym == "Left":
self.canvas.move("circle", -5, 0)
self.canvas.move("text", -5, 0)
self.canvas.update()
else:
self.canvas.move("circle", 5, 0)
self.canvas.move("text", 5, 0)
self.canvas.update()
def main():
Circle().mainloop()
main()
I cant get the text to say if its in the circle or not. and i cant get the text to reprint in the center of the circle when the circle is moved.