1

As an alternative to tkinter.filedialog.askdirectory I stumbled upon tix.DirSelectDialog.
Sadly I just don't get how to use it. I found out that there is an __init__(self, master) method but I especially don't know what "self" should be.

My goal is to trigger a directory selection dialog by pressing a button in the main window and to store the selected directory in a variable for later use. I suggest this minimized example for python 3.3:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from tkinter import *
import tkinter.tix as tix

root = Tk()

def pathSelect():
    d = tix.DirSelectDialog.__init__(self=???, master=root)
    if d != "":
        print(d)

button = Button(root, text="dialog", command=pathSelect)
button.pack()

root.mainloop()

If this was working, would it be enough to do what I want?

There is not much about this topic in the net. Two of my references are:
http://sourcecodebrowser.com/python3.2/3.2.3~rc2/classtkinter_1_1tix_1_1_dir_select_dialog.html#ae545b097538938871e9576b83fc664be
http://epydoc.sourceforge.net/stdlib/Tix.DirSelectDialog-class.html
They are always repeating the syntax but I'm not able to make use of that. Maybe anyone else with more programming skills can? After three hours of unsuccessful research and experimenting I just would be very happy with a little example.

Thanks very much in advance!


EDIT:

After applying the fix suggested in the answer of furas:

d = tix.DirSelectDialog(master=root)

I surprisingly got this error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\Python\lib\tkinter\__init__.py", line 1442, in __call__

    return self.func(*args)
  File "dirdialog.py", line 10, in pathSelect
    d = tix.DirSelectDialog(master=root)
  File "C:\Program Files\Python\lib\tkinter\tix.py", line 762, in __init__
    ['options'], cnf, kw)
  File "C:\Program Files\Python\lib\tkinter\tix.py", line 322, in __init__
    self.tk.call(widgetName, self._w, *extra)
_tkinter.TclError: invalid command name "tixDirSelectDialog"

It seems to come from within tix, but there can't be an error in the library, right? Does anyone have an answer for that?


SOLUTION (thanks to furas):

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from tkinter import *
import tkinter.tix as tix

root = tix.Tk()

def print_selected(args):
    print('selected dir:', args)

def pathSelect():
    d = tix.DirSelectDialog(master=root, command=print_selected)
    d.popup()

button = Button(root, text="dialog", command=pathSelect)
button.pack()

root.mainloop()
S818
  • 391
  • 3
  • 15

1 Answers1

1

Create instance as always:

d = tix.DirSelectDialog(master=root)

This code will execute (internally) tix.DirSelectDialog.__init__(self, master) with correct argument for self


You can treat

d = tix.DirSelectDialog(master=root)

almost like execution (internally by python) of code

tix.DirSelectDialog(self=d, master=root)

# which executes

tix.DirSelectDialog.__init__(self=d, master=root)

but normally you can't do this.


EDIT: to run it probably you have to install Tix (Tcl/Tk extensions) for your own and use tix.Tk() in place of tkinter.Tk()

Working example for Python 2:

import Tix as tix

def print_selected(args):
    print('selected dir:', args)

root = tix.Tk()
dialog = tix.DirSelectDialog(root, command=print_selected)
dialog.popup()

Similar example for Python 3 (but I have some problem to work properly on my computer)

import tkinter.tix as tix

def print_selected(args):
    print('selected dir:', args)

root = tix.Tk()
dialog = tix.DirSelectDialog(root, command=print_selected)
dialog.popup()
furas
  • 134,197
  • 12
  • 106
  • 148
  • Thank you! This was such a stupid mistake of me... I am definitely going to learn from that. I corrected this line and expected a nice dialog to pop up, but instead I got the error I will add to my question in a minute. Do you have any ideas? – S818 Jul 22 '14 at 15:25
  • `tkinter` is only wrapper on `tcl/tk` language (which is installed with python). And `tkinter.tix` is wrapper on `tix` extension for `tcl/tk` but probably you have to install it on your own. – furas Jul 22 '14 at 16:07
  • I found solution - probably you have to use `tix.Tk()` in place of `tkinter.Tk()` - see example (for Python 2) on internet: [Using Tix with Python](http://tix.sourceforge.net/docs/html/TixUser/subsection3_2_2.html) – furas Jul 22 '14 at 16:16
  • And again you are perfectly right. Just replacing `Tk()` with `tix.Tk()` printed me 8-digit numbers with a dot like .43934720, so I realized that this must be a handle or so. The `d.popup()`finally gave me the dialog I wanted. By the way, your example for python 3 pops up 2 windows for a fraction of a second. Adding `time.sleep(2000)` crashed the program, but let me see, that the first was the main window and the second the still empty and not responding dialog. So, I guess, that's it! Thank you for all your help! I'll add a fusion of our both examples as final result to my initial post. – S818 Jul 22 '14 at 17:54