By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Tkinter: focus/text selection problem with tkFileDialog

P: n/a
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?

TIA,
--Irmen de Jong
-----test.py----
import Tkinter, tkFileDialog

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

name=tkFileDialog.askopenfilename()
txt.insert(Tkinter.END,"\nFile chosen: "+name)
window.mainloop()
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

Jul 18 '05 #2

P: n/a
Peter Otten wrote:
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


Okay, I didn't know about the after_idle, and still don't understand why
my first attempt resulted in the weird behavior I described. But hey,
with a bit of tweaking, it works now :-)

Also another weird thing: I found out (using my original code
without the after_idle) that if I inserted a tkMessageBox before the
mainloop(), that the 'correct' behavior of the Text box is restored!
....confused...

--Irmen.

P.S. I have now also discovered Fredrik Lundh's page:
http://www.effbot.org/zone/tkinter-threads.htm Interesting.
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.