Home > other >  Getting an Invalid Syntax error using tkinter in python IDLE Shell
Getting an Invalid Syntax error using tkinter in python IDLE Shell

Time:02-01

Heres the code, please help

from tkinter import *
tk = Tk()
canvas = Canvas(tk, width=400, height=400)
canvas.pack()
canvas.create_polygon(10, 10, 10, 60, 50, 35)
1
def movetriangle(event):
    canvas.move(1, 5, 0)
Canvas.bind_all('<KeyPress-Return>', movetriangle)
SyntaxError: invalid syntax

Its highlighting the C in Canvas.bind_all as invalid syntax. I'm trying to get the triangle to move when enter is pressed. Any help or alternate code would be appreciated. Im guessing the code needs altering for Canvas.bind_all to work or something needs to be imported for it to work.

CodePudding user response:

You are using the class name Canvas instead of the variable name canvas. Also you must call tk.mainloop() to actually have your actions executed. So try this:

from tkinter import *
tk = Tk()
canvas = Canvas(tk, width=400, height=400)
canvas.pack()
canvas.create_polygon(10, 10, 10, 60, 50, 35)

def movetriangle(event):
    canvas.move(1, 5, 0)
canvas.bind_all('<KeyPress-Return>', movetriangle)
tk.mainloop()
  •  Tags:  
  • Related