有人可以帮我这个忙吗?我将值从一个列表框传递到另一个列表框,然后在tkinter中再次返回,但我需要输出当前列表(右侧框中的值)。此框中的值将更改,因为它们可以移回原始框(左侧) )。我尝试使用return语句返回当前列表,但一直没有打印出任何内容
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():
listbox_events_filtered.insert(END, listbox_events.get(ANCHOR))
listbox_events.delete(ANCHOR)
current_list.append(ANCHOR)
return current_list
def deselect_events(event=None):
listbox_events.insert(END, listbox_events_filtered.get(ANCHOR))
listbox_events_filtered.delete(ANCHOR)
# ref https://effbot.org/tkinterbook/tkinter-events-and-bindings.htm
listbox_events.bind('<Right>', select_events)
listbox_events.bind('<Left>', deselect_events)
for item in current_list:
print(item)
mainloop()
答案 0 :(得分:1)
此语句返回一个元组,其中包含第二个列表框中的所有值。
listbox_events_filtered.get(0,END)
但是,您的代码还存在其他一些问题:
def select_events()
也必须是def select_events(event=None)
您将错误的列表框绑定到deselect_events;应该是listbox_events_filtered.bind('<Left>', deselect_events)
current_list
中的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()
希望这个答案可以解决您的问题:)