How could I call function by clicking in the graphics window. I currently have a draw eyes function but i want to be able to call it by clicking on the graphics window and call 30 eyes in the same colour sequence of blue, green and brown? so i could plot the eyes on chosen centre points of the graphics window?

from graphics import *

eye_colours = set("""green brown blue""".strip().split())

def drawCircle(win, centre, radius, colour):
    circle = Circle(centre, radius)
    circle.setFill(colour)
    circle.setWidth(2)
    circle.draw(win)

def eyePicker():
    while True:
      colour = raw_input("Please enter the colour: ").strip()
      colour = colour.lower()
      if colour in eye_colours:
        return colour
      else:
        print("Invalid colour '{0}'.".format(colour))
        print("Possible values are {0}".format(tuple(eye_colours)))
    


def drawEye():
    w = 250
    h = 250
    win = GraphWin("Eye", w, h)
    try:
      # center should be at 1/2 width and height
      centre = Point(w//2, h//2)
      colour = eyePicker()
      drawCircle(win, centre, 40, colour)
      drawCircle(win, centre, 20, colour)
      drawCircle(win, centre, 10, colour)
      win.getMouse()
    finally:
      win.close()

drawEye()

def eyesEverywhere():
    w = 500
    h = 500

Recommended Answers

All 6 Replies

You could do something simple like this ...

from graphics import *
import random

def drawCircle(win, centre, radius, colour):
    circle = Circle(centre, radius)
    circle.setFill(colour)
    circle.setWidth(2)
    circle.draw(win)

def drawEye(win, centre):
    drawCircle(win, centre, 40, "green")
    drawCircle(win, centre, 20, "brown")
    drawCircle(win, centre, 10, "blue")

def createWin():
    w = 250
    h = 250
    win = GraphWin("Click Spooky Eyes", w, h)
    while True:
        # center should be at 1/2 width and height
        # but give it a random offset
        x = random.randint(-w//2, w//2)
        y = random.randint(-h//2, h//2)
        centre = Point(w//2+x, h//2+y)
        drawEye(win, centre)
        # click the mouse for next eye
        win.getMouse()

createWin()

I need to warn you that very few people use the Zelle module graphics and it only works with Python2.

You could do something simple like this ...

from graphics import *
import random

def drawCircle(win, centre, radius, colour):
    circle = Circle(centre, radius)
    circle.setFill(colour)
    circle.setWidth(2)
    circle.draw(win)

def drawEye(win, centre):
    drawCircle(win, centre, 40, "green")
    drawCircle(win, centre, 20, "brown")
    drawCircle(win, centre, 10, "blue")

def createWin():
    w = 250
    h = 250
    win = GraphWin("Click Spooky Eyes", w, h)
    while True:
        # center should be at 1/2 width and height
        # but give it a random offset
        x = random.randint(-w//2, w//2)
        y = random.randint(-h//2, h//2)
        centre = Point(w//2+x, h//2+y)
        drawEye(win, centre)
        # click the mouse for next eye
        win.getMouse()

createWin()

I need to warn you that very few people use the Zelle module graphics and it only works with Python2.

thx for this, but is there is any possible code i could do to make the eye appear where is click?

thx

thx for this, but is there is any possible code i could do to make the eye appear where is click?

thx

I am sure somewhere in the graphics.py documentation is a function to get the x, y position of the mouse click.

In Tkinter it is tied to the click event via event.x and event.y

Also struggling with the same problem.

I need to allow the user to draw up 30 eyes, with the centre of each eye being defined by a click of the mouse (through using getX and getY). All eyes should be of radius 40, but they should be of
different colours: specifically, the colours should repeatedly cycle
through the sequence "blue", "green" and "brown".

This function should call upon:

def drawEye():
    win = GraphWin("Eye", 250, 250)
    centre = Point(w//2, h//2)
    colour = eyePicker()
    drawCircle(win, centre, 40, colour)
    drawCircle(win, centre, 20, colour)
    drawCircle(win, centre, 10, colour)
    win.getMouse() 
    win.close()
    drawEye()

Presumably by using a loop?

Figured the loop out.

for i in range (10):
        centre = win.getMouse()
        drawEye(win, centre, 40, "blue")
        centre = win.getMouse()
        drawEye(win, centre, 40, "green")
        centre = win.getMouse()
        drawEye(win, centre, 40, "brown")

Figured the loop out.

for i in range (10):
        centre = win.getMouse()
        drawEye(win, centre, 40, "blue")
        centre = win.getMouse()
        drawEye(win, centre, 40, "green")
        centre = win.getMouse()
        drawEye(win, centre, 40, "brown")

Ah, simple enough, so ...

mouse_position = win.getMouse()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.