fork download
  1. import tkinter
  2.  
  3. key = ""
  4.  
  5. def key_down(e):
  6. global key
  7. key = e.keysym
  8.  
  9. def key_up(e):
  10. global key
  11. key = ""
  12.  
  13. cx = 400
  14. cy = 300
  15.  
  16. def main_proc():
  17. global cx,cy
  18. if key == "UP":
  19. cy = cy-20
  20. if key == "DOWN":
  21. cy = cy + 20
  22. if key == "LEFT":
  23. cx = cx - 20
  24. if key == "RIGHT":
  25. cx = cx + 20
  26.  
  27. canvas.coords("MYCHR",cx,cy)
  28. root.after(100,main_proc)
  29.  
  30.  
  31. root = tkinter.Tk()
  32. root.title("キャラクターの移動")
  33. root.bind("<KeyPress>",key_down)
  34. root.bind("<KeyRelease>",key_up)
  35.  
  36. canvas = tkinter.Canvas(width = 800,height = 600,bg = "lightgreen")
  37.  
  38. canvas.pack()
  39.  
  40.  
  41. img = tkinter.PhotoImage(file = "mimi.png")
  42.  
  43. canvas.create_image(cx,cy,image = img,tag= "MYCHR")
  44.  
  45. main_proc()
  46. root.mainloop()
Runtime error #stdin #stdout #stderr 0.12s 23468KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 1, in <module>
ModuleNotFoundError: No module named 'tkinter'