使用按钮打开文件,然后在Python3中存储路径

时间:2017-11-30 18:51:58

标签: python-3.x tkinter

正如标题所述,我想打开一个带有浏览按钮的文件,然后存储文件路径供以后使用。我没有浏览文件的问题,我只是无法获得存储到变量的路径。非常感谢。

from tkinter import *
from tkinter import filedialog

import openpyxl
from termcolor import cprint


# initializing tk
root = Tk()
root.title("Tools")
root.geometry("600x300")
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack(side = BOTTOM )

def getFile():
    # open dialog box to select file
    root.filename = filedialog.askopenfilename(initialdir="/", title="Select file")


#create button
browsebutton = Button(frame, text="Browse", command=getFile)
browsebutton.pack(side = BOTTOM)

#store the open file path into a variable
path = root.filename

1 个答案:

答案 0 :(得分:1)

您可以使用全局(如果可能,您应该避免使用全局)或使用类

app.set('view engine', 'pug');