My program has two components I want to control via the keyboard, an image that moves up/down/left/right within a canvas, and a scrollable Treeview that controls the coloring of the image.
Treeview and movable image
Currently the Left, Right, Next, and Prior keys control the image placement, and the Up and Down keys control the selected coloring in the Treeview. It works.
But what I really want is to swap the behavior of the Up key with the Prior key, and the Down key with the Next key, so I can use the arrow keys to control the image placement, and Prior/Next to do what Up/Down would normally do in the Treeview. The problem is getting the Prior and Next keys to do what the Up and Down keys would normally do in the Treeview. Is this even possible? Here's what my current code looks like:
Treeview and movable image
Currently the Left, Right, Next, and Prior keys control the image placement, and the Up and Down keys control the selected coloring in the Treeview. It works.
But what I really want is to swap the behavior of the Up key with the Prior key, and the Down key with the Next key, so I can use the arrow keys to control the image placement, and Prior/Next to do what Up/Down would normally do in the Treeview. The problem is getting the Prior and Next keys to do what the Up and Down keys would normally do in the Treeview. Is this even possible? Here's what my current code looks like:
class PartTree(ttk.Treeview): """ Using code based on discussion with boughtonp at https://www.linuxquestions.org/questions/showthread.php?p=6235877#post6235877 to not change selection when using <Up> and <Down> """ def __init__(self, parent=None): super().__init__(parent, columns=('Part1', 'Part2')) # code omitted # Force Treeview not to respond to PgDn/PgUp self.unbind_class('Treeview', '<KeyPress-Next>') self.unbind_class('Treeview', '<KeyPress-Prior>') self.unbind_class('Treeview', '<KeyPress-Next>') self.unbind_class('Treeview', '<KeyPress-Prior>') # grab focus when Up/Dn released (after highlight bar has moved) self.bind_class('Treeview', '<KeyRelease-Up>', self.set_selection) self.bind_class('Treeview', '<KeyRelease-Down>', self.set_selection) # ---------------------------------- def set_selection(self,event): """ Set focus to currently highlighted partitioning """ self.current_selection = event.widget.selection(); self.focus(self.current_selection)