Мое намерение, позволить пользователю выбрать файл из проводника, нажав кнопку в окне приложения Tkinter. Я обработал эту кнопку с помощью библиотеки filedialog. Поэтому, как только пользователь выберет файл, путь к файлу будет автоматически введен в поле ввода. Я хочу получить данные пути к файлу из поля ввода. Однако всякий раз, когда я пытаюсь получить текущее содержимое поля ввода, он возвращает пустую строку. Ниже мой код:
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()
Есть ли что-то, что мне не хватает? Любая помощь очень ценится !!!
Заранее спасибо