Hello there, I am trying to write a script for an old 133mhz laptop I have, that will bind every key to different functions, and on a key press a different image, or video will be displayed, and/or a sound file. I have figured out how to bind keys with tkinter, and I have figured out how to display images on a canvas, providing I keep the code seperate, but when I put them together it doesn't work. I would like the image and/or movie to show up on the main tkinter window that starts up, as I will have this fullscreen. Any suggestions would be GREATLY appreciated, as I am really sick right now and spent a good deal of the night trying to figure this out. I am a newbie to python too, which should be obvious. :)

The functions were just defined for testing, so I know where to put code later on.

from Tkinter import *

canvas = Canvas(width = 300, height = 200, bg = 'yellow')
canvas.pack(expand = YES, fill = BOTH)
gif1 = PhotoImage(file = 'getstart.gif')
canvas.create_image(50, 10, image = gif1, anchor = NW)


def press_a(event):
    print 'You Pressed a'  

def press_b(event):
    print 'You pressed b'

def press_c(event):
    print 'You pressed c'

def press_d(event):
    print 'You pressed d'

def press_e(event):
    print 'You pressed e'

def press_f(event):
    print 'You pressed f'

def press_g(event):
    print 'You pressed g'

def press_h(event):
    print 'You pressed h'

def press_i(event):
    print 'You pressed i'

def press_j(event):
    print 'You pressed j'

def press_k(event):
    print 'You pressed k'

def press_l(event):
    print 'You pressed l'

def press_m(event):
    print 'You pressed m'

def press_n(event):
    print 'You pressed n'

def press_o(event):
    print 'You pressed o'

def press_p(event):
    print 'You pressed p'

def press_q(event):
    print 'You pressed q'

def press_r(event):
    print 'You pressed r'

def press_s(event):
    print 'You pressed s'

def press_t(event):
    print 'You pressed t'

def press_u(event):
    print 'You pressed u'

def press_v(event):
    print 'You pressed v'

def press_w(event):
    print 'You pressed w'

def press_x(event):
    print 'You pressed x'

def press_y(event):
    print 'You pressed y'

def press_z(event):
    print 'You pressed z'

def press_1(event):
    print 'You pressed 1'

def press_enter(event):
    print 'You pressed enter'

root = Tk()
frame = Frame(root, width=500,height=500)
Tkinter.Label(frame, image='c:\blah.bmp')

root.bind('a', press_a)
root.bind('b', press_b)
root.bind('c', press_c)
root.bind('d', press_d)
root.bind('e', press_e)
root.bind('f', press_f)
root.bind('g', press_g)
root.bind('h', press_h)
root.bind('i', press_i)
root.bind('j', press_j)
root.bind('k', press_k)
root.bind('l', press_l)
root.bind('m', press_m)
root.bind('n', press_n)
root.bind('o', press_o)
root.bind('p', press_p)
root.bind('q', press_q)
root.bind('r', press_r)
root.bind('s', press_s)
root.bind('t', press_t)
root.bind('u', press_u)
root.bind('v', press_v)
root.bind('w', press_w)
root.bind('x', press_x)
root.bind('y', press_y)
root.bind('z', press_z)
root.bind('1', press_1)
root.bind('<Enter>', press_enter)

frame.pack()

canvas.mainloop()
root.mainloop()

Ouch! This is not an easy project for a newbie!
I corrected some things in your code to make it work at least ...

from Tkinter import *

def press_a(event):
    # you are mixing console and GUI
    #print 'You Pressed a'
    # try this ...
    root.title('You Pressed a')

def press_b(event):
    print 'You pressed b'

def press_c(event):
    print 'You pressed c'

def press_d(event):
    print 'You pressed d'

def press_e(event):
    print 'You pressed e'

def press_f(event):
    print 'You pressed f'

def press_g(event):
    print 'You pressed g'

def press_h(event):
    print 'You pressed h'

def press_i(event):
    print 'You pressed i'

def press_j(event):
    print 'You pressed j'

def press_k(event):
    print 'You pressed k'

def press_l(event):
    print 'You pressed l'

def press_m(event):
    print 'You pressed m'

def press_n(event):
    print 'You pressed n'

def press_o(event):
    print 'You pressed o'

def press_p(event):
    print 'You pressed p'

def press_q(event):
    print 'You pressed q'

def press_r(event):
    print 'You pressed r'

def press_s(event):
    print 'You pressed s'

def press_t(event):
    print 'You pressed t'

def press_u(event):
    print 'You pressed u'

def press_v(event):
    print 'You pressed v'

def press_w(event):
    print 'You pressed w'

def press_x(event):
    print 'You pressed x'

def press_y(event):
    print 'You pressed y'

def press_z(event):
    print 'You pressed z'

def press_1(event):
    print 'You pressed 1'

def press_enter(event):
    print 'You pressed enter'

root = Tk()

canvas = Canvas(width = 300, height = 200, bg = 'yellow')
canvas.pack(expand = YES, fill = BOTH)
#gif1 = PhotoImage(file = 'getstart.gif')
# temporary change, used gif file I had ...
gif1 = PhotoImage(file = 'Green100x100.GIF')
canvas.create_image(50, 10, image = gif1, anchor = NW)

frame = Frame(root, width=500, height=500)
frame.pack()
#Tkinter.Label(frame, image='c:\blah.bmp')  # this won't work!!!!!
Label(frame, text="Something ........").pack()

root.bind('a', press_a)
root.bind('b', press_b)
root.bind('c', press_c)
root.bind('d', press_d)
root.bind('e', press_e)
root.bind('f', press_f)
root.bind('g', press_g)
root.bind('h', press_h)
root.bind('i', press_i)
root.bind('j', press_j)
root.bind('k', press_k)
root.bind('l', press_l)
root.bind('m', press_m)
root.bind('n', press_n)
root.bind('o', press_o)
root.bind('p', press_p)
root.bind('q', press_q)
root.bind('r', press_r)
root.bind('s', press_s)
root.bind('t', press_t)
root.bind('u', press_u)
root.bind('v', press_v)
root.bind('w', press_w)
root.bind('x', press_x)
root.bind('y', press_y)
root.bind('z', press_z)
root.bind('1', press_1)
root.bind('<Enter>', press_enter)

#canvas.mainloop()  # not needed!!!!
root.mainloop()

Thank you your editing helped me get a better grasp of what was going on, yes this is extremely hard for a newbie, but i'm close to accomplishing what I want and that feels good. :)

One thing I can't figure out though, is how to repopulate the canvas with a different image, for each key press. I tried changing the press_b function to:

def press_b(event):
    root.title('You pressed b')
    gif1 = PhotoImage(file = '2.gif')
    root.canvas.create_image(50, 10, image = gif1, anchor = NW)

thinking that maybe this would update the canvas in my frame, but instead it just shows "you pressed b". I need almost all the functions to update the image shown to a new image, because he will see different pictures on each key press, but for some of the keys I am going to launch a sound file, or play a video with windows media player.

Again thank you very much for your help, it makes a little more sense now then how I had it written before. Any other advice would be GREATLY appreciated.

It is best to load/create all your image objects outside of the function, otherwise you have to load the file everytime any key is pressed, could lead to a timing problem ...

# ...

def press_b(event):
    root.title('You Pressed b')
    # gif_b created outside of function
    canvas.create_image(50, 10, image = gif_b, anchor = NW)

# ...

# load/create all your picture objects first
gif_b = PhotoImage(file = 'b.GIF')

There we go, now i'm making a bit of headway...
I'll give it a go for a while on my own, and try to see if I can get something together and atleast somewhat tested, thanks a lot for your help, you've given me a lot so far.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.