1

I have been using tkinter to supply a file dialog (in python 3.6) which allows users to select a directory. It works fine when it is a sub-function within the same module, but if I move that subfunction into a separate module and then try to import it from that module, it no longer works. Instead the code just hangs when the file dialog should pop up but it never appears.

working code: This works if I run the main function

from tkinter import Tk
from tkinter.filedialog import askdirectory

def SelectDirectory():
    # start up the tk stuff to have a file directory popup
    print('start')
    root = Tk()
    print('postroot')
    root.withdraw()
    print('postwithdraw')
    # let the user pick a folder
    basepath = askdirectory(title='Please select a folder')
    print('postselection')
    root.destroy()
    print('postdestroy')
    return basepath

def main():
    ans = SelectDirectory()
    print(ans)

If I instead put this in another module and import it from that module, then it will print until 'postwithdraw' and then just hang.

submod.py:

from tkinter import Tk
from tkinter.filedialog import askdirectory

def SelectDirectory():
    # start up the tk stuff to have a file directory popup
    print('start')
    root = Tk()
    print('postroot')
    root.withdraw()
    print('postwithdraw')
    # let the user pick a folder
    basepath = askdirectory(title='Please select a folder')
    print('postselection')
    root.destroy()
    print('postdestroy')
    return basepath

and then run this:

from submod import SelectDirectory

def main():
    ans = SelectDirectory()
    print(ans)

It never gets past 'postwithdraw' and the file dialog never pops up.

Does anyone know what I am doing wrong here? I would think it has something to do with the tkinter window not appearing since its not the main module but is there some way to get past that?

user2731076
  • 689
  • 1
  • 6
  • 21
  • 1
    Read [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) – stovfl Oct 14 '19 at 21:34
  • Possible duplicate of [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) – ivan_pozdeev Oct 14 '19 at 22:12
  • your first version doesn't work for me - it gives error `'module' object is not callable` . You make `import tkinter as Tk` so you should use `root = Tk.Tk()`, not `root = Tk()`. So maybe your second version doesn't work because you also use `root = Tk()` which gives error `'module' object is not callable`. Did you run it in console/terminal/cmd.exe to see errors? If you get error then you should add it in question. – furas Oct 14 '19 at 22:21
  • I don't see how this is a duplicate. All I want is a dialog window to pop up once and then I destroy it, I'm not constantly creating new Tk instances. All I want is to move that bit of code into a different module so I can use it in multiple programs by importing it. It does the exact same thing it always has, just from an imported module instead of the current one. – user2731076 Oct 15 '19 at 11:07
  • your current code works correctly for me - on Linux. So I don't know what is the problem. – furas Oct 15 '19 at 14:19

1 Answers1

0

Your both versions don't work. Both give 'module' object is not callable.

You have to use

 root = Tk.Tk()

instead of

 root = Tk()

and then both versions works.


Maybe two Tk in Tk.Tk() seems weird but usually we use lowercase tk instead of Tk in

import tkinter as tk

and then you have

root = tk.Tk()
furas
  • 134,197
  • 12
  • 106
  • 148