You can do something like this:
from Tkinter import * from random import * root=Tk() canvas=Canvas(root,width=400,height=300,bg='white') def draw(event): if event.char == ' ': canvas.delete(ALL)
Basically, you bind the drawing function to a top-level element to a <Key> binding, which is triggered whenever a key is pressed on the keyboard. Then, the event object that passed inside has a char member that contains a string representing the key that was pressed on the keyboard.
An event is fired only when the object attached to the object has focus, so I attach the draw method to the root object, since it will always be in focus.
Michael0x2a
source share