[英]How to move a item between two listboxes with dynamic btn and listbox with Tkinter?
我對GUI和Tkinter知識的了解已經走到了盡頭。
題:
我想以“動態”方式在列表框之間移動項目。
我在Create_Widget()
函數中定義了一些這樣的列表框。 在這種情況下5
。
for lbIdx in range(1:6):
self.listbox[lbIdx] = tk.Listbox(self.center, selectmode='multiple', height=25,
width=40, exportselection=0)
我還創建了一些出現在列表框之間的按鈕:
# dynamic buttons
for btn_rID in range(1,6):
self.btn_right[btn_rID] = tk.Button(self.center, text = '>>',
command=lambda i=btn_rID: self.move_item_right(i))
for btn_lID in range(1,6):
self.btn_left[btn_lID] = tk.Button(self.center, text = '<<',
command= lambda i=btn_lID: self.move_item_left(i))
這些按鈕使用了類似的函數move_item_right()
和move_item_left()
。
當我想使用以下代碼向右或向左移動時出現問題:
def move_item_right(self, btn_ID):
# btn_ID holds the information of which button_idx was pressed
idxToMove = self.listbox[btn_ID].curselection()
itemToMove = self.listbox[btn_ID].selection_get()
self.listbox[btn_ID+1].insert(idxToMove,itemToMove)
self.listbox[btn_ID].delete(idxToMove)
我收到此錯誤:
File "C:\Python38\lib\tkinter\__init__.py", line 968, in selection_get
return self.tk.call(('selection', 'get') + self._options(kw))
_tkinter.TclError: PRIMARY selection doesn't exist or form "STRING" not defined
在我開始使用動態按鈕定義之前,它運行良好。 但是后來我確實為每個按鈕調用編寫了一個函數。
誰能解釋這個奇怪的錯誤? 當我使用調試器時,調試器會在
itemToMove = self.listbox[btn_ID].selection_get()
我自己在列表框定義中使用錯字exportselection=0
實現了錯誤。
沒有它,它運行良好..
# solution
for lbIdx in range(1:6):
self.listbox[lbIdx] = tk.Listbox(self.center, selectmode='multiple', height=25,
width=40)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.