Python Forum
conway's game of life / Single Responsibility Principle
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
conway's game of life / Single Responsibility Principle
#5
Hi, I'm struggling with something basic
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'
Reply


Messages In This Thread
RE: conway's game of life / Single Responsibility Principle - by hanscvanleeuwen - Dec-15-2016, 10:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  While loop/half-life Miraclefruit 6 8,598 Mar-06-2017, 05:24 PM
Last Post: nilamo
  conway's game of life hanscvanleeuwen 17 18,158 Dec-09-2016, 04:05 PM
Last Post: hanscvanleeuwen
  Game of life using IDLE Informatics109 4 5,213 Oct-29-2016, 01:39 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020