Allow user to change default text in tkinter entry widget.
Question: I'm writing a python script that requires the user to enter the name of a
folder. For most cases, the default will suffice, but I want an entry box to
appear that allows the user to over-ride the default. Here's what I have:

    
    
    from Tkinter import *
    import time
    def main():
        #some stuff
        def getFolderName():
            master = Tk()
            folderName = Entry(master)
            folderName.pack()
            folderName.insert(END, 'dat' + time.strftime('%m%d%Y'))
            folderName.focus_set()
            createDirectoryName = folderName.get()
            def callback():
                global createDirectoryName
                createDirectoryName = folderName.get()
                return 
            b = Button(master, text="OK and Close", width=10, command=callback)
            b.pack()
            mainloop()
            return createDirectoryName
        getFolderName()
    
       #other stuff happens....
        return
    
    if __name__ == '__main__':
        main()
    

I know next to nothing about tkInter and have 2 questions.

  1. Is over-riding the default entry using `global createDirectoryName` within the callback function the best way to do this?
  2. How can I make the button close the window when you press it. I've tried
    
        def callback():
        global createDirectoryName
        createDirectoryName = folderName.get()
        master.destroy
    

but that simply destroys the window upon running the script.


Answer: I don't know how experienced are you in Tkinter, but I suggest you use
classes.

    
    
    try:
        from tkinter import * #3.x
    except:
        from Tkinter import * #2.x
    
    
    class anynamehere(Tk): #you can make the class inherit from Tk directly, 
        def __init__(self): #__init__ is a special methoed that gets called anytime the class does 
            Tk.__init__(self) #it has to be called __init__
            #further code here e.g.
            self.frame = Frame()
            self.frame.pack()
            self.makeUI()
            self.number = 0 # this will work in the class anywhere so you don't need global all the time
        def makeUI(self):
            #code to make the UI
            self.number = 1 # no need for global
            #answer to question No.2
            Button(frame, command = self.destroy).pack()
    
    
    anyname = anynamehere() #remember it alredy has Tk
    anyname.mainloop()
    

Also why do you want to override the deafult Entry behavior ? The solution
would be to make another button and bind a command to it like this

    
    
        self.enteredtext = StringVar()
        self.entry = Entry(frame, textvariable = self.enteredtext)
        self.entry.pack()
        self.button = Button(frame, text = "Submit", command = self.getfolder, #someother options, check tkitner documentation for full list)
        self.button.pack()
    def getfolder(self): #make the UI in one method, command in other I suggest
        text = self.enteredtext.get()
        #text now has whats been entered to the entry, do what you need to with it
    

