-1

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.

ICW
  • 4,875
  • 5
  • 27
  • 33
  • You can set an event trigger on selection of an item in the listbox such that the `focus` returns to the main window. You have posted no code, so that is as far I can help. – James Jul 15 '18 at 22:25
  • Please create a [mcve] that illustrates the problem. – Bryan Oakley Jul 15 '18 at 23:56
  • I don't think there is a need. I'm asking if there is a way to disable a feature in a certain framework, it has nothing to do with the specific case of my code. @Bryan Oakley – ICW Jul 16 '18 at 01:17

1 Answers1

1

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685