Python Canvas object.coords倒置

时间:2013-08-08 22:32:32

标签: python tkinter

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

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()

1 个答案:

答案 0 :(得分:4)

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

enter image description here