繁体   English   中英

Python,使用箭头键移动对象

[英]Python, moving object with arrow keys

我需要使用 Python 创建一个对象(特别是椭圆形),该对象可以自行移动或使用户能够使用箭头键移动对象。 我需要使用这两个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) 

好的,这就是我需要的。

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) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM