【发布时间】:2019-09-14 23:09:42
【问题描述】:
我一直试图弄清楚我需要做什么才能使用键盘在树视图小部件中选择多行。我已经尝试了所有不同的绑定,但我似乎无法让任何东西正常工作。 看来我的电话没有任何效果。
我已经提供了我用来测试的代码,我一定是遗漏了什么!
from tkinter import *
from tkinter.ttk import Treeview
class App(Frame):
def __init__(self, parent):
super().__init__()
self.container = Frame.__init__(self, parent)
self.tv = None
self.tree()
def shift_down(event):
_widget = event.widget
_focus = event.widget.focus()
_widget.selection_add(_focus)
print(_focus)
self.tv.bind('<Shift-Down>', lambda e: shift_down(e))
def tree(self):
tv = self.tv = Treeview(self.container)
tv.grid(sticky='NSEW')
tv.insert('', '0', 'item1', text='Item 1', tags='row')
tv.insert('', '1', 'item2', text='Item 2', tags='row')
tv.insert('', '2', 'item3', text='Item 3', tags='row')
tv.insert('item1', '0', 'python1', text='Python 1')
tv.insert('item1', '1', 'python2', text='Python 2')
tv.insert('python1', '0', 'sub1', text='Sub item 1')
tv.insert('python1', '1', 'sub2', text='Sub item 2')
def main():
root = Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
App(root)
root.mainloop()
if __name__ == '__main__':
main()
【问题讨论】:
-
我查看了您提供的链接,但对于我想要完成的工作没有任何帮助。我希望能够在按住 shift 键并按下 up 和 down 键的同时选择多行。