Tkinter: focus/text selection problem with tkFileDialog

Discussion in 'Python' started by Irmen de Jong, Apr 30, 2004.

  1. 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()
     
    Irmen de Jong, Apr 30, 2004
    #1
    1. Advertising

  2. Irmen de Jong

    Peter Otten Guest

    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
     
    Peter Otten, Apr 30, 2004
    #2
    1. Advertising

  3. 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.
     
    Irmen de Jong, Apr 30, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank Chen
    Replies:
    3
    Views:
    518
    Cameron Laird
    Oct 8, 2003
  2. Gerardo ARnaez
    Replies:
    1
    Views:
    719
    Eric Brunel
    Aug 14, 2008
  3. Leonhard Vogt

    Tkinter Entry blocked by tkFileDialog

    Leonhard Vogt, Oct 2, 2008, in forum: Python
    Replies:
    0
    Views:
    367
    Leonhard Vogt
    Oct 2, 2008
  4. Steve Offutt
    Replies:
    1
    Views:
    1,950
    Steve Offutt
    Apr 7, 2009
  5. Roger
    Replies:
    3
    Views:
    338
Loading...

Share This Page