1 回答
TA贡献1842条经验 获得超12个赞
此语句返回一个元组,其中包含第二个列表框中的所有值。
listbox_events_filtered.get(0,END)
但是,您的代码还存在许多其他问题:
def select_events()也需要def select_events(event=None)
您将错误的列表框绑定到取消选择事件;它应该是listbox_events_filtered.bind('<Left>', deselect_events)
current_listin是select_events()局部变量,不影响current_list函数外部。
current_list在结束时返回select_events()什么都不做,因为你没有将函数分配给任何东西。
最后,
for item in current_list:
print(item)
只会运行一次,因为mainloop()只会循环 tkinter 事件。
考虑到所有这些,您应该将代码更改为以下内容:
from tkinter import *
from tkinter import ttk
my_window = Tk()
my_frame_in = Frame(my_window)
my_frame_in.grid(row=0, column=0)
my_frame_out = Frame(my_window)
my_frame_out.grid(row=0, column=1)
listbox_events = Listbox(my_frame_in, height='5')
listbox_events.grid(row=0, column=0, padx=10, pady=10)
listbox_events_filtered = Listbox(my_frame_out, height='5')
listbox_events_filtered.grid(row=0, column=2, padx=(0, 10), pady=10)
my_instructions = Label(my_window, text='Use arrow keys to move selected items')
my_instructions.grid(row=1, column=0, columnspan=3, pady=(0, 10))
my_list_events = ['A', 'B', 'C', 'D']
for item in my_list_events:
listbox_events.insert(END, item)
current_list = []
def select_events(event=None):
listbox_events_filtered.insert(END, listbox_events.get(ANCHOR))
listbox_events.delete(ANCHOR)
current_list.append(ANCHOR)
for item in listbox_events_filtered.get(0,END):
print(item)
def deselect_events(event=None):
listbox_events.insert(END, listbox_events_filtered.get(ANCHOR))
listbox_events_filtered.delete(ANCHOR)
for item in listbox_events_filtered.get(0,END):
print(item)
listbox_events.bind('<Right>', select_events)
listbox_events_filtered.bind('<Left>', deselect_events)
mainloop()
希望这个答案可以让你解决你的问题:)
添加回答
举报