如何在Python/Tkinter中使用浏览按钮显示文件路径

如何在Python/Tkinter中使用浏览按钮显示文件路径,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,在使用Python和Tkinter时,我一直在试图找到在Browse按钮旁边显示文件路径的方法,但无法做到这一点 这是我的密码: import os from tkFileDialog import askopenfilename from Tkinter import * content = '' file_path = '' #~~~~ FUNCTIONS~~~~ def open_file(): global content globa

在使用Python和Tkinter时,我一直在试图找到在Browse按钮旁边显示文件路径的方法,但无法做到这一点

这是我的密码:

  import os
  from tkFileDialog import askopenfilename
  from Tkinter import *


  content = ''
  file_path = ''


  #~~~~ FUNCTIONS~~~~

  def open_file():
    global content
    global file_path

    filename = askopenfilename()
    infile = open(filename, 'r')
    content = infile.read()
    file_path = os.path.dirname(filename)
    return content

  def process_file(content):
    print content

  #~~~~~~~~~~~~~~~~~~~


  #~~~~~~ GUI ~~~~~~~~

  root = Tk()
  root.title('Urdu Mehfil Ginti Converter')
  root.geometry("598x120+250+100")

  mf = Frame(root)
  mf.pack()


  f1 = Frame(mf, width=600, height=250)
  f1.pack(fill=X)
  f2 = Frame(mf, width=600, height=250)
  f2.pack()

  file_path = StringVar


  Label(f1,text="Select Your File (Only txt files)").grid(row=0, column=0, sticky='e')
  Entry(f1, width=50, textvariable=file_path).grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
  Button(f1, text="Browse", command=open_file).grid(row=0, column=27, sticky='ew', padx=8, pady=4)
  Button(f2, text="Process Now", width=32, command=lambda: process_file(content)).grid(sticky='ew', padx=10, pady=10)


  root.mainloop()


  #~~~~~~~~~~~~~~~~~~~
请指导我如何显示文件路径以及“浏览按钮”按钮后,用户选择的文件如图所示


提前谢谢

首先,更改此行:

    Entry(f1, width=50, textvariable=file_path).grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
为此:

entry = Entry(f1, width=50, textvariable=file_path)
entry.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
然后,在
open_file()
函数中,在
返回之前添加这两行:

entry.delete(0, END)
entry.insert(0, file_path)

说明: 首先,我们给条目一个名称,以便对其进行修改。
然后,在
open_file()
函数中,我们清除它并添加文件路径的文本。

首先,更改此行:

    Entry(f1, width=50, textvariable=file_path).grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
为此:

entry = Entry(f1, width=50, textvariable=file_path)
entry.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
然后,在
open_file()
函数中,在
返回之前添加这两行:

entry.delete(0, END)
entry.insert(0, file_path)

说明: 首先,我们给条目一个名称,以便对其进行修改。
然后,在
open_file()
函数中,我们清除它并为文件路径添加文本。

这里有一个diff,它修复了
文件路径,即
StringVar()
用法:

--- old.py      2016-08-10 18:22:16.203016340 +0200
+++ new.py      2016-08-10 18:24:59.115328029 +0200
@@ -4,7 +4,6 @@


 content = ''
-file_path = ''


 #~~~~ FUNCTIONS~~~~
@@ -16,7 +15,7 @@
   filename = askopenfilename()
   infile = open(filename, 'r')
   content = infile.read()
-  file_path = os.path.dirname(filename)
+  file_path.set(os.path.dirname(filename))
   return content

 def process_file(content):
@@ -40,7 +39,7 @@
 f2 = Frame(mf, width=600, height=250)
 f2.pack()

-file_path = StringVar
+file_path = StringVar(root)


 Label(f1,text="Select Your File (Only txt files)").grid(row=0, column=0, sticky='e')

下面是一个diff,它修复了
文件路径,即
StringVar()
用法:

--- old.py      2016-08-10 18:22:16.203016340 +0200
+++ new.py      2016-08-10 18:24:59.115328029 +0200
@@ -4,7 +4,6 @@


 content = ''
-file_path = ''


 #~~~~ FUNCTIONS~~~~
@@ -16,7 +15,7 @@
   filename = askopenfilename()
   infile = open(filename, 'r')
   content = infile.read()
-  file_path = os.path.dirname(filename)
+  file_path.set(os.path.dirname(filename))
   return content

 def process_file(content):
@@ -40,7 +39,7 @@
 f2 = Frame(mf, width=600, height=250)
 f2.pack()

-file_path = StringVar
+file_path = StringVar(root)


 Label(f1,text="Select Your File (Only txt files)").grid(row=0, column=0, sticky='e')

非常感谢@laurencevs!我花了几个小时来实现这个功能,但我无法做到。非常感谢。@pleasureinblues您可以通过单击左侧的小勾号向其他人显示此答案很有用。非常感谢@laurencevs!我花了几个小时来实现这个功能,但我无法做到。非常感谢。@pleasureinblues您可以通过单击左侧的小勾号向其他人显示此答案很有用。