Set Entry widget values dynamically in Tkinter

1.3k views Asked by At

I have a window to browse a folder containing necessary files. I am using tkFileDialog for the same. I want to set the value of Entry widget equal to this selected folder. Initially when no folder is selected it will be null. As soon as I select the folder, the path of the selected folder should appear in the Entry widget. The user should be able to modify.Below mentioned is the code for the same.

from Tkinter import *
from tkFileDialog import *

class Checkit:
    root = Tk()
    #default string to be displayed in the entry of path        
    path_to_file = StringVar(root, value="abc")

    def __init__(self):     
    self.inputDetail()

    def inputDetail(self):              
        #copy the root window
        master = self.root

        #create frame for details in the root window
        details_frame = Frame(master)
        details_frame.grid(row=0, column=0)

        #Create the Labels
        papercode_label = Label(details_frame, text="Paper code:")
        subject_label = Label(details_frame, text="Subject:")
        chapter_label = Label(details_frame, text="Chapter:")
        batch_label = Label(details_frame, text="Batch:")
        ansFolder_label = Label(details_frame, text="Folder containing answer-keys:")

        #create entry for the labels
        papercode_entry = Entry(details_frame)
        subject_entry = Entry(details_frame)
        chapter_entry = Entry(details_frame)
        batch_entry = Entry(details_frame)
        ansFolder_entry = Entry(details_frame)

        #create button to add path
        path = Button(details_frame, text="Browse", command= lambda: self.addpath(details_frame))

        #button to enter the next window
        next = Button(details_frame, text="Next", command= lambda: self.checkOmr(details_frame, master))

        #Use grid layout to place labels and entry
        papercode_label.grid(row=0, sticky=W)
        papercode_entry.grid(row=1, sticky=W)

        subject_label.grid(row=2, sticky=W)
        subject_entry.grid(row=3, column=0, sticky=W)

        chapter_label.grid(row=4, sticky=W)
        chapter_entry.grid(row=5, column=0, sticky=W)

        batch_label.grid(row=6, sticky=W)
        batch_entry.grid(row=7, column=0, sticky=W)

        ansFolder_label.grid(row=8, sticky=W)
        path.grid(row=9, sticky=W, columnspan=2)
        next.grid(row=10, sticky=E, columnspan=2)

        master.mainloop()

    def checkOmr(self, old_frame, master):
        #destoy the  old frame
        old_frame.destroy()

        #create frame for details in the root window
        inputPath_frame = Frame(master)
        inputPath_frame.grid(row=0, column=0)

        #create label to input folder containing 
        omrFolder_label = Label(inputPath_frame, text="Folder containing OMR sheet to be checked:")     

        #create button to add path
        path = Button(inputPath_frame, text="Browse", command= lambda: self.addpath(inputPath_frame))

        selected_path = Entry(inputPath_frame, textvariable=self.path_to_file)

        #place the label and button on the grid     
        omrFolder_label.grid(row=0, sticky=W)       
        path.grid(row=1, column=0, sticky=W)
        selected_path.grid(row=1, column=1, sticky=W)

        #master.mainloop()

    def addpath(self, details_frame):
        self.path_to_file = askdirectory(parent=details_frame,initialdir="/",title='Please select a directory')     

if __name__=='__main__':
    handle = Checkit()

Here I am trying to change the modifying the self. path_to_file value on the click of the button. I tried to print the value of self.path_to_file value in addpath(). It gives correct result there but the value in the Entry selected_path in checkOMR() does not modify.

Can somebody suggest what changes should I make to make that thing possible.

1

There are 1 answers

0
Bryan Oakley On BEST ANSWER

Look at this line:

self.path_to_file = askdirectory(...)  

Before this line of code runs, self.path_to_file is an instance of a StringVar. After this line of code has run, self.path_to_file is reset to be just a string.

Assuming you want self.path_to_file to remain an instance of StringVar, you need to change that line to this:

path = askdirectory(...)
self.path_to_file.set(path)