简体   繁体   中英

Insert File Dialog Output into Entry Widget in Tkinter

I was wondering if I could possibly get someone's assistance. I am new to Tkinter and UI's in general, and want to create one using Tkinter. The only problem I have right now is adding the path of a file to an entry widget, as shown below:

from tkinter import *    
from tkinter import ttk    
from tkinter import filedialog
import tkcalendar
import datetime


class Feedback:

    def __init__(self, master):
        self.frame_date = ttk.Frame(master)
        self.frame_date.pack()

        self.style = ttk.Style()
        self.style.configure('Header.TLabel', font=('Arial', 12, 'bold'))

        ttk.Label(self.frame_date, text='Quarter Start Date', style='Header.TLabel').grid(row=0, column=0, padx=40)
        ttk.Label(self.frame_date, text='Quarter End Date', style='Header.TLabel').grid(row=0, column=1, padx=40)

        self.calendar_start = tkcalendar.DateEntry(self.frame_date)
        self.calendar_start.grid(row=1, column=0, padx=40, ipadx=20)
        self.calendar_end = tkcalendar.DateEntry(self.frame_date)
        self.calendar_end.grid(row=1, column=1, padx=40, ipadx=20)

        self.frame_docs = ttk.Frame(master)
        self.frame_docs.pack()

        ttk.Label(self.frame_docs, text='Choose Counter Level File', style='Header.TLabel').grid(row=0, column=0,
                                                                                                 columnspan=2)
        self.cl_import_button = ttk.Button(self.frame_docs, text='Import Counter Level',
                                           command=lambda: self.paste_file_name()).grid(row=1, column=0, ipadx=40)    #the button pressed to open up the file dialog

        self.my_string = StringVar()    #string variable used to hold file dialog input

        self.cl_filepath = ttk.Entry(self.frame_docs, textvariable=self.my_string).grid(row=2, column=0)    #the entry widget used to hold the file path

    def paste_file_name(self):    #the function called to open up the file dialog and save the path
        self.file_name = filedialog.askopenfile()
        self.my_string = self.file_name


def main():
    root = Tk()
    feedback = Feedback(root)
    root.mainloop()


if __name__ == '__main__': main()

As you may be able to see, I would like to add the file path to the String Variable 'self.my_string', which is the text variable of my entry widget. This should only be done once the import button is pressed.

Since self.my_string is a StringVar , you should use self.my_string.set() to update its value:

    def paste_file_name(self):    #the function called to open up the file dialog and save the path
        self.file_name = filedialog.askopenfile()
        self.my_string.set(self.file_name.name)

Note that askopenfile() will open the file as well, so if you only want the filename, use askopenfilename() instead:

    def paste_file_name(self):    #the function called to open up the file dialog and save the path
        self.file_name = filedialog.askopenfilename()
        self.my_string.set(self.file_name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM