【问题标题】:How to Show File Path with Browse Button in Python / Tkinter如何在 Python / Tkinter 中使用浏览按钮显示文件路径
【发布时间】:2014-09-30 06:53:54
【问题描述】:

使用 Python 和 Tkinter,我一直在尝试找出在浏览按钮旁边显示 file_path 的方法,但无法这样做。

这是我的代码:

  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()


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

请指导我如何在用户选择文件后显示文件路径以及“浏览按钮”按钮,如image 所示。

提前致谢!

【问题讨论】:

    标签: python python-2.7 tkinter


    【解决方案1】:

    首先,改变这一行:

        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() 函数中,将这两行添加到return 之前:

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

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

    【讨论】:

    • 非常感谢@laurenevs!我花了几个小时来获得这个功能,但我做不到。非常感谢。
    • @pleasureinblues 您可以通过单击其左侧的小勾号向其他人展示此答案很有用。
    【解决方案2】:

    这是一个修复 file_path 的差异,即 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')
    

    【讨论】:

      猜你喜欢
      • 2016-12-22
      • 2017-09-16
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 2013-11-25
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多