944,147 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1306
  • Python RSS
Sep 20th, 2006
0

Keybindings and Image Display

Expand Post »
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.

Python Syntax (Toggle Plain Text)
  1. from Tkinter import *
  2.  
  3. canvas = Canvas(width = 300, height = 200, bg = 'yellow')
  4. canvas.pack(expand = YES, fill = BOTH)
  5. gif1 = PhotoImage(file = 'getstart.gif')
  6. canvas.create_image(50, 10, image = gif1, anchor = NW)
  7.  
  8.  
  9. def press_a(event):
  10. print 'You Pressed a'
  11.  
  12. def press_b(event):
  13. print 'You pressed b'
  14.  
  15. def press_c(event):
  16. print 'You pressed c'
  17.  
  18. def press_d(event):
  19. print 'You pressed d'
  20.  
  21. def press_e(event):
  22. print 'You pressed e'
  23.  
  24. def press_f(event):
  25. print 'You pressed f'
  26.  
  27. def press_g(event):
  28. print 'You pressed g'
  29.  
  30. def press_h(event):
  31. print 'You pressed h'
  32.  
  33. def press_i(event):
  34. print 'You pressed i'
  35.  
  36. def press_j(event):
  37. print 'You pressed j'
  38.  
  39. def press_k(event):
  40. print 'You pressed k'
  41.  
  42. def press_l(event):
  43. print 'You pressed l'
  44.  
  45. def press_m(event):
  46. print 'You pressed m'
  47.  
  48. def press_n(event):
  49. print 'You pressed n'
  50.  
  51. def press_o(event):
  52. print 'You pressed o'
  53.  
  54. def press_p(event):
  55. print 'You pressed p'
  56.  
  57. def press_q(event):
  58. print 'You pressed q'
  59.  
  60. def press_r(event):
  61. print 'You pressed r'
  62.  
  63. def press_s(event):
  64. print 'You pressed s'
  65.  
  66. def press_t(event):
  67. print 'You pressed t'
  68.  
  69. def press_u(event):
  70. print 'You pressed u'
  71.  
  72. def press_v(event):
  73. print 'You pressed v'
  74.  
  75. def press_w(event):
  76. print 'You pressed w'
  77.  
  78. def press_x(event):
  79. print 'You pressed x'
  80.  
  81. def press_y(event):
  82. print 'You pressed y'
  83.  
  84. def press_z(event):
  85. print 'You pressed z'
  86.  
  87. def press_1(event):
  88. print 'You pressed 1'
  89.  
  90. def press_enter(event):
  91. print 'You pressed enter'
  92.  
  93. root = Tk()
  94. frame = Frame(root, width=500,height=500)
  95. Tkinter.Label(frame, image='c:\blah.bmp')
  96.  
  97. root.bind('a', press_a)
  98. root.bind('b', press_b)
  99. root.bind('c', press_c)
  100. root.bind('d', press_d)
  101. root.bind('e', press_e)
  102. root.bind('f', press_f)
  103. root.bind('g', press_g)
  104. root.bind('h', press_h)
  105. root.bind('i', press_i)
  106. root.bind('j', press_j)
  107. root.bind('k', press_k)
  108. root.bind('l', press_l)
  109. root.bind('m', press_m)
  110. root.bind('n', press_n)
  111. root.bind('o', press_o)
  112. root.bind('p', press_p)
  113. root.bind('q', press_q)
  114. root.bind('r', press_r)
  115. root.bind('s', press_s)
  116. root.bind('t', press_t)
  117. root.bind('u', press_u)
  118. root.bind('v', press_v)
  119. root.bind('w', press_w)
  120. root.bind('x', press_x)
  121. root.bind('y', press_y)
  122. root.bind('z', press_z)
  123. root.bind('1', press_1)
  124. root.bind('<Enter>', press_enter)
  125.  
  126. frame.pack()
  127.  
  128. canvas.mainloop()
  129. root.mainloop()
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
whisper is offline Offline
3 posts
since Sep 2006
Sep 21st, 2006
0

Re: Keybindings and Image Display

