Tkinter: focus/text selection problem with tkFileDialog

Peter Otten __peter__ at web.de
Fri Apr 30 10:41:50 EDT 2004


Irmen de Jong wrote:

> Hi,
> I'm having trouble with the code below.
> It's just a regular Tk text widget in which you can type and
> select text as expected, however the call to
> tkFileDialog.askopenfilename() seems to screw things up. After the file
> dialog, I can no longer use the Text widget (typing, selecting, it doesn't
> work anymore!) What am I doing wrong?

No sure, but opening the dialog before entering the main loop could be the
problem. The following modification seems to work:

import Tkinter, tkFileDialog

window = Tkinter.Tk()
txt = Tkinter.Text(window)
txt.insert(Tkinter.END, "Choose a file, then try to select me")
txt.pack()

def openFile(event=None):
    name = tkFileDialog.askopenfilename()
    txt.insert(Tkinter.END, "\nFile chosen: " + name)

txt.focus_set()
window.after_idle(openFile)
window.mainloop()

Peter




More information about the Python-list mailing list