Python 如何删除目录并在tkinter中仅显示文件名?

Python 如何删除目录并在tkinter中仅显示文件名?,python,tkinter,Python,Tkinter,我试图使用filedialog只显示文件名 下面是一个例子: import tkinter as tk import pygame from tkinter import filedialog root = tk.Tk() controls_frame = tk.Frame(root) controls_frame.pack() def add_name(): name = filedialog.askopenfile() names.insert(filedialog.E

我试图使用filedialog只显示文件名

下面是一个例子:

import tkinter as tk
import pygame
from tkinter import filedialog

root = tk.Tk()

controls_frame = tk.Frame(root)
controls_frame.pack()

def add_name():
    name = filedialog.askopenfile()
    names.insert(filedialog.END,name)

names = tk.Listbox(root,bg="black",fg="red",width=60,height=6)
names.pack(pady=0,side='bottom')

add_name_btn = tk.Button(controls_frame , text='add name' , command=add_name)
add_name_btn.grid(column=3,row=0,padx=20) 

root.mainloop()
它显示了文件的完整目录,这是我不想要的。
如何删除显示在框中的目录?

模块
os
可以帮助:

import os

    def add_name():
        name = os.path.basename(str(filedialog.askopenfile()))
        name = name.replace("' mode='r' encoding='UTF-8'>", "")
        names.insert(filedialog.END,name)

阅读python中的所有功能感谢您的帮助尽管请记住,我认为您需要
askopenfilename()
askopenfile()
用于打开文件本身。您不能拆分文件名,然后将其放入列表框吗?