Ouch! This is not an easy project for a newbie!
I corrected some things in your code to make it work at least ...
Python Syntax (Toggle Plain Text)
  1. from Tkinter import *
  2.  
  3. def press_a(event):
  4. # you are mixing console and GUI
  5. #print 'You Pressed a'
  6. # try this ...
  7. root.title('You Pressed a')
  8.  
  9. def press_b(event):
  10. print 'You pressed b'
  11.  
  12. def press_c(event):
  13. print 'You pressed c'
  14.  
  15. def press_d(event):
  16. print 'You pressed d'
  17.  
  18. def press_e(event):
  19. print 'You pressed e'
  20.  
  21. def press_f(event):
  22. print 'You pressed f'
  23.  
  24. def press_g(event):
  25. print 'You pressed g'
  26.  
  27. def press_h(event):
  28. print 'You pressed h'
  29.  
  30. def press_i(event):
  31. print 'You pressed i'
  32.  
  33. def press_j(event):
  34. print 'You pressed j'
  35.  
  36. def press_k(event):
  37. print 'You pressed k'
  38.  
  39. def press_l(event):
  40. print 'You pressed l'
  41.  
  42. def press_m(event):
  43. print 'You pressed m'
  44.  
  45. def press_n(event):
  46. print 'You pressed n'
  47.  
  48. def press_o(event):
  49. print 'You pressed o'
  50.  
  51. def press_p(event):
  52. print 'You pressed p'
  53.  
  54. def press_q(event):
  55. print 'You pressed q'
  56.  
  57. def press_r(event):
  58. print 'You pressed r'
  59.  
  60. def press_s(event):
  61. print 'You pressed s'
  62.  
  63. def press_t(event):
  64. print 'You pressed t'
  65.  
  66. def press_u(event):
  67. print 'You pressed u'
  68.  
  69. def press_v(event):
  70. print 'You pressed v'
  71.  
  72. def press_w(event):
  73. print 'You pressed w'
  74.  
  75. def press_x(event):
  76. print 'You pressed x'
  77.  
  78. def press_y(event):
  79. print 'You pressed y'
  80.  
  81. def press_z(event):
  82. print 'You pressed z'
  83.  
  84. def press_1(event):
  85. print 'You pressed 1'
  86.  
  87. def press_enter(event):
  88. print 'You pressed enter'
  89.  
  90. root = Tk()
  91.  
  92. canvas = Canvas(width = 300, height = 200, bg = 'yellow')
  93. canvas.pack(expand = YES, fill = BOTH)
  94. #gif1 = PhotoImage(file = 'getstart.gif')
  95. # temporary change, used gif file I had ...
  96. gif1 = PhotoImage(file = 'Green100x100.GIF')
  97. canvas.create_image(50, 10, image = gif1, anchor = NW)
  98.  
  99. frame = Frame(root, width=500, height=500)
  100. frame.pack()
  101. #Tkinter.Label(frame, image='c:\blah.bmp') # this won't work!!!!!
  102. Label(frame, text="Something ........").pack()
  103.  
  104. root.bind('a', press_a)
  105. root.bind('b', press_b)
  106. root.bind('c', press_c)
  107. root.bind('d', press_d)
  108. root.bind('e', press_e)
  109. root.bind('f', press_f)
  110. root.bind('g', press_g)
  111. root.bind('h', press_h)
  112. root.bind('i', press_i)
  113. root.bind('j', press_j)
  114. root.bind('k', press_k)
  115. root.bind('l', press_l)
  116. root.bind('m', press_m)
  117. root.bind('n', press_n)
  118. root.bind('o', press_o)
  119. root.bind('p', press_p)
  120. root.bind('q', press_q)
  121. root.bind('r', press_r)
  122. root.bind('s', press_s)
  123. root.bind('t', press_t)
  124. root.bind('u', press_u)
  125. root.bind('v', press_v)
  126. root.bind('w', press_w)
  127. root.bind('x', press_x)
  128. root.bind('y', press_y)
  129. root.bind('z', press_z)
  130. root.bind('1', press_1)
  131. root.bind('<Enter>', press_enter)
  132.  
  133. #canvas.mainloop() # not needed!!!!
  134. root.mainloop()
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Sep 21st, 2006
0

Re: Keybindings and Image Display

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:

Python Syntax (Toggle Plain Text)
  1. def press_b(event):
  2. root.title('You pressed b')
  3. gif1 = PhotoImage(file = '2.gif')
  4. 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
whisper is offline Offline
3 posts
since Sep 2006
Sep 21st, 2006
0

Re: Keybindings and Image Display

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)
  1. # ...
  2.  
  3. def press_b(event):
  4. root.title('You Pressed b')
  5. # gif_b created outside of function
  6. canvas.create_image(50, 10, image = gif_b, anchor = NW)
  7.  
  8. # ...
  9.  
  10. # load/create all your picture objects first
  11. gif_b = PhotoImage(file = 'b.GIF')
Last edited by vegaseat; Sep 21st, 2006 at 9:44 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Sep 22nd, 2006
0

Re: Keybindings and Image Display

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
whisper is offline Offline
3 posts
since Sep 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Canvas and Layered Images
Next Thread in Python Forum Timeline: Click event >> window appears?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC