Home > Software design >  File-path not displaying on tkinter entry box
File-path not displaying on tkinter entry box

Time:10-28

I've looked into multiple threads and I can't seem to find out why the stringVar is not displaying on the entry box, once the file has been selected. I can see the print(filePath.get()) has the selected file but entry box stays blank.

Can someone please help?

#import libraries

import os
import paramiko
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter.filedialog import askopenfilename
import sys

def getFilePath():
    file_selected = filedialog.askopenfilename(title="Select File")
    filePath.set(file_selected)
    print(filePath.get())

#-------------------------------GUI------------------------------

gui = tk.Tk()
gui.title("NetWizard 1.0")
tabControl = ttk.Notebook(gui, width = 1000, height = 700)
gui.resizable(False, False)
tabControl.pack( expand = 1, fill ="both")
  
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
  
tabControl.add(tab1, text ='RUGGEDCOM', )
tabControl.add(tab2, text ='WESTERMO')


#--------------------------RUGGEDCOM WIDGET----------------------
filePath = StringVar()
entry1 = Entry(gui, textvariable=filePath, width=1000)
entry1 = tk.Entry(tab1, width =  60)

label1 = Label(tab1, text="IP Address List Input:", font=('Arial', '11', 'bold'))

button1 = Button(tab1, text='Browse', command=getFilePath, font=("Arial", 10), width = 10, bg='#add8e6')


label1.grid(row=0, column=0, padx = 15, pady=10, sticky = W)
entry1.grid(row=1, column=0, padx=15, pady=5)
button1.grid(row=1, column=1, padx=0, pady=0, sticky = W)

gui.mainloop()

CodePudding user response:

It is because you did not set your entry text value in your function. Supposedly after you get your file path, you should assign the file path text value into the entry.

You can do so by inserting this code into your codes.

entry1.insert(0, "Test Value")

Your codes:

def getFilePath():
    file_selected = filedialog.askopenfilename(title="Select File")
    filePath.set(file_selected)
    print(filePath.get())
    entry1.insert(0, filePath.get())

You can refer to https://www.geeksforgeeks.org/how-to-set-the-default-text-of-tkinter-entry-widget/ .

  • Related