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:
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.