Python 当graphics.py对象到达窗口边缘时关闭窗口
关于,我希望在Python 当graphics.py对象到达窗口边缘时关闭窗口,python,zelle-graphics,Python,Zelle Graphics,关于,我希望在圆圈对象到达窗口边缘且看不见后立即关闭图形 以下代码创建并移动圆: win = GraphWin("My Circle", 100, 100) c = Circle(Point(50,50), 10) c.draw(win) for i in range(40): c.move(30, 0) #speed=30 time.sleep(1) #c should move until the end of the windows
圆圈
对象到达窗口边缘且看不见后立即关闭图形
以下代码创建并移动圆:
win = GraphWin("My Circle", 100, 100)
c = Circle(Point(50,50), 10)
c.draw(win)
for i in range(40):
c.move(30, 0) #speed=30
time.sleep(1)
#c should move until the end of the windows(100),
win.close() # then windows of title "My Circle" should close immediately
有什么方法可以代替使用range
并计算其确切的“步数”吗?将圆圈左侧的x位置与窗口右侧进行比较:
from graphics import *
WIDTH, HEIGHT = 300, 300
RADIUS = 10
SPEED = 30
win = GraphWin("My Circle", WIDTH, HEIGHT)
c = Circle(Point(50, 50), RADIUS)
c.draw(win)
while c.getCenter().x - RADIUS < WIDTH:
c.move(SPEED, 0)
time.sleep(1)
win.close() # then windows of title "My Circle" should close immediately
你能用文字解释一下这行代码的作用吗而c.getCenter().x-RADIUS
@KimSuYu,在word格式中,“当圆的左边缘(圆的中心x位置减去圆的半径)位于窗口右边缘(宽度)的左侧时,执行以下操作:”,其中“following”是“以速度单位向右移动圆,然后在继续之前睡眠一秒钟。”然后假设使用的是图像
对象,而不是圆
对象。getAnchor()
的工作方式是否与getCenter()
相同?如果它是一个图像
对象,您建议如何获取对象的最左侧位置,以将其与窗口的宽度进行比较?@KimSuYu,我用图像对象实现补充了我的答案。
from graphics import *
WIDTH, HEIGHT = 300, 300
SPEED = 30
win = GraphWin("My Image", WIDTH, HEIGHT)
image = Image(Point(50, 50), "file.gif")
image.draw(win)
image_half_width = image.getWidth() / 2
while image.getAnchor().x - image_half_width < WIDTH:
image.move(SPEED, 0)
time.sleep(1)
win.close() # the window of title "My Image" should close immediately