简体   繁体   English

通过单击按钮使用askopenfilename获取文件名后,如何在python的条目中显示该文件名

[英]After I use askopenfilename to get a filename by click a button, how can I show that filename in an Entry in python

Here is my code, so after I click the button, it will call loadFile function, and the file name will be saved in the function, after that how can I change the text of Entry(self.filedir) to that file name? 这是我的代码,因此在单击按钮后,它将调用loadFile函数,文件名将保存在该函数中,之后如何将Entry(self.filedir)的文本更改为该文件名?

from tkinter import *
from tkinter.filedialog import askopenfilename

class Checker:
    def loadFile(self):
        self.filename = askopenfilename(filetypes=(("info", "*.xlsx"), ("all file", "*.*")))

    def __init__(self, master):
        master.title("Checker")

        self.load_button = Button(master, text="load file", command=self.loadFile)
        self.load_button.grid(row=0, column=0)
        self.filedir = Entry(master, text=" ")
        self.filedir.grid(row=0, column=1)

if __name__=='__main__':
    root = Tk()
    k = Checker(root)
    root.mainloop()

Add the following two lines to loadFile : 将以下两行添加到loadFile

self.filedir.delete(0, "end")
self.filedir.insert(0, self.filename)

I have a tkinter class that creates a frame containing a label, entry field to contain the filepath and a browse button to select the file. 我有一个tkinter类,它创建一个包含标签的框架,一个包含文件路径的输入字段和一个用于选择文件的浏览按钮。

Try this simple example (Sorry it is python 2 not python 3 given its age). 试试这个简单的例子(很抱歉,它是python 2而不是python 3)。

from Tkinter import *
import tkFileDialog

class FileSelect(Frame):
    def __init__(self,master,label,opensave,filetype,**kw):
        Frame.__init__(self,master)
        self.configure(**kw)
        self.file = StringVar()
        self.opensave = opensave
        self.filetypes = filetype

        self.Label = Label(self, text=label)
        self.Label.config(width=10,anchor=E)
        self.filenamebox = Entry(self,text=self.file)
        self.filenamebox.config(width=50)
        self.btnBrowse = Button(self,text='Browse',command=self.browse_file)
        self.btnBrowse.config(width=10)
        self.Label.grid(row=0,column=0,pady=5,sticky=E)
        self.filenamebox.grid(row=0,column=1,pady=5)
        self.btnBrowse.grid(row=0,column=2,pady=5,padx=5)
    def browse_file(self):
        filename = []
        if self.opensave == "open":
            filename = tkFileDialog.askopenfilename(filetypes=self.filetypes)
        else:
            filename = tkFileDialog.asksaveasfilename(filetypes=self.filetypes)
        self.file.set(filename)
    def get_filename(self):
        return self.file.get()

def main():
    root = Tk()
    root.title("Select File Example")
    selectFile = FileSelect(root,"My File","open",[('All Files','*.*')])
    selectFile.grid()    
    root.mainloop()


if __name__ == '__main__':
    main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用用户条目来更改文件名'? - How can I use an user Entry to change a filename'? 如何使用askopenfile获取有效的文件名 - How can I use askopenfile to get a valid filename 如何打印 askopenfilename 返回的文件名? - How to print the filename returned by askopenfilename? 如何使用python更改文件名的一部分 - How can I change a part of a filename with python 如何创建一个按钮,将tkFileDialog.askopenfilename()的输入文件名作为字符串返回? - How to make a button that returns the input filename of tkFileDialog.askopenfilename() as a string? 如何在通过askopenfilename插入的labelframe中显示图像? - How can I show the image in a labelframe which is inserted through askopenfilename? 为什么我不能在 python 中使用包含带有 playsound 空格的文件名? - Why can't I use a filename containing a space with playsound in python? 如何在Python中设置模块的文件名,以便模块中发生的异常使用该文件名? - How do I set the filename of a module in Python so that exceptions that occur in the module use that filename? 如何获取要打开的文件的文件名和要保存在python中的文件的文件名 - How do I get the filename of a file to open and the filename of a file to save in python 如何使用字典存储 Python 中方法生成的文件名? - How do I use dictionary to store the filename generated by a method in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM