[Tkinter] Unable to get current contents of the entry field where the content is the file path - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Unable to get current contents of the entry field where the content is the file path (/thread-26015.html) |
Unable to get current contents of the entry field where the content is the file path - pmpinaki - Apr-18-2020 My intention , allow user to select a file from the file explorer by clicking a button on Tkinter window application. I handled this button command using filedialog library. So once the user select the file, the file path will be automatically entered in the entry field. I want to get that file path details from the entry field. However, whenever I try to fetch the current content of Entry field, it returns an empty string. Below is my Code: from tkinter import * from tkinter import filedialog import time ''' Enabling High DPI in Windows 10 System''' try: from ctypes import windll windll.shcore.SetProcessDpiAwareness(1) except: pass filepath = '' root = Tk() # Initializing Tk root widget root.title("Trace log Analysis") # Setting the title of widget root.geometry("600x300") # Setting the size of the window root.resizable(0, 0) # Restricting user to resize the window in x and y direction root.configure(background="powder blue") info_display = StringVar() localtime = time.asctime(time.localtime(time.time())) def main(): #global info_display # layout all of the main containers root.grid_rowconfigure(1, weight=1) root.grid_columnconfigure(0, weight=1) # ####### Label- Display local time ################################################################################ lbl_localtime = Label(root, font=("arial", 10, "bold"), text=localtime, fg="steel blue", bd=5, anchor='w') lbl_localtime.pack(side=TOP) # Splitting the window in to frames ################################################################################ fileselection = LabelFrame(root) fileselection.pack(padx=0, pady=0, side=TOP) fileselection.config(relief=GROOVE, text="Select log file", borderwidth=5, background="white", fg="blue", font="Helvetica 10 bold", height=100, width=600) other_options = LabelFrame(root) other_options.pack(side=LEFT, pady=0, fill=X) other_options.config(relief=SUNKEN, text="Output", borderwidth=5, background="white", fg="blue", font="Helvetica 10 bold", height=100, width=400 ) # Label Info to display current date and time on window ####################################### lbl_info = Label(fileselection, font=("arial", 8, "bold"), text="File Name:", fg="steel blue", bd=5) lbl_info.pack(side=LEFT, padx=5) # Entry Field: State is disabled so that user can only select file from file explorer txt_filepath = Entry(fileselection, font=("arial", 8, "bold"), textvariable=info_display, insertwidth=2, bg="white", justify=LEFT, state=DISABLED, width=50) txt_filepath.pack(side=LEFT, padx=12, pady=0) # Add browse button to select file from file explorer browse_btn = Button(fileselection, text="Browse file...", justify=CENTER, anchor=E, width=10, height=0, font=("arial", 8, "bold")) browse_btn.pack(side=RIGHT, padx=5) # Configure the button command. # When user click Browse file... button, file explorer open and user can navigate to directory to select log file browse_btn.config(command=file_dialog) print(info_display.get()) # Get the Entry field data print(txt_filepath.get()) root.mainloop() def file_dialog(): global filepath global info_display getfile = filedialog.askopenfilename(initialdir="/", title="Select log file", filetypes=(("log files", "*.log"), ("all files", "*.*"))) filepath = '' + getfile info_display.set(filepath) if __name__ == '__main__': main()When I ran the above code, I could able to select the file and file path gets displayed on Entry widget. But when I print print(txt_filepath.get())output is blank. RE: Unable to get current contents of the entry field where the content is the file path - deanhystad - Apr-18-2020 The code works perfectly. when "print(txt_filepath.get)" is executed, the value is "". You have not yet selected a file or set the variable. I rewrote file_dialot() to display the path before drawing the dialog. def file_dialog(): print('dialog', info_display.get()) getfile = filedialog.askopenfilename(initialdir="/", title="Select log file", \ filetypes=(("log files", "*.log"), ("all files", "*.*"))) filepath = '' + getfile info_display.set(filepath)The first time I select a log file the display is: The second time:dialog C:/Python_Musings/test.logThis is the correct output. The first time through the variable has not been set, so the value is "". The second time we see the previously set value is still there. Your problem is not a variable issue, but a timing issue. You are trying to use the variable at the wrong time. |