Tkinter Listbox - How to disable arrow key selection? - python

I have a Python 3.7 tkinter GUI, and within the GUI I have implemented up-down arrow key controls for the main part of the application. Next to it I have a list box that also controls the application but in a different way, and by default AFTER a listbox selection has been made the listbox selection will scroll with up and down arrows. So, after I've used the list box in the app, the arrow key triggers an up arrow event in both the main part of the application and in the list box. This triggers my application to respond in the change in list box selection by loading new data into the main app. This is obviously unacceptable.
How can I disable the arrow key controls feature of tkinter's
ListBox?
I've tried configuring the listbox to not take focus, but this doesn't seem to disable the feature.
Edit:
I solved this by binding the list box's FocusIn event to a function that immediately focus's something else. This is far from ideal, as the code now focus's in then changes focus for no reason. If there is a way to disable focus on a widget completely or disable the list box key bindings that would be a preferred solution.
from tkinter import *
class App:
def __init__(self):
self.root = Tk()
self.dummy_widget = Label()
self.lb = ListBox(master=self.root)
self.lb.bind("<FocusIn>", lambda event: self.dummy_widget.focus())
# Additional setup and packing widgets...
if __name__ == '__main__':
mainloop()
This seems very "hacky", although it does the job perfectly.

How can I disable the arrow key controls feature of tkinter's ListBox?
Create your own bindings for the events you want to override, in the widget in which you want them overridden. Do anything you want in that function (including nothing), and then return the string break, which is the documented way to prevent events from being processed any further.
For a more extensive description of how bindings work, see this answer to the question Basic query regarding bindtags in tkinter. It describes how a character is inserted into an entry widget, but the mechanism is identical for all events and widgets in tkinter.

Related

tkinter popup and text processing for autocomplete

I'm making autocomplete feature for a text editor in tkinter & python.
Currently the process of autocomplete is:
If there is a input like the one in a dictionary of autocomplete,call popup.
I do it via t_start.bind("< Key >", asprint) where asprint is my popup function.
I can escape the popup via escape button or by clicking elsewhere.
What I want is - upon user pressing any text key - re-trigger popup again, narrowing search in the autocomplete.
F->FI->FIL->FILE
sort of thing. I don't know what to use to get that input, AFTER the popup is open. How do I get 2nd and every following input character?
The popup function is:
def popup(event):
selected_text=''
try:
selected_text=t_start.get("sel.first", "sel.last")
except TclError:
for i in range(len(selected_text)):
if selected_text[i:0]==word[i:0]:
menu.add_command(label="%s" %selected_text, command=insert_word)
menu.delete(0)
else:
pass
menu.tk_popup(event.x_root, event.y_root)
The key is to keep the keyboard focus in your entry widget. When you popup your window, make sure the focus stays (or is returned to) the entry widget. Any events that affect the popup need to be attached to the entry widget rather than the popup window.
However, if you're using a menu as your popup, this will be impossible. A menu is the wrong choice for an auto-complete feature because it steals all events until the menu is dismissed. Your popup needs to be an instance of a Toplevel widget (if you want it to "float") or some other widget (listbox, text, canvas, etc) if you want it embedded inside your window.
There is a recipe on ActiveState that gives an example of doing autocomplete using an embedded window. http://code.activestate.com/recipes/578253-an-entry-with-autocompletion-for-the-tkinter-gui/

Key Event Handling using Tkinter in Python

check the following link out :
[PyObjC Key Event Handling Question] Key Events Handling using PyObjC in Mac OS X
This was my initial question. I somehow managed to find a built-in plugin to solve the Key Event Management, but using Python. It is called Tkinter.
from Tkinter import *
root = Tk()
def screenshot(*ignore): os.system("screencapture -s %s" % check_snapshot)
root.bind('<Return>', greet)
root.mainloop( )
On pressing return (enter) key, it would successfully call screenshot function, and it would work.
Now, what I am looking for is, whenever I press combination of keys, like Command+Shift+4, the above function should be call.
This should be done in the same manner for Command+Shift+3 and Command+Shift+5 as well.
This should be done by checking which combination of keys are pressed, and accordingly, their respective screenshot functions should be called.
Also, this app shortcuts shouldn't be just relied on this app's window or frame, the window / frame of this window shouldn't be visible, yet, the shortcuts should work and trigger their respective functions.
root.withdraw()
This is the built-in function which hides the Tkinter window, but then, I am unable to invoke any of the functions. These functions only work on Tkinter window, or else, keys shortcuts don't work.
Any help would be appreciated.
Tkinter events only work when the tkinter window has focus,and for it to have focus it must be visible. You cannot use tkinter to handle events while another program is in the foreground.
The format of an event is <modifier-modifier-event-detail>, with modifier and event being optional. Event is something like KeyPress, ButtonPress, ButtonRelease and so on. Detail gives more detail, such as which key, or which button. For example, <ButtonRelease-1> is for releasing mouse button 1 (one).
Modifier is where you specify control, alt, delete or shift, and you can have more than one. Shift is a bit special, because it is often interpreted by the OS before tkinter ever sees is. So, for example, "Shift-3" on an American English keyboard is "#". Thus, instead of <Shift-3> you would use <#>.
Putting that all together, command-shift-3 would be <Command-#>. However, if you do that on a Mac, it will intercept that event and do a screenshot, so the binding will only work on Windows and Linux. On each OS there are a few key bindings you cannot override.
The best description of the format to use for specifying events is the tcl/tk man page on bind. Even though you're asking about tkinter, the underlying engine is tcl/tk.

