0

我对编程很陌生,我刚刚编写了这个小代码来在画布中移动一个球。它工作得很好,除了“顶部”和“底部”按钮没有按预期工作;他们做的与他们应该做的完全相反!我道歉,但经过一个小时的头痛后,我就是无法理解。谢谢你的帮助。

from tkinter import *

x1, y1 = 135, 135

def moveo (lr, tb):
    global x1, y1
    x1, y1 = x1+lr, y1+tb
    can.coords (oval, x1, y1, x1+30, y1+30)

def moveLeft ():
    moveo (-10,0)

def moveRight ():
    moveo (10,0)

def moveTop ():
    moveo (0,10)

def moveBottom ():
    moveo (0,-10)


##########MAIN############

wind = Tk()
wind.title ("Move Da Ball")

can = Canvas (wind, width = 300, height = 300, bg = "light blue")
can.pack (side = LEFT,padx = 5, pady = 5)
oval = can.create_oval(x1,y1,x1+30,y1+30,width=2,fill='orange')
Button(wind, text = 'Left', command=moveLeft).pack(padx = 5, pady = 5)
Button(wind, text = 'Right', command=moveRight).pack(padx = 5, pady = 5)
Button(wind, text = 'Top', command=moveTop).pack(padx = 5, pady = 5)
Button(wind, text = 'Bottom', command=moveBottom).pack(padx = 5, pady = 5)
Button(wind, text = 'Quit', command=wind.destroy).pack(padx = 5, pady = 5)


wind.mainloop()
4

1 回答 1

5

原点 (0, 0) 位于屏幕的左上角。向右走时,x 轴增加,向下走时,y 轴增加。

在此处输入图像描述

于 2013-08-08T22:48:48.323 回答