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