0

I am trying to use graphics.py to make simple graphics that can be used as buttons. Though I know how to do this by individual drawing of each object and association to a letter or number, I am using a loop to generate the objects. I am lost on how to go about associating each object created with a specific letter or number.

Below is some code that I have been working on to attempt to find a solution to this problem. The loop functions work to create graphics as well as to detect mouse clicks in them. Where I am lost is how to assign each member of the list a specific assignment of a letter or number. The code below generates three circles that I would like to associate with letters A,B,C respectively so that when clicked, they draw the letter above the graphic.

from graphics import*
import string
def main():
    window = GraphWin("Window", 400, 400)
    circles1 = keys(window)
    # line here to call upon function that draws labels over
    # the circles using ascii string removed for relevance
    # test for clicking circles
    while True:
        clickPoint=window.checkMouse()
        if clickPoint != None:
            for i in circles1:
                if mouse(clickPoint, i) == True:
                    i.undraw()
                    # i.undraw used to test while loop


def keys(window):
    # Define circles as an empty list
    circles = []
    x = 50
    for i in range(3):
        x += 50
        circle = Circle(Point(0+x, 100), 20)
        circle.setFill("black")
        circles.append(circle)
        circle.draw(window)
    return circles



def mouse(point, circle):
   x = point.getX()
   y = point.getY()
   radius = circle.getRadius()
   center = circle.getCenter()
   cx = center.getX()
   cy = center.getY()
   distance=(((cx-x)**2)+((cy-y)**2))**0.5
   if (distance < radius):
       return True
    else:
        return False

Ideally, clicking on a button would draw the letter it is assigned to above it. Thank you for any help

ec2604
  • 501
  • 3
  • 11

1 Answers1

0

You can iter over string.ascii_lowercase Now, you return an array, but it looks like more pythonic to use generator

from graphics import*
import string

def keys(window):
    for letter, index in enumerate(string.ascii_lowercase):
        x = 50*(index+1)
        circle=Circle(Point(0+x,100),20)
        circle.setFill("black")
        # Do what you need with Letter
        circle.draw(window)
        yield circle

If this code is confusing, I kindly suggest you document yourself about:

  • Generator and yield keyword (some resource here)
  • enumerate function (you can check here)

With those two stuff in mind, you just need to now string.ascii_lowercase will return abcdefghijklmnopqrstuvwxyz. I'll let you guess what string.ascii_uppercase returns

Julien Kieffer
  • 1,116
  • 6
  • 16
  • I understand the thought process behind this but I am getting a value error for the line "for letter, index in string.ascii_lowercase:" " ValueError: not enough values to unpack (expected 2, got 1)" – LoveMyYard Oct 23 '19 at 06:21
  • Forgot the `enumerate` on the for loop.... Response has been edited – Julien Kieffer Oct 23 '19 at 06:22