【问题标题】:Graphics.py, I want to create three separate objects when the mouse is clickedGraphics.py,我想在鼠标点击时创建三个单独的对象
【发布时间】:2021-12-19 19:50:16
【问题描述】:

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

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

【问题讨论】:

  • 你为什么使用while True?你为什么使用continue?如果point 将具有point.x > 20 and point.y > 20:,那么它将仅首先运行if 而不是其他。
  • 通常GUI框架有方法将函数分配给even,就像mouse button press一样,然后这个函数函数只有在你点击鼠标时才会执行——它可以创建新的对象。并且需要使用一些全局变量来记住上次使用的颜色和下次使用不同的颜色。

标签: python python-3.x loops while-loop graphics


【解决方案1】:

您可以将颜色保留在列表中,并使用变量来显示当前圆圈中要使用的颜色。画完圆圈后,您可以更改索引以在下一个圆圈中使用下一种颜色。当它使用最后一种颜色时,将索引移动到 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,因为它还必须同时使用checkKey,而getMouse 会阻塞代码。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 2013-02-14
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多