# Import the library for Marty to function and to connect the Raspberry Pi to Marty from martypy import Marty # Import the library needed to bind keys to actions, for Marty import tkinter as tk marty = Marty('exp', '/dev/ttyAMA0') # This prepares Marty for movement by properly aligning his legs and feet marty.get_ready() # Prepare an event callback function associating actions with key presses def remote_key(event): # Use the values that are understood by the Tkinter library, convert any letter keystrokes to lowercase key_press = event.keysym.lower() # Carry out actions depending on the key pressed if key_press == 'w': print('Marty walks two steps forward') marty.walk() elif key_press == 'a': print('Marty walks two steps left') marty.sidestep('left') elif key_press == 's': print('Marty walks two steps backward') # Makes use of the documentation, naming the parameter and giving it a value; without the parameter, this would not work properly marty.walk(step_length=-25) elif key_press == 'd': print('Marty walks two steps right') marty.sidestep('right') elif key_press == 'q': print('Marty turns 15 degrees left') marty.walk(2, 'auto', 15) elif key_press == 'e': print('Marty turns 15 degrees right') marty.walk(2, 'auto', -15) elif key_press == 'z': print('Marty stands straight') marty.stand_straight() elif key_press == 'c': print('Marty dances') marty.dance() elif key_press == 'x': print('Marty plays excited sound') marty.play_sound('excited') # Create a window that will receive the keypress keys = tk.Tk() # Send the key to the remote_key function keys.bind_all('', remote_key) # Starts and loops remote_key function keys.mainloop()