Tkinter 列表框:操作行的显示(或显示一列,但来自同一数据框的“curselection”另一列)
Tkinter Listbox: manipulate display of rows (or display one column but `curselection` another from same dataframe)
我目前在 Listbox
中显示 pandas DataFrame 的一列,其中包含我需要处理的一些文件的 full path
。现在一切正常,因为我在列表框中显示了完整路径,所以在我的 on_select
中我得到了 current_selection_text
的完整路径,我可以随意处理这个项目。
def on_select(event,**kwargs):
# display element selected on list
current_selection_text=event.widget.get(event.widget.curselection())
完整路径通常很长所以我不想显示它而只显示文件名,仍然可以 select 完整路径而不显示完整路径因为它只是混乱。
我的问题是是否有办法在列表框中显示文件名,同时select显示相应的完整路径。我看到的唯一前进道路是:
- Listbox 可以提供功能来显示我填充的列表的功能。具体来说,我用
my_listbox.inset(end, x)
填充它,但在 GUI 中我想看到 f(x)
。由于似乎无法控制项目在列表框中的显示方式,因此我无法继续(准确地说:the official doc has no mention of Listbox, and the best reference I found 未提及操纵列表框的显示。
另外,我考虑尝试通过 pandastable 而不是列表框来显示 DataFrame 本身(或列的子集)。那时我可能 select 我需要的行并应用我需要的功能。这似乎是解决这个问题的一把大枪,而且我从未使用过 pandastable 所以我可能看不到前面的路障。非常欢迎提出建议(如果有必要,我愿意尝试其他 GUI 制造商,例如 pyQT)。
没有直接支持,但保持完整路径列表与文件名列表同步是微不足道的。
import tkinter as tk
from pathlib import Path
paths = [
"/tmp/foo.py",
"/tmp/bar.py",
"/user/somebody/baz.py",
]
def get_selected_path(event):
listbox = event.widget
selection = listbox.curselection()
if selection:
index = selection[0]
full_path = paths[index]
label.configure(text=full_path)
root = tk.Tk()
listbox = tk.Listbox(root)
label = tk.Label(root, anchor="w")
label.pack(side="bottom", fill="x")
listbox.pack(fill="both", expand=True)
listbox.bind("<<ListboxSelect>>", get_selected_path)
for path in paths:
name = Path(path).name
listbox.insert("end", name)
root.mainloop()
我目前在 Listbox
中显示 pandas DataFrame 的一列,其中包含我需要处理的一些文件的 full path
。现在一切正常,因为我在列表框中显示了完整路径,所以在我的 on_select
中我得到了 current_selection_text
的完整路径,我可以随意处理这个项目。
def on_select(event,**kwargs):
# display element selected on list
current_selection_text=event.widget.get(event.widget.curselection())
完整路径通常很长所以我不想显示它而只显示文件名,仍然可以 select 完整路径而不显示完整路径因为它只是混乱。
我的问题是是否有办法在列表框中显示文件名,同时select显示相应的完整路径。我看到的唯一前进道路是:
- Listbox 可以提供功能来显示我填充的列表的功能。具体来说,我用
my_listbox.inset(end, x)
填充它,但在 GUI 中我想看到f(x)
。由于似乎无法控制项目在列表框中的显示方式,因此我无法继续(准确地说:the official doc has no mention of Listbox, and the best reference I found 未提及操纵列表框的显示。
另外,我考虑尝试通过 pandastable 而不是列表框来显示 DataFrame 本身(或列的子集)。那时我可能 select 我需要的行并应用我需要的功能。这似乎是解决这个问题的一把大枪,而且我从未使用过 pandastable 所以我可能看不到前面的路障。非常欢迎提出建议(如果有必要,我愿意尝试其他 GUI 制造商,例如 pyQT)。
没有直接支持,但保持完整路径列表与文件名列表同步是微不足道的。
import tkinter as tk
from pathlib import Path
paths = [
"/tmp/foo.py",
"/tmp/bar.py",
"/user/somebody/baz.py",
]
def get_selected_path(event):
listbox = event.widget
selection = listbox.curselection()
if selection:
index = selection[0]
full_path = paths[index]
label.configure(text=full_path)
root = tk.Tk()
listbox = tk.Listbox(root)
label = tk.Label(root, anchor="w")
label.pack(side="bottom", fill="x")
listbox.pack(fill="both", expand=True)
listbox.bind("<<ListboxSelect>>", get_selected_path)
for path in paths:
name = Path(path).name
listbox.insert("end", name)
root.mainloop()