I need to create an object (specifically oval) using Python that moves on its own or enables the user to move the object with arrow keys. I need to do so using these two def
.
import tkinter
def motion():
if m==1:
can.move (id, 0,-5)
elif m==3:
can.move (id,0, 5)
elif m==0:
can.move (id,5, 0)
else:
can.move (id, - 5,0)
can.after (50, motion)
def arrows (event):
global m
if event.keysym=='Up':
m==1
elif event.keysym=='Down':
m==3
elif event.keysym=='Right':
m==0
else:
m==2
can=tkinter.Canvas (width=800,height=800)
can.pack ()
id=can.create_oval (100,100,150,150)
can.bind ('<Button-1>',motion)
can.bind_all ('<Key>', arrows)
Okay, so here is what I needed.
import tkinter
def motion():
if m==1:
can.move (id, 0,-5)
elif m==3:
can.move (id,0, 5)
elif m==0:
can.move (id,5, 0)
else:
can.move (id, - 5,0)
can.after (50, motion)
def arrows (event):
global m
if event.keysym=='Up':
m=1
elif event.keysym=='Down':
m=3
elif event.keysym=='Right':
m=0
else:
m=2
can=tkinter.Canvas (width=800,height=800)
can.pack ()
id=can.create_oval (100,100,150,150)
m=3
motion ()
can bind ('<Button-1>',motion)
can.bind_all ('<Key>', arrows)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.