【发布时间】:2022-01-05 13:53:11
【问题描述】:
我正在开发我的软件的一项功能,该功能采用我已绑定到窗口的键盘,如下所示
win.bind('<Key>', self.triggerBackButton)。这是回调函数。
class Navigator(Validator):
def __init__(self, win, renameFrameOBJ):
self.win = win
self.renameFrameOBJ = renameFrameOBJ
self.child_window = None
# Initializing List Frame to display file names
listFrame = Frame(win, bg='#222222')
listFrame.grid(row=1, column=0, sticky='nsew')
Grid.columnconfigure(listFrame, 0, weight=1)
listFrame.update()
width = listFrame.winfo_width() // 16
self.listBox = Listbox(listFrame, width=width, height= 20,justify='center', font=('Flux Regular', 15),
fg='white', selectbackground='#375a7f', selectforeground='white', bg='#222222', bd = 0,
activestyle='none', highlightthickness=0)
self.listBox.grid(ipadx=10, ipady=10)
win.bind('<Key>', self.triggerBackButton)
def triggerBackButton(self, event):
if event.char in string.ascii_lowercase:
files = os.listdir(self.path_history[-1])
for idx in range(len(files)):
if files[idx][0].lower() == event.char:
self.listBox.curselection(0)
self.listBox.activate(idx)
break
上述函数的逻辑是这样的。它将 self 和 event 作为输入,其中 event 保存诸如触发位置、触发它的按钮等信息。
如果条件验证此事件是否由键盘上的按钮触发,包含 a-z
之间的值self.path_history 存储文件的路径,所以这里我从 self.path_history 变量中获取最后一个路径,并将该路径中存在的所有文件存储到文件变量。
我正在遍历 files 列表并检查该文件的第一个字母 是否等于 event.char 包含来自 az 的字母强>。如果是这种情况,那么我正在尝试访问 self.listBox 小部件并使用 Listbox 小部件的 curselection() 方法。 但是我收到以下错误。我已经多次使用这个 self.listBox.curselection() inbuild 方法,但这一次它给了我错误
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.8/tkinter/__init__.py", line 1892, in __call__
return self.func(*args)
File "/media/hackytech/Local Disk1/data/python/Project/Bunch File Renamer LINUX/packages/navigator.py", line 104, in triggerBackButton
self.listBox.curselection(0)
TypeError: curselection() takes 1 positional argument but 2 were given
非常感谢您的帮助
【问题讨论】: