0

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.

6
  • what happend when you click the import button? Commented Dec 1, 2020 at 9:57
  • Use self.my_string.set(self.file_name.name) instead. However, better use askopenfilename() instead of askopenfile(). Commented Dec 1, 2020 at 9:58
  • you dont need lambda for your button command as you dont pass parameters use command=self.paste_file_name instead Commented Dec 1, 2020 at 10:00
  • This is perfect. Thank you @acw1668, your suggestion is what I needed. Commented Dec 1, 2020 at 10:27
  • @reece, thank you for your prompt response and input as well, it was much appreciated. Commented Dec 1, 2020 at 10:27

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. This is exactly what I was looking for. I really appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.