How do I stop gtk.Entry from losing focus on arrow keypress?

I'm writing a Python GTK app that uses a gtk.Entry widget to forward keypresses to multiple other widgets. Unfortunately, it seems like GTK has a default binding that switches widget focus on the up and down arrow keys out of the Entry widget.
I want to disconnect this key-based focus switching, but leave mouse based focus switching in place. I can't seem to find where in the API I'd do this.
Any suggestions?
Return True from a handler for the key-press-event signal to prevent the default handling: http://www.pygtk.org/docs/pygtk/class-gtkwidget.html#signal-gtkwidget--key-press-event

Qt - Temporarily disable all events or window functionality?

I have a Qt program with many buttons, user-interactable widgets, etc.
At one stage in the program, I would like all the widgets to temporarily 'stop working'; stop behaving to mouse clicks and instead pass the event on to one function.
(This is so the User can select a widget to perform meta operations. Part explanation here: Get variable name of Qt Widget (for use in Stylesheet)? )
The User would pick a widget (to do stuff with) by clicking it, and of course clicking a button must not cause the button's bound function to run.
What is the correct (most abstracted, sensible) method of doing this?
(which doesn't involve too much new code. ie; not subclassing every widget)
Is there anything in Qt designed for this?
So far, I am able to retrieve a list of all the widgets in the program (by calling
QObject.findChildren(QtGui.QWidget)
so the solution can incorporate this.
My current horrible ideas are;
Some how dealing with all the applications events all the time in one
function and not letting through the events when I need the
application to be dormant.
When I need dormancy, make a new transparent widget which recieves
mouse clicks and stretch it over the entire window. Take coordinates
of click and figure out the widget underneath.
Somehow create a new 'shell' instance of the window.
THANKS!
(Sorry for the terrible write-up; in a slight rush)
python 2.7.2
PyQt4
Windows 7
You can intercept events send to specific widgets with QObject::installEventFilter.
graphite answered this one first so give credit where credit is due.
For an actual example in PySide, here's an example you might draw some useful code from:
my_app.py
from KeyPressEater import KeyPressEater
if __name__ == "__main__":
app = QApplication(sys.argv)
eater = KeyPressEater()
app.installEventFilter(eater)
KeyPressEater.py
class KeyPressEater(QObject):
# subclassing for eventFilter
def eventFilter(self, obj, event):
if self.ignore_input:
# swallow events
pass
else:
# bubble events
return QObject.eventFilter(self,obj,event)

Tkinter: Listbox separators, disabled items, keyboard navigation?

I'm studying the Tkinter Listbox widget and have been unable to find solutions for the following functionality:
How can I create non-selectable horizontal separator items, eg. separators equivalent to the Tkinter Menu widget's .add_separator()? (Using chars like dashes and underscores looks awful).
How can I disable a specific item? I tried using .itemconfig( index, state='disabled' ) without success.
How can I enable keyboard navigation, eg. when a user's keyboard input automatically scrolls one forward to the closest item that begins with the text the user typed? Must I bind(<KeyPress>, ...) and manage this behavior myself?
Would some of the above functionality be easier to implement using a Text widget or the ttk.Treeview widget?
you cannot. The widget doesn't support that.
you can't disable certain items, the widget doesn't support a state attribute. That being said, you can monitor the selection and do the appropriate thing if the user selects something that is disabled, and use the item foreground to denote disabled-ness.
You will need to bind to keypress events and manage the behavior yourself. It's not particularly difficult, just a little tedious.
the text widget might be your best bet, though you'll have to add bindings to mimic the default bindings of the listbox.
Bottom line: Tkinter provides nothing that directly supports what you want to do, but the building blocks are all there. You'll just have to build it yourself.

Categories

Resources