Graphics.py,我想在点击鼠标的时候创建三个独立的对象

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

这就是我目前所拥有的,但我想做到这一点,我可以在 window 中单击三次,并在鼠标单击点处出现三个不同颜色的圆圈。

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()

您可以将颜色保留在列表中并使用变量来显示当前圆圈中使用的颜色。在绘制圆圈后,您可以更改索引以使用下一个圆圈中的下一个颜色。当它使用最后一种颜色时,将索引移至 0 以再次使用第一种颜色

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()

但我不会使用 while True 循环,而是 bind() 将左键单击分配给某些功能

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()

在查看 Graphics 中的一些示例后,我发现它宁愿使用 while True 而不是 bind()

此版本使用键wby改变颜色,q退出程序。

它需要 checkMouse 而不是 getMouse 因为它还必须同时使用 checkKeygetMouse 会阻塞代码。

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()