Python Forum
[Tkinter] Unable to get current contents of the entry field where the content is the file path
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Unable to get current contents of the entry field where the content is the file path
#1
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.
Reply


Messages In This Thread
Unable to get current contents of the entry field where the content is the file path - by pmpinaki - Apr-18-2020, 06:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] remove file from current project rwahdan 2 2,338 Jul-25-2021, 09:14 AM
Last Post: Larz60+
  [Tkinter] compare entry with value inside file rwahdan 1 2,105 Jun-19-2021, 08:01 AM
Last Post: Yoriz
  Disable entry field and still see value scratchmyhead 5 5,230 May-11-2020, 08:09 PM
Last Post: menator01
  Converting Entry field value to integer in tkinter scratchmyhead 2 5,021 May-11-2020, 03:41 PM
Last Post: scratchmyhead
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,505 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] Unable to save filepath to config.ini file using filedialog.askopenfilename JackMack118 10 5,082 Dec-29-2019, 08:12 PM
Last Post: JackMack118
  [PyQt] Collect entry from textline Widget via UI file mart79 3 2,944 Aug-05-2019, 01:40 PM
Last Post: Denni
  [Tkinter] How to get the name of a file only from a file path ? DT2000 4 10,478 Jul-25-2019, 08:27 PM
Last Post: DT2000
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 3,536 Mar-18-2019, 05:36 PM
Last Post: woooee
  [Tkinter] Displaying file's contents as labels gellerb 3 3,339 Apr-02-2018, 01:35 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020