我正在開發我的軟體的一項功能,該功能采用我已系結到視窗的鍵盤,如下所示
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 保存資訊,例如從哪里觸發,哪個按鈕觸發了它等等。
如果條件驗證此事件是否由鍵盤上的按鈕觸發,其中包含az之間的值
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
非常感謝你的幫助
uj5u.com熱心網友回復:
此時,呼叫self.listBox.curselection(0)會向該方法發送兩個引數:對類 Listbox (self.listBox) 的實體的參考和整數值 (0)。你的意思是self.listBox.curselection()不是?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/369265.html
