我目前正在开发一个项目,它只显示一个青色球从画布顶部循环移动到画布底部。我不知道我似乎做错了什么,我是 python 和 tkinter 的初学者。感谢您提供的所有帮助。
def moveDown():
canvas.data.cy += 5
if (canvas.data.cy + canvas.data.radius \>= canvasWidth ):
canvas.data.cy = canvas.data.radius
def doTimerFired():
moveDown()
redrawAll()
def timerFired():
doTimerFired()
delay = canvas.data.delay # milliseconds
canvas.after(delay, timerFired) # pause, then call timerFired again
def redrawAll():
canvas.delete(ALL)
# draw the circle
r = canvas.data.radius
cx = canvas.data.cx
cy = canvas.data.cy
canvas.create_oval(cx-r, cy-r, cx+r, cy+r, fill="cyan")
def init():
canvas.data.radius = 10
canvas.data.cx = 10
canvas.data.cy = 14
canvas.data.delay = 100 #milliseconds
def run():
# create the root and the canvas
global canvas, canvasWidth, canvasHeight
root = Tk()
canvasWidth = 500
canvasHeight = 500
canvas = Canvas(root, width=canvasWidth, height=canvasHeight)
canvas.pack()
# Set up canvas data and call init
class Struct: pass
canvas.data = Struct()
canvas.data.canvasWidth = canvasWidth
canvas.data.canvasHeight = canvasHeight
init()
timerFired()
# and launch the app
root.mainloop() # This call BLOCKS
if __name__ == "__main__":
run()
如何让球从屏幕顶部中心落到 底部。
拼写错误
\>= canvasWidth
应该是 == canvasWidthin moveDown()
函数。
将球从屏幕顶部中心放置到底部。
在第 23 行添加值:
cx = canvas.data.cx
至:
cx = canvas.data.cx+225