python_tkinter_canvas画布移动小球


from tkinter import *

def ballMove(event):
    if event.keysym == 'Left':
        canvas.move(1,-5,0)
    if event.keysym == 'Right':
        canvas.move(1,5,0)
    if event.keysym == 'Up':
        canvas.move(1,0,-5)
    if event.keysym == 'Down':
        canvas.move(1,0,5)

tk = Tk()
canvas = Canvas(tk,width=500, height= 300)
canvas.pack()
canvas.create_oval(225,125,275,175,fill="yellow",outline="lightgray")
canvas.bind_all('<KeyPress-Left>',ballMove)
canvas.bind_all('<KeyPress-Right>',ballMove)
canvas.bind_all('<KeyPress-Up>',ballMove)
canvas.bind_all('<KeyPress-Down>',ballMove)

tk.mainloop()

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM