1

How do I make something move on a canvas with 2 keys? Before anybody tells me that I did not do some research, I did. The reason I am still asking this is because I do not know what they are talking about. The people are talking about mini states and commands I do not know about.

from tkinter import *

def move(x,y):
    canvas.move(box,x,y)

def moveKeys(event):
    key=event.keysym
    if key =='Up':
        move(0,-10)
    elif key =='Down':
    move(0,10)
    elif key=='Left':
    move(-10,0)
    elif key=='Right':
        move(10,0)

window =Tk()
window.title('Test')

canvas=Canvas(window, height=500, width=500)
canvas.pack()

box=canvas.create_rectangle(50,50,60,60, fill='blue')
canvas.bind_all('<Key>',moveKeys)

Is there any way I can get 2 keys to move at once? I would like it to be done using this format, and not with mini states.

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
  • 1
    You can not do it using the format you specified. An event handler in tkinter will always respond to a single key press. If the constraint is that you can't use a "mini state", then you can't do it. – Bryan Oakley May 19 '17 at 16:35

1 Answers1

2

If the "mini state" method you are talking about refers to this answer (Python bind - allow multiple keys to be pressed simultaneously), it is actually not that difficult to understand.

See below a modified & commented version of your code which follows that philosophy:

from tkinter import tk

window = tk.Tk()
window.title('Test')

canvas = tk.Canvas(window, height=500, width=500)
canvas.pack()

box = canvas.create_rectangle(50, 50, 60, 60, fill='blue')

def move(x, y):
    canvas.move(box, x, y)

# This dictionary stores the current pressed status of the (← ↑ → ↓) keys
# (pressed: True, released: False) and will be modified by Pressing or Releasing each key
pressedStatus = {"Up": False, "Down": False, "Left": False, "Right": False}

def pressed(event):
    # When the key "event.keysym" is pressed, set its pressed status to True
    pressedStatus[event.keysym] = True

def released(event):
    # When the key "event.keysym" is released, set its pressed status to False
    pressedStatus[event.keysym] = False

def set_bindings():
    # Bind the (← ↑ → ↓) keys's Press and Release events
    for char in ["Up", "Down", "Left", "Right"]:
        window.bind("<KeyPress-%s>" % char, pressed)
        window.bind("<KeyRelease-%s>" % char, released)

def animate():
    # For each of the (← ↑ → ↓) keys currently being pressed (if pressedStatus[key] = True)
    # move in the corresponding direction
    if pressedStatus["Up"] == True: move(0, -10)
    if pressedStatus["Down"] == True: move(0, 10)
    if pressedStatus["Left"] == True: move(-10, 0)
    if pressedStatus["Right"] == True: move(10, 0)
    canvas.update()
    # This method calls itself again and again after a delay (80 ms in this case)
    window.after(80, animate)

# Bind the (← ↑ → ↓) keys's Press and Release events
set_bindings()

# Start the animation loop
animate()

# Launch the window
window.mainloop()
Community
  • 1
  • 1
Josselin
  • 2,593
  • 2
  • 22
  • 35
  • Do you expect a 13 year old's brain to be able to process that. – 21harrisont May 30 '17 at 02:11
  • Well, you could start by running the code to see if it does what you want. Then experiment with it changing one line at a time until you are happy with the result :) – Josselin Jun 01 '17 at 09:00