如何维护完整文件路径但仅在可排序的 python 列表框中显示基本名称?

问题描述 投票:0回答:1

我试图只在 python 列表框中显示文件的基名,但以某种方式维护完整的文件路径。我知道如何只显示基本文件名,但问题是列表框是可排序的,当我尝试执行程序的下一步(将文件打印为 pdf)时,它无法在列表中找到文件,因为它们没有完整的文件路径。如果我在列表框中显示完整的文件路径,它可以工作,但我更愿意只看到文件名。

代码如下。我删除了大部分功能,只专注于我所要求的内容。 下面的代码将打开一个 tk 框。单击“单独排序”按钮。选择任何图像文件。在新窗口中单击“保存列表”。其目标是打印图像的原始文件路径,但它当前打印程序所在的路径。如果您注释/取消注释标记行,它将起作用,但列表将显示完整的文件路径。

import tkinter as tk
from tkinter import *
from tkinter import filedialog
import os

ws = Tk()
ws.title('Select Files to Import')
ws.geometry('500x200')
ws.config(bg='#456')

f = ('sans-serif', 13)
btn_font = ('sans-serif', 10)
bgcolor = '#BF5517'

def sort():
    global second
    second = Toplevel()
    second.geometry('800x400')
    menubar=Menu(second)
    menubar.add_command(label="Save List", command=save)
    second.config(menu=menubar)
    global listbox
    listbox = Drag_and_Drop_Listbox(second)
    listbox.pack(fill=tk.BOTH, expand=True)
    directory = filedialog.askopenfilenames()
    n = 0
    for file in directory:        
        listbox.insert(n, os.path.basename(file))   ####comment
        #listbox.insert(n, file)                    ####uncomment
        n=n+1
 
def save():
  image=listbox.get(0, listbox.size())
  for x in image:
      print(os.path.abspath(x))

class Drag_and_Drop_Listbox(tk.Listbox):
  def __init__(self, master, **kw):
    kw['selectmode'] = tk.SINGLE
    tk.Listbox.__init__(self, master, kw)
    self.bind('<Button-1>', self.getState, add='+')
    self.bind('<Button-1>', self.setCurrent, add='+')
    self.bind('<B1-Motion>', self.shiftSelection)
  def setCurrent(self, event):
    self.curIndex = self.nearest(event.y)
  def getState(self, event):
    i = self.nearest(event.y)
    self.curState = self.selection_includes(i)
  def shiftSelection(self, event):
    i = self.nearest(event.y)
    if self.curState == 1:
      self.selection_set(self.curIndex)
    else:
      self.selection_clear(self.curIndex)
    if i < self.curIndex:
      x = self.get(i)
      selected = self.selection_includes(i)
      self.delete(i)
      self.insert(i+1, x)
      if selected:
        self.selection_set(i+1)
      self.curIndex = i
      x = self.get(i)
      selected = self.selection_includes(i)
      self.delete(i)
      self.insert(i-1, x)
      if selected:
        self.selection_set(i-1)
      self.curIndex = i

frame = Frame(ws, padx=20, pady=20, bg=bgcolor)
frame.pack(expand=True, fill=BOTH)

btn_frame = Frame(frame, bg=bgcolor)
btn_frame.grid(columnspan=2, pady=(50, 0))

sort_btn = Button(
    btn_frame,
    text='Individual Sort',
    command=sort,
    font=btn_font,
    padx=10, 
    pady=5
)
sort_btn.pack(side=LEFT, expand=True, padx=(5,5))

ws.mainloop()
python tkinter path listbox
1个回答
0
投票

根据提供的代码,所有文件都来自同一文件夹。 因此,您可以简单地将文件夹名称存储为列表框的属性,并使用它来获取内部的完整路径

save()
:

...

def sort():
    ...
    directory = filedialog.askopenfilenames()
    # check whether files are selected
    if directory:
        for file in directory:
            listbox.insert('end', os.path.basename(file))
        # store the selected folder as an attribute of the listbox
        listbox.directory = os.path.dirname(file)

def save():
    image = listbox.get(0, 'end')
    for x in image:
        # use the saved folder to construct the full pathname
        print(os.path.normpath(os.path.join(listbox.directory, x)))
...
© www.soinside.com 2019 - 2024. All rights reserved.