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



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

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,因为它还必须同时使用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()

相关内容

  • 没有找到相关文章

最新更新