1

so I have a submarine:

from tkinter import *
HEIGHT = 500
WIDTH = 800
window = Tk()
window.title("Bubble Blaster")
c = Canvas(window, width=WIDTH, height=HEIGHT, bg="darkblue")
c.pack()
ship_id = c.create_polygon(5, 5, 5, 25, 30, 15, fill="red")
ship_id2 = c.create_oval(0, 0, 30, 30, outline="red")  

and Im reading a book on how to make the sub move, this is what it says:

def move_ship(event):
    if event.keysym == "up":
        c.move(ship_id, 0, -ship_spd)
        c.move(ship_id2, 0, -ship_spd)
    elif event.keysym == "Down":
        c.move(ship_id, 0, ship_spd)
        c.move(ship_id2, 0, ship_spd)
    elif event.keysym == "Left":
        c.move(ship_id, -ship_spd, 0)
        c.move(ship_id2,  -ship_spd, 0)
    elif event.keysym == "Right":
        c.move(ship_id, ship_spd, 0)
        c.move(ship_id2,  ship_spd, 0)
    c.bind_all('<key', move_ship)

When I run it, it gives me an error:
PS. Im doing it in a snippet because Ctrl+v wont do all of the message

<h4 style="color: red">Traceback (most recent call last):
  File "C:\Users\Bloxy Craft\Desktop\Bubble Blaster.py", line 28, in &lt;module&gt;
    c.bind_all('&lt;key&gt;', move_ship)
  File "C:\Users\Bloxy Craft\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1257, in bind_all
    return self._bind(('bind', 'all'), sequence, func, add, 0)
  File "C:\Users\Bloxy Craft\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1200, in _bind
    self.tk.call(what + (sequence, cmd))
_tkinter.TclError: bad event type or keysym "key"</h4>
Can someone help me?
Thanks!
- Bloxy Craft

2
  • btw the book is called, Help your kids with computer coding Commented Aug 24, 2017 at 23:24
  • <key> is not the same as <Key>. You need to use <Key> Commented Aug 25, 2017 at 0:03

1 Answer 1

1

Like Bryan Oakley said you need to replace '<key' with '<Key>'. Also you need to replace if event.keysym == "up": with if event.keysym == "Up": in your code (U in 'Up' must be capital). Hole code is:

from tkinter import *
HEIGHT = 500
WIDTH = 800
window = Tk()
window.title("Bubble Blaster")
c = Canvas(window, width=WIDTH, height=HEIGHT, bg="darkblue")
c.pack()
ship_id = c.create_polygon(5, 5, 5, 25, 30, 15, fill="red")
ship_id2 = c.create_oval(0, 0, 30, 30, outline="red")

def move_ship(event):
    if event.keysym == "Up":
        c.move(ship_id, 0, -ship_spd)
        c.move(ship_id2, 0, -ship_spd)
    elif event.keysym == "Down":
        c.move(ship_id, 0, ship_spd)
        c.move(ship_id2, 0, ship_spd)
    elif event.keysym == "Left":
        c.move(ship_id, -ship_spd, 0)
        c.move(ship_id2,  -ship_spd, 0)
    elif event.keysym == "Right":
        c.move(ship_id, ship_spd, 0)
        c.move(ship_id2,  ship_spd, 0)

c.bind_all('<Key>', move_ship)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.