【发布时间】:2021-02-02 16:35:00
【问题描述】:
我创建了一个列表框,我希望 Left 和 Right 箭头键(分别)上下移动列表框,但默认键绑定功能→ 和 ← 键的作用是像水平滚动一样左右移动列表。
如何更换左右键的功能?
代码看起来像这样吗:
from tkinter import *
window=Tk()
list_b=Listbox()
list_b.insert(END,'line 1')
list_b.insert(END,'line 2')
list_b.insert(END,'this is a really really really long line')
list_b.insert(END,'line 4')
list_b.grid()
list_b.disable_horizontal_scroll() #some method to disable horizontal scroll
list_b.bind('<Right>',move_down) # has the same effect as the up arrow key
list_b.bind('<Left>',move_up) # has the same effect as the down arrow key
list_b.focus()
window.mainloop()
我已经从How can I disable horizontal scrolling in a Tkinter listbox? (Python 3) 尝试过list_b.bind("<B1-Leave>", lambda event: "break"),但它并没有禁用水平滚动。
注意-
我不是要替换 ↑ 和 ↓ 键,我问这个问题是因为它是一个更大、更复杂的代码和给定 sn 的一部分-p 的代码只是一个例子。
【问题讨论】: