将列表框的选定变量的索引存储在列表中
Store the indices of the selected variables of a listbox in a list
我想在“索引”列表中保存列表框中 selected 变量的所有索引。任何解决方案?目前我 select 一个变量,它 returns 它对我的索引。我无法 select 多个变量并将所有索引保存在列表中。
import tkinter as tk
from tkinter.constants import RIGHT, VERTICAL, Y
Ch_Output = ["a","b","c","d","e","f"]
root = tk.Tk()
root.title('Seleziona canale')
root.geometry("400x500")
var_select = str()
indice = int()
def getElement(event):
selection = event.widget.curselection()
index = selection[0]
value = event.widget.get(index)
result.set(value)
print('\n',index,' -> ',value)
global var_select
global indice
var_select = value
indice = index
#root.destroy()
# scroll bar-------
result =tk.StringVar()
my_frame = tk.Frame(root)
my_scrollbar = tk.Scrollbar(my_frame, orient=VERTICAL)
var2 = tk.StringVar()
var2.set(Ch_Output)
# list box-----------
my_listbox = tk.Listbox(my_frame, width=50, height=20, listvariable=var2)
# configure scrolbar
my_scrollbar.config(command=my_listbox.yview)
my_scrollbar.pack(side=RIGHT, fill=Y)
my_frame.pack()
my_listbox.pack(pady=15)
my_listbox.bind('<<ListboxSelect>>', getElement) #Select click
root.mainloop()
print('\n',indice)
默认情况下,您只能select 一项。如果您想一次 select 多个项目,您需要将 selectmode
指定为 tk.MULTIPLE
或 tk.EXTENDED
.
你的情况:
import tkinter as tk
from tkinter.constants import RIGHT, VERTICAL, Y, MULTIPLE
...
selected_index = []
def getElement(event):
global selected_index
selected_index = list(event.widget.curselection())
print(selected_index)
print("Values: ", [event.widget.get(x) for x in selected_index])
...
my_listbox = tk.Listbox(my_frame, width=50, height=20, listvariable=var2, selectmode=MULTIPLE)
...
root.mainloop()
我想在“索引”列表中保存列表框中 selected 变量的所有索引。任何解决方案?目前我 select 一个变量,它 returns 它对我的索引。我无法 select 多个变量并将所有索引保存在列表中。
import tkinter as tk
from tkinter.constants import RIGHT, VERTICAL, Y
Ch_Output = ["a","b","c","d","e","f"]
root = tk.Tk()
root.title('Seleziona canale')
root.geometry("400x500")
var_select = str()
indice = int()
def getElement(event):
selection = event.widget.curselection()
index = selection[0]
value = event.widget.get(index)
result.set(value)
print('\n',index,' -> ',value)
global var_select
global indice
var_select = value
indice = index
#root.destroy()
# scroll bar-------
result =tk.StringVar()
my_frame = tk.Frame(root)
my_scrollbar = tk.Scrollbar(my_frame, orient=VERTICAL)
var2 = tk.StringVar()
var2.set(Ch_Output)
# list box-----------
my_listbox = tk.Listbox(my_frame, width=50, height=20, listvariable=var2)
# configure scrolbar
my_scrollbar.config(command=my_listbox.yview)
my_scrollbar.pack(side=RIGHT, fill=Y)
my_frame.pack()
my_listbox.pack(pady=15)
my_listbox.bind('<<ListboxSelect>>', getElement) #Select click
root.mainloop()
print('\n',indice)
默认情况下,您只能select 一项。如果您想一次 select 多个项目,您需要将 selectmode
指定为 tk.MULTIPLE
或 tk.EXTENDED
.
你的情况:
import tkinter as tk
from tkinter.constants import RIGHT, VERTICAL, Y, MULTIPLE
...
selected_index = []
def getElement(event):
global selected_index
selected_index = list(event.widget.curselection())
print(selected_index)
print("Values: ", [event.widget.get(x) for x in selected_index])
...
my_listbox = tk.Listbox(my_frame, width=50, height=20, listvariable=var2, selectmode=MULTIPLE)
...
root.mainloop()