| | |
Keybindings and Image Display
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2006
Posts: 3
Reputation:
Solved Threads: 0
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.

The functions were just defined for testing, so I know where to put code later on.
Python Syntax (Toggle Plain Text)
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 ...
I corrected some things in your code to make it work at least ...
Python Syntax (Toggle Plain Text)
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()
May 'the Google' be with you!
•
•
Join Date: Sep 2006
Posts: 3
Reputation:
Solved Threads: 0
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:
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.
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:
Python Syntax (Toggle Plain Text)
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 ...
Python Syntax (Toggle Plain Text)
# ... 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')
Last edited by vegaseat; Sep 21st, 2006 at 9:44 pm.
May 'the Google' be with you!
![]() |
Similar Threads
- how to display one image per day from an array? (JSP)
- Image Swap Problems (JavaScript / DHTML / AJAX)
- Help with SWAP IMAGE (Site Layout and Usability)
- url image prefix (HTML and CSS)
- how can i output more than one image?? (PHP)
- How to display TIFF file on web page? (PHP)
Other Threads in the Python Forum
- Previous Thread: Canvas and Layered Images
- Next Thread: 450 bugs consumed by Python
Views: 1167 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Python
anti array avogadro beginner builtin clear client code color count csv curved def dictionary dynamic enter examples excel file float format frange ftp function gui heads hints homework import input java lapse line lines linux list lists loop microcontroller mouse multiple mysqldb mysqlquery newb number numbers output parsing path port prime program programming projects py2exe pygame pyopengl pyqt python random raw_input recursion recursive redirect script scrolledtext singleton software sqlite ssh stderr string strings subprocess sum syntax table terminal text thread threading time tkinter tlapse tooltip tuple tutorial twoup ubuntu unicode unix urllib urllib2 variable web-scrape wikipedia windows word wx.wizard wxpython






