I have been fooling around in python (v3.9.10) and I have found no way to read keypresses from the enter key and have tkinter do something with that keypress. I am trying to make a dumb little program to show some numbers on my screen for school and want to be able to do it quickly. this is my code so far:
from tkinter import *
#DEFS
def submit():
submitted=entrybox.get()
display.config(text=submitted)
app=Tk()
entrybox=Entry(width=10)
entrybox.place(x=10,y=10)
#display label
display=Label(text=' ')
display.place(x=50,y=50)
display.config(font='Arial 72')
#submit button
subbutton=Button(text='SUBMIT',command=submit)
subbutton.place(x=70,y=10)
(Sorry if my code looks really bad I am quite new to python and everything it has to offer)
(I am also sorry for sounding repetitive and/or dumb. Forums such as this aren't familiar to me either)
is there any way to read key presses from the enter key while allowing the rest of my tkinter stuff to run as intended? thanks in advance.
You can use .bind() to do this like so:
app.bind('<Return>', function)
This will call function when enter is pressed. '<return>' denotes the return or enter key, and it calls function when the event of a return keypress occurs.
You could add it to your code like so:
from tkinter import *
def submit():
submitted=entrybox.get()
display.config(text=submitted)
app=Tk()
entrybox=Entry(width=10)
entrybox.place(x=10,y=10)
#display label
display=Label(text=' ')
display.place(x=50,y=50)
display.config(font='Arial 72')
#submit button
subbutton=Button(text='SUBMIT',command=submit)
subbutton.place(x=70,y=10)
app.bind('<Return>', lambda x: submit())
app.mainloop()
This will do what you are looking for...
How to bind and handle events
Use bind(sequence, func) method on your widget (like Button or even the root app Tk).
The parameter sequence is one of the predefined events, a string name for keys, mouse-events, etc.
The parameter func is a callback or handler function, given by name only (without parentheses or arguments), which must have one positional parameter for Event.
Demo
See this minimal example:
from tkinter import *
def callback(event):
print(event)
def quit(): # did we miss something ?
print("Escape was pressed. Quitting.. Bye!")
exit()
app = Tk()
app.bind('<Return>', callback) # on keypress of Enter or Return key
app.bind('<Enter>', callback) # on mouse-pointer entering the widget - here the app's root window (not confuse with Enter key of keyboard)
app.bind('<Escape>', quit) # on keypress of Escape key
app.mainloop() # to start the main loop listening for events
Prints after following keys pressed:
Enter key was pressed
Escape key was pressed
<KeyPress event keysym=Return keycode=36 char='\r' x=-583 y=-309>
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.6/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
TypeError: quit() takes 0 positional arguments but 1 was given
Note:
The Enter key was caught and bound callback/handler function (callback) invoked which prints the passed event.
The Escape key was caught and bound callback/handler function (quit) invoked. But since it hasn't the required parameter (like event) the invocation failed with a TypeError.
When adding the parameter def quit(event): it succeeds:
<Enter event focus=True x=6 y=188>
<Enter event focus=True x=182 y=45>
<KeyPress event keysym=Return keycode=36 char='\r' x=-583 y=-309>
<Enter event state=Button1 focus=True x=123 y=68>
Escape was pressed. Quitting.. Bye!
Note:
The <Enter> event is when the mouse-pointer enters the (visible) widget frame, here of the application's root window.
The <Escape> event exits the application
Further reading
RealPython: Python GUI Programming With Tkinter is a rich tutorial to learn more, especially on Using .bind().
Related
I'm trying to overwrite an existing keyboard function on the enter key with a custom hotkey. The problem is, I cannot stop the default action from occurring also. Worse yet, it occurs after the custom action, so I don't have the chance to retroactively correct it as well.
Here are the relevant parts of the code:
from tkinter import *
from tkinter import ttk
from keyboard import *
root = Tk()
root.geometry('400x300')
'''This is the problematic function called by the hotkey'''
def entr_key(text):
'''Enter key results in printing of the line to the right of cursor'''
curs_pos = text.index(INSERT)
right_hand = text.get(curs_pos,END)
right_hand = right_hand.split('\n')[:1] #lines -> strs in a list, selects the first
print (right_hand)
return
'''THIS IS THE MAIN WIDGET FOR THIS FRAME'''
text_view = ttk.Frame(root, padding=10)
text_view.grid()
text_box = Text(text_view, width=45, wrap=WORD)
text_box.grid(column=0, row=1)
text_box.insert('1.0', 'This is a Text widget demo,\nThis text is aimed at testing the enter key function')
add_hotkey("enter", lambda: entr_key(text_box))
root.mainloop()
(I've changed up some variable names to be more understandable, apologies if I missed any but it's not the source of the problem!)
I've also (unsuccesfully) tried other ways of doing this, eg:
while True:
if is_pressed('enter'):
entr_key(text_box)
'''AND ALSO'''
on_press_key("enter", lambda x=None: entr_key(text_box))
Just to be clear, I don't want the default action of enter key moving the text to a new line.
I need either a way to "break" the key event so that only the custom action takes place, or a way for the custom action to occur after default action, so I can retroactively edit it out
EDIT!!!
I've found a workaround: at the start of entr_key() I call time.sleep(0.01). During this, the default action of the enter key occurs first, and I can retroactively edit it out when the custom function resumes. The delay is slight enough to not be noticeable at all.
But still, if anyone knows how to prevent the default action from occurring completely, I would really appreciate it.
I am trying to make a simplest calculator and I want to operate it with the keyboard. This is the file.
I want that whenever I press any number button or a sign button it insert to Entry widget and it does nothing if any button other than number button or sign button is pressed. I want to put all the code in the keyboardbutton function.
Sorry for less comments in the code. And thanks in advance for any help.
First, I made a list in __init__ that matched your self.lst but where all characters are strings:
self.STR_LIST = list(map(str, self.lst))
Next, I bound keypresses to your tkinter window, so that the keyboardbutton mehtod will be called whenever a key is pressed (also in __init__):
self.window.bind("<Key>", self.keyboardbutton)
Then this should work for your keyboardbutton method:
def keyboardbutton(self, event):
if event.char in self.STR_LIST:
self.insert(event.char)
When a key is pressed, an event is sent to this method and we just simply check if it is a valid character for entry, then call your insert method.
Let me know if this works for you.
I have an entry field, and as I type into the data field, I want a method, that updates a treeview widget to be ran. Currently, I can type in a search parameter, then press a 'search' button and it will run the method to search through the treeview to find specified clients, but I want to the treeview to be updating whilst typing into the entry, not by a button press
I am unsure as to weather this is possible, if it should be doing by binding keys or if there is a way using the event loop to achieve this?
See this SO post:
TkInter keypress, keyrelease events
Essentially:
from Tkinter import *
def keyup(e):
pass;
# e.char contains the pressed key if you need that info
# use your search function here
Edit (Sorry I forgot this):
You'll need to bind the keyup function to your widget with something like:
frame.bind("<KeyRelease>", keyup) # you can also bind to a search widget
I am building a GUI application using Python and Tkinter.
I want to control the behavior of the program when the user closes it.
I've installed a new WM_DELETE_WINDOW protocol using:
root = Tk()
root.protocol("WM_DELETE_WINDOW", lambda: closes_gracefully())
This indeed is working when the user clicks the X button on the titlebar, but it is NOT working when the user presses ALT+F4.
I tried binding the key sequence: root.bind("<Alt-F4>", lambda: closes_gracefully()) but it did not work.
How can I capture the ALT+F4 event?
For this purpose, you could use atexit.register.
It works like a stack that is executed when the program gets closed. Every time you do register(function), this function gets pushed on top. If you added a, b and c they get executed in the opposite order(c, b, a).
In your case, you should do:
register(closes_gracefully)
You should note that this works almost always, except with crashes(alt-f4 works too, just tested it).
You can even use register as a decorator when the function takes no parameters:
#register
def bye():
print("I'm out!")
The code below is part of a function that produces a simple dialog with caller selected text and buttons.
The problem lies in the handling of key inputs of 's', 'a', 'd', and 'c. The code operates correctly for mouse clicks and tabbing followed by the space or enter keys. The code is written in Python 3.4 and is being tested for compliance with Windows 7.
My understanding is that Tk handles refocusing for the end user's mouse clicks. The space and enter keys are used after the user has changed focus by tabbing. For all of these interactions the keys are bound to each button by the code:
for action in ('<1>', '<space>', '<Return>'):
b.bind(action, handler)
For 'underline' key input I believe my code needs to handle the refocusing prior to calling the handler. This is the purpose of the refocus routine. The print statement ("refocussing to...") is printed with the correct value for button.winfo_name() whenever 's', 'a', 's', or 'c' is pressed . This is why I believe button.focus_set() is failing.
If it worked this would enable the handler to simply check which button was pressed by looking at event.widget.winfo_name(). As it is, the failure to refocus means that the handler is called with the wrong button in event.widget.winfo_name()
If I move focus manually by tabbing then the focus gives the name returned by event.widget.winfo_name() regardless of which of 's', 'a', 'd', or 'c' is pressed.
After reading other posts on Stack Overflow, I tried adding button.focus_force() after button.focus_set(). This had no effect on the problem.
I have tried passing the button name by changing the signature of the handler to def button_handler(event, *args) and then changing the last line of refocus to handler(event, button.winfo_name()) but *args is empty when called.
def refocus_wrapper(button):
def refocus(event):
print("refocusing to '{}'".format(button.winfo_name()))
button.focus_set()
handler(event)
return refocus
for button_text, underline, button_name in buttons:
b = ttk.Button(button_inner_frame, text=button_text, name=button_name,
underline=underline)
b.pack(padx=2, side='left')
for action in ('<1>', '<space>', '<Return>'):
b.bind(action, handler)
action = '{}'.format(button_text[underline:underline + 1].lower())
dialog.bind(action, refocus_wrapper(b))
if not default or default == button_name:
default = button_name
b.focus_set()
Your initial assumption about needing to set the focus on the button is not correct. The usual method to handle this in Tk is to bind the accelerator key event on the dialog toplevel form and call the buttons invoke method for the event binding.
In Tcl/Tk thats:
toplevel .dlg
pack [button .dlg.b -text "Save" -underline 0 -command {puts "Save pressed"}]
bind .dlg <Alt-s> {.dlg.b invoke}
So bind the key events on whatever toplevel is the parent for your buttons. If that is a dialog then its parent should be the application toplevel widget.
A python equivalent is:
from tkinter import *
main = Tk()
dialog = Toplevel(main)
button = Button(dialog, text="Send", underline="0", command=lambda: print("hello"))
button.pack()
dialog.bind('<Alt-s>', lambda event: button.invoke())
main.mainloop()
The key binding appends an event object to the callback function which we can discard using a lambda to wrap the call the the button's invoke method.
focus_set() was, of course, working perfectly. My expectation that refocusing would rewrite the event object was at fault. The following code (which has been extensively revised to incorporate Brian Oakley's suggestions) produces the expected results.:
def _make_buttons(dialog, buttons, buttonbox, default, handler):
def bcommand_wrapper(button):
def bcommand():
name = button.winfo_name()
dialog.destroy()
handler(name)
return bcommand
for button_text, underline, button_name in buttons:
b = ttk.Button(buttonbox, text=button_text, underline=underline,
name=button_name)
b.configure(command=bcommand_wrapper(b))
b.pack(padx=2, side='left')
action = button_text[underline:underline + 1].lower()
try:
dialog.bind(action, lambda event, b=b: b.invoke())
except tk.TclError:
raise ValueError(
"Invalid underline of '{}' in position {}. Character '{}'.".
format(button_text, underline, action))
b.bind('<Return>', lambda event, b=b: b.invoke())
if not default or default == button_name:
default = button_name
b.focus_set()
I gave up my original idea of returning a Tk event to the caller. This is a dialog so the caller isn't going to need anything more than the name of the button that was clicked.
Note that I am not trapping accelerator keys with the 'Alt' modifier. The 'Alt' key, at least on MS Windows, is a functional key when used with accelerator keys: It causes the display of underlines on menus. Here the underlines are static and so the use of the 'Alt' key would be inappropriate.
Might I suggest using root.bind('c', ) to just have the shortcuts perform what you want?
Just make sure to only bind them when the window pops up, and unbind them when you're done.