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
#2
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:
Output:
dialog
The second time:
dialog C:/Python_Musings/test.log
This 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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] remove file from current project rwahdan 2 2,256 Jul-25-2021, 09:14 AM
Last Post: Larz60+
  [Tkinter] compare entry with value inside file rwahdan 1 2,052 Jun-19-2021, 08:01 AM
Last Post: Yoriz
  Disable entry field and still see value scratchmyhead 5 4,974 May-11-2020, 08:09 PM
Last Post: menator01
  Converting Entry field value to integer in tkinter scratchmyhead 2 4,886 May-11-2020, 03:41 PM
Last Post: scratchmyhead
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,427 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] Unable to save filepath to config.ini file using filedialog.askopenfilename JackMack118 10 4,909 Dec-29-2019, 08:12 PM
Last Post: JackMack118
  [PyQt] Collect entry from textline Widget via UI file mart79 3 2,858 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,264 Jul-25-2019, 08:27 PM
Last Post: DT2000
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 3,457 Mar-18-2019, 05:36 PM
Last Post: woooee
  [Tkinter] Displaying file's contents as labels gellerb 3 3,235 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