所以這就是我到目前為止所擁有的,但我想做到這一點,我可以在視窗中單擊三次,并在滑鼠單擊點處顯示三個不同顏色的圓圈。
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()
uj5u.com熱心網友回復:
您可以將顏色保留在串列中,并使用變數來顯示當前圓圈中使用的顏色。繪制圓圈后,您可以更改索引以使用下一個圓圈中的下一種顏色。當它使用最后一種顏色時,將索引移動到 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()
此版本使用w、b、鍵y來改變顏色和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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/349905.html
標籤:Python 蟒蛇-3.x 循环 while 循环 图形
上一篇:從日志中回傳最頻繁的IP地址
