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

我一直在尝试使用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所示.

提前致谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值