Tkinter: buttons are dead of tkFileDialog.askopenfilename

John Grayson johngrayson at home.com
Mon Jul 10 14:24:58 EDT 2000


In article <3969F138.4EFE768E at acm.org>,
  John Powers <jpowers at acm.org> wrote:
>
> --------------0EF7BCC78CC8B59055EF54F1
> Content-Type: text/plain; charset=us-ascii
> Content-Transfer-Encoding: 7bit
>
> I have a simple Python script to illustrate a problem I'm having with
> askopenfilename. The following program displays a window with a "Quit"
> button. The "Quit" button works until you uncomment the statement
which
> calls askopenfilename. After selecting a file from the Open Dialog
box,
> the button window appears but the "Quit" button is dead -- absolutely
no
> response to pressing it. What's up? Maybe I shouldn't be calling
> askopenfilename while initializing App?

Fairly simple problem... You need to ensure that the mainloop is
spinning before you show the dialog. One way to do this is to delay
the dialog for a few milliseconds...

> from Tkinter import *
import tkFileDialog

class App(Tk):

  def __init__(self):
    Tk.__init__(self)
    filename = "filename"
    Label(self, text=filename).pack()
    Button(self, text="Quit", command=self.quit).pack()

    self.after(5, lambda s=self, f=filename: s.ask(f))

  def ask(self, filename):
    tkFileDialog.askopenfilename(initialfile=filename)

App().mainloop()



  John


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list