[Tutor] tkFileDialog.Directory

K.Weinert@gmx.net K.Weinert at gmx.net
Mon Nov 14 14:42:27 CET 2005


Hello!
I want to learn Tkinter and try to build a small File search dialog. Tkinter
is nice, but here is a problem where I am stuck:

I want to allow the dialog's user to pick a directory. The widget for this
is tkFileDialog.Directory. But when I start the Directory-Window, it is
possible move the focus back to my File Search dialog. Thus it is possible
but not wanted to create several "Pick directory" windows. I tried to make
the Directory instance modal by calling .grab_set() but this raises an
attribute error. Here is a minimal example:

--- snip ---
import Tix
from Tkconstants import *
from tkFileDialog import Directory

class FileSearchBase:
    def __init__(self, root):
        self.root = root
        self.top = None
        self.title= "File Search"

    def open(self):
        if not self.top:
            self.create_widgets()
        else:
            self.top.deiconify()
            self.top.tkraise()
        self.top.grab_set()

    def close(self, event=None):
        if self.top is not None:
            self.top.grab_release()
            self.top.withdraw()

    def create_widgets(self):
        top = Tix.Toplevel(self.root) 
        top.resizable(0,0)
        top.bind("<Escape>", self.close)
        top.protocol("WM_DELETE_WINDOW", self.close)
        top.wm_title(self.title)
        self.top = top
        pickButton= Tix.Button(top, text = 'Pick..', 
                       command = self.pick_dir)
        pickButton.grid(row= 0, column=1)

    def pick_dir(self):
        dir_dlg= Directory()
        #dir_dlg.grab_set() # AttributeError: grab_set
        #self.top.wait_window(dir_dlg) # AttributeError: _w 
        dir= dir_dlg.show()
        print dir

if __name__ == '__main__':
    root = Tix.Tk()
    root.title("File Search Demo")

    widget = FileSearchBase(root)
    
    searchButton = Tix.Button(root,  text = 'Show search dialog', 
                                            command = widget.open)
    searchButton.pack(padx = 8, pady = 8)
    
    root.mainloop()

--- snip ---

The above example allows to create several Directory dialogs.
On the other hand, if I start the Directory dialog from the root
window (from the Tix.Tk() instance), everything works fine. The following
example works:

--- snip ---
import Tix
from Tkconstants import *
from tkFileDialog import Directory

def pick_dir():
    dir_dlg= Directory()
    dir= dir_dlg.show()
    print dir

if __name__ == '__main__':
    root = Tix.Tk()
    root.title("File Search Demo")

    pickButton = Tix.Button(root, text = 'Choose Directory', 
                   command = pick_dir)
    pickButton.pack(side = 'bottom')

    root.mainloop()
--- snip ---

Would be nice if someone could help me with the first example...

Kind regards,
Karsten.

-- 
Lust, ein paar Euro nebenbei zu verdienen? Ohne Kosten, ohne Risiko!
Satte Provisionen für GMX Partner: http://www.gmx.net/de/go/partner


More information about the Tutor mailing list