Python 2.7 如何以windows路径格式显示条目小部件中的文件路径

Python 2.7 如何以windows路径格式显示条目小部件中的文件路径,python-2.7,tkinter,Python 2.7,Tkinter,我已经编写了一个Tkinter程序,其中Browse按钮用于选择一个文件,所选文件的完整路径将显示在Entry小部件中。但我的问题是,它显示路径时使用的是“前进”/斜杠,而不是传统的windows格式的“后退”\斜杠。这对我来说很奇怪,因为我在windows操作系统上工作。 为什么会发生这种情况?是否有任何手动修复,而不是替换字符串选项 我的代码: def selectfile(): fileName = askopenfilename(parent=root, title='Choos

我已经编写了一个Tkinter程序,其中Browse按钮用于选择一个文件,所选文件的完整路径将显示在Entry小部件中。但我的问题是,它显示路径时使用的是“前进”/斜杠,而不是传统的windows格式的“后退”\斜杠。这对我来说很奇怪,因为我在windows操作系统上工作。 为什么会发生这种情况?是否有任何手动修复,而不是替换字符串选项

我的代码:

def selectfile():
    fileName = askopenfilename(parent=root, title='Choose a file', initialdir='C:\\')       
    custName.set(fileName) #Populate the text field with the selected file

#create the 'filepath' field
custName = StringVar(None)
filepath = Entry(root, width ='50', textvariable=custName).pack(anchor=W)

#Create the 'Browse' button
browseButton = Button(root, text="Browse", relief = 'raised', width=8, command=selectfile, cursor='hand2').place(x=325,y=16)
条目小部件中的预期输出:

c:\data\file.txt
c:/data/file.txt
条目小部件中的实际输出:

c:\data\file.txt
c:/data/file.txt

您始终可以使用replace替换/with\作为字符串。以下是有关替换方法的文档链接:


这是解决方法。尝试更深入地查看Tkinter的文档,以获得出现这种情况的实际答案。

我已经知道这种修复方法,但我的目的是想知道为什么在Tkinter文档中找不到任何解释,以及我是否可以在输入小部件中显示之前手动修复这种情况。