Home > database >  Graphics.py, I want to create three separate objects when the mouse is clicked
Graphics.py, I want to create three separate objects when the mouse is clicked

Time:11-06

So this is what I have so far, but I wanted to make it so that, I could click in the window three times and have three different colored circles at the mouse click point.

from Graphics import *


def main():
    win = GraphWin("ball", 400, 400)
    win.setBackground('black')  
    
    while True:
        point = win.getMouse()
        if point.x > 20 and point.y > 20:             
            circ_1 = Circle(Point(point.x,point.y), 20)
            circ_1.setFill("white")
            circ_1.draw(win)
            continue        
        
        if point.x > 20 and point.y > 20:             
            circ_2 = Circle(Point(point.x,point.y), 20)
            circ_2.setFill("blue")
            circ_2.draw(win)    
            continue
        
        if point.x > 20 and point.y > 20:             
            circ_3 = Circle(Point(point.x,point.y), 20)
            circ_3.setFill("yellow")
            circ_3.draw(win)    
            continue        
       
    win.getMouse()
    win.close()    
main()

CodePudding user response:

You could keep colors on list and use variable to show which color to use in current circle. And after drawing circle you could change index to use next color in next circle. And when it use last color then move index to 0 to use again first color

from graphics import *


def main():
    colors = ['white', 'blue', 'yellow']
    color_index = 0
    
    win = GraphWin("ball", 400, 400)
    win.setBackground('black')  
    
    while True:
        point = win.getMouse()
        if point.x > 20 and point.y > 20:
            color = colors[color_index]
            print('color:', color)
            
            circ = Circle(Point(point.x,point.y), 20)
            circ.setFill(color)
            circ.draw(win)
            
            color_index  = 1
            if color_index >= len(colors):
                color_index = 0
       
    win.close()    

main()

But I wouldn't use while True loop but bind() to assign left button click to some function

from graphics import *

# global variables

colors = ['white', 'blue', 'yellow']
color_index = 0

win = None

def draw_circle(event):
    global color_index  # inform function to assign new value to global variable `color_index` instead of local variable `color_index`  because I will need this value when `mouse button` will run again this function

    print('event:', event)
    
    if event.x > 20 and event.y > 20:
        color = colors[color_index]
        print('color:', color)
        
        circ = Circle(Point(event.x, event.y), 20)
        circ.setFill(color)
        circ.draw(win)
        
        color_index  = 1
        if color_index >= len(colors):
            color_index = 0

def main():
    global win  # inform function to assign new value to global variable `win` instead of local variable `win` because I need this value in other function
    
    win = GraphWin("ball", 400, 400)
    win.setBackground('black')

    win.bind('<Button-1>', draw_circle)  # function's name without `()`
    
    win.getMouse()
    win.close()
    
main()

After checking some examples in Graphics I see it rather use while True instead of bind()

This version uses keys w, b, y to change color, and q to quit program.

It needs checkMouse instead of getMouse because it has to also use checkKey at the same time and getMouse would block code.

from graphics import *

# global variables

current_color = 'white'
win = None

def draw_circle(event):
    print('event:', event)
    
    if event.x > 20 and event.y > 20:
       
        print('color:', current_color)
        
        circ = Circle(Point(event.x, event.y), 20)
        circ.setFill(current_color)
        circ.draw(win)

def main():
    global win  # inform function to assign new value to global variable instead of local variable
    global current_color
    
    win = GraphWin("ball", 400, 400)
    win.setBackground('black')

    while True:
        point = win.checkMouse()
        if point:
            draw_circle(point)
            
        key = win.checkKey()
        if key == 'w':
            current_color = 'white'
        elif key == 'y':
            current_color = 'yellow'
        elif key == 'b':
            current_color = 'blue'

        elif key == 'q':  # quit loop
            break
        
    win.close()
    
main()
  • Related