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()