Python Forum
[Tkinter] [Solved]Detecting key press in gui
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] [Solved]Detecting key press in gui
#1
I am having trouble with this code:
from tkinter import *

root = Tk()

def key(event):
    kp = repr(event.char)
    print ("pressed", kp) #repr(event.char))
    if (kp == 'x'):
        print ("pressed x", repr(event.char))
def callback(event):
    frame.focus_set()
    print ("clicked at", event.x, event.y)

frame = Frame(root, width=100, height=100)
frame.bind("<Key>", key)
frame.bind("<Button-1>", callback)
frame.pack()

root.mainloop()
which is a modification of code from here:
 
Quote:http://effbot.org/tkinterbook/tkinter-ev...ndings.htm

The mod is the bit which I hoped would allow me to pick out a specific key so I could respond accordingly but it is ignored so I guess it's some sort of error.
Can you point me in the right direction please?
Reply
#2
works for me
results:
Output:
clicked at 68 60 pressed ' ' pressed 'd' pressed 'd'
that's because I clicked in the window which makes it the widget in focus.
if you issue a focus command on the Frame, you should be OK.
You may also have to do a
.update_idletasks()
on the frame, the following from http://infohost.nmt.edu/tcc/help/pubs/tk...ersal.html
Quote: w.update_idletasks()

Some tasks in updating the display, such as resizing and redrawing widgets, are called idle tasks because they are usually deferred until the application has finished handling events and has gone back to the main loop to wait for new events.

If you want to force the display to be updated before the application next idles, call the w.update_idletasks() method on any widget.
Reply
#3
Thanks but I did click in the window.
Running my code and pressing the 'x' key gives the following:
Quote:clicked at 73 43
pressed 'v'
pressed '1'
pressed '2'
pressed ''
pressed '1' 
So my little 'if' test is being ignored

Okay, I have found the answer here:
http://infohost.nmt.edu/tcc/help/pubs/tk...names.html
So my 'if' test is now
if(event.keycode == 82): # This time I chose to test pressing 1 on the keyboard
    print ("pressed 1", repr(event.char))
Although I can't guess how the keycodes have been assigned Smile
Reply
#4
(Dec-01-2017, 04:19 PM)Barrowman Wrote: Although I can't guess how the keycodes have been assigned Smile

Here's the table of keysums I found with a little searching:

http://www.tcl.tk/man/tcl8.4/TkCmd/keysyms.htm
Reply
#5
Big Grin 
This works too (note use "'x'" instead of 'x')

def key(event):
    kp = repr(event.char)
    print ("pressed", kp) #repr(event.char))
    if (kp == "'x'"):
        print ("pressed x", repr(event.char))
Pressing 'x' outputs:
Output:
pressed 'x' pressed x 'x'
Reply
#6
richstimson - Please be aware that you are responding to a post from 2017.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Detecting Buttons - Suggestions? RockBlok 3 696 Nov-29-2023, 11:54 AM
Last Post: RockBlok

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020