The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

285786 (1) [Avatar] Offline
#1
Thanks for an excellent publication. What is the best call method to start/initialize the Tag program in chapter 38. My syntax likely is wrong, but my program reports 0 errors but will not show any canvas objects. Is there a loop call to be made? thank you
Ana Bell (33) [Avatar] Offline
#2
I think I see the problem. In Anaconda, if you go to Tools -> Preferences -> iPython Console -> Graphics tab, the Graphics backend might be "Inline" or something other than "tkinter". For me, it is "tkinter". If you change the backend, you'll have to restart Anaconda for the change to take effect.

The following lines take care of creating the window and the canvas, and should have it show up automatically if your backend is "tkinter":

window = tkinter.Tk()
window.geometry("800x800")
window.title("Tag!")
canvas = tkinter.Canvas(window)
canvas.pack(expand=1, fill='both')

player1 = Player(canvas, "yellow")
player2 = Player(canvas, "blue")
canvas.bind_all('<Key>', handle_key)


If your backend is NOT tkinter, then you should have the same lines, plus one more line at the end to window.mainloop():

window = tkinter.Tk()
window.geometry("800x800")
window.title("Tag!")
canvas = tkinter.Canvas(window)
canvas.pack(expand=1, fill='both')

player1 = Player(canvas, "yellow")
player2 = Player(canvas, "blue")
canvas.bind_all('<Key>', handle_key)
window.mainloop()



The following code should be what you end up with after following through the entire Lesson 38, and with the extra call to window.mainloop():

import tkinter
import random

class Player(object):
    def __init__(self, canvas, color):
        size = random.randint(1,100)
        x1 = random.randint(100,700)
        y1 = random.randint(100,700)
        x2 = x1+size
        y2 = y1+size
        self.color = color
        self.coords = [x1, y1, x2, y2]
        self.piece = canvas.create_rectangle(self.coords, tags=color)
        canvas.itemconfig(self.piece, fill=color)
    def move(self, direction):
        if direction == 'u':
            self.coords[1] -= 10
            self.coords[3] -= 10
            canvas.coords(self.piece, self.coords)
        if direction == 'd':
            self.coords[1] += 10
            self.coords[3] += 10
            canvas.coords(self.piece, self.coords)
        if direction == 'l':
            self.coords[0] -= 10
            self.coords[2] -= 10
            canvas.coords(self.piece, self.coords)
        if direction == 'r':
            self.coords[0] += 10
            self.coords[2] += 10
            canvas.coords(self.piece, self.coords)

def handle_key(event):
    if event.char == 'w' :
        player1.move("u")
    if event.char == 's' :
        player1.move("d")
    if event.char == 'a' :
        player1.move("l")
    if event.char == 'd' :
        player1.move("r")
    if event.char == 'i' :
        player2.move("u")
    if event.char == 'k' :
        player2.move("d")
    if event.char == 'j' :
        player2.move("l")
    if event.char == 'l' :
        player2.move("r")
    yellow_xy = canvas.bbox(1)
    overlapping = canvas.find_overlapping(yellow_xy[0],yellow_xy[1],yellow_xy[2],yellow_xy[3])
    if 2 in overlapping:
        canvas.create_text(100,100,font=("Arial",20),text="Tag!")

window = tkinter.Tk()
window.geometry("800x800")
window.title("Tag!")
canvas = tkinter.Canvas(window)
canvas.pack(expand=1, fill='both')

player1 = Player(canvas, "yellow")
player2 = Player(canvas, "blue")
canvas.bind_all('<Key>', handle_key)
window.mainloop()