Dec-15-2016, 10:04 PM
Hi, I'm struggling with something basic
In the part where I use the movement keys to "draw" squares"
If I use bind_keys() , I get Type error: missing 1 required positional argument: 'canvas'
In the part where I use the movement keys to "draw" squares"
from tkinter import * window = Tk() canvas = Canvas(window, width=1200, height=900,bd=0, highlightthickness=0) canvas.pack() text_ = Text(window, height=3, width=30) text_.pack() text_.insert(END, "To MOVE use arrow keys. Press x to seed. ") moves={"Up":(0, -50),"Down":(0,50), "Left":(-50,0), "Right":(50,0)} sq = canvas.create_rectangle(50, 50, 100, 100, fill="grey") def movesquare(event): x,y=moves[event.keysym] canvas.move(1,x,y) def otherkey(event): keypressed = event.keysym if keypressed == "x": Square(canvas.coords(1)) class Square: def __init__(self,x1y1x2y2): self.x1y1x2y2=x1y1x2y2 canvas.create_rectangle(self.x1y1x2y2) bindings = {"<KeyPress-Up>" : movesquare, "<KeyPress-Down>": movesquare, "<KeyPress-Left>": movesquare, "<KeyPress-Right>": movesquare, "x": otherkey} for binding in bindings: canvas.bind_all(binding, bindings[binding]) mainloop()The suggestion was to replace lines 24-28 with:
def bind_keys(canvas): """Bind all appropriate keys to the correct function.""" bindings = {"<keypress-up>" : movesquare, "<keypress-down>": movesquare, "<keypress-left>": movesquare, "<keypress-right>": movesquare, "x": otherkey, "f": otherkey, "r": otherkey} for binding in bindings: canvas.bind_all(binding, bindings[binding])How do you actually invoke the bind_key in the main?
If I use bind_keys() , I get Type error: missing 1 required positional argument: 'canvas'