3

I am writing a python program which executes the following sequence:
1. Dialog box to open/select a directory
2. perform certain operations
3. rename the file using a tkinter dialog box
4. Perform rest of the operations

I have written the following code:

def open_directory():
    directory_name = filedialog.askdirectory(title='Ordner Auswählen',parent=root)
    print(directory_name)
    root.destroy()

def input_name():
    def callback():
        print(e.get())
        root.quit()
    e = ttk.Entry(root)
    NORM_FONT = ("Helvetica", 10)
    label = ttk.Label(root,text='Enter the name of the ROI', font=NORM_FONT)
    label.pack(side="top", fill="x", pady=10)
    e.pack(side = 'top',padx = 10, pady = 10)
    e.focus_set()
    b = ttk.Button(root, text = "OK", width = 10, command = callback)
    b.pack()

def close_window():
    root.destory()

root = tk.Tk()
root.withdraw()
open_directory()  #dialogue box to select directory
<perform certain operations>
input_name()  #dialgue box for user input file name
root.mainloop()
close_window() #exit the mainloop of tkinter
<perform rest of the functions>

but I get the following error Tclerror: NULL main window I think it is realted to declaring root as the main window, but I dont seem to find where I have made the mistake. Is there some other method, which is better, for what I am trying to do here?

Gaurav Kumar
  • 33
  • 2
  • 6
  • Is there any idea behind `root.destroy()` in `open_directory`? This is a root of your problem. – CommonSense Nov 27 '18 at 13:47
  • Looks like you need to `withdraw` your root window before asking for directory and `deiconify` it later. Take a look at this [topic](https://stackoverflow.com/questions/1406145/how-do-i-get-rid-of-python-tkinter-root-window). – CommonSense Nov 27 '18 at 13:51
  • @CommonSense: tried that, still the same – Gaurav Kumar Nov 27 '18 at 13:57
  • @CommonSense when I add `root.dectroy()` in `open_directroy` I get the tcl error. without that code dosent execute the functions after `open_directory` – Gaurav Kumar Nov 27 '18 at 14:00

2 Answers2

1

As @CommonSense has mentioned, when you use withdraw to hide the main window, then you need to use the method deiconify to use the root again. Hence, change the function change_directory as follows:

def open_directory():
    directory_name = filedialog.askdirectory(title='Ordner Auswählen',parent=root)
    print(directory_name)
    root.deiconify()

If you do not deiconify the window, you could not call the function input_name, which makes use of the root window.

I have tested this code and it works.

PS: You also have a typo in the function close_window (when destroying the window).

David Duran
  • 1,786
  • 1
  • 25
  • 36
0

Your use of .destroy() and .quit() as @CommonSense truly said do not really seem well planned.

Not only that, you need to use triggers or events to control your function calls, else they just run straight the one preventing the other from running as is the case in your code.

You should also control when close_window() is called with an event:

from tkinter import filedialog
import tkinter as tk
def open_directory():
    directory_name = filedialog.askdirectory(title='Ordner Auswählen',parent=root)
    print(directory_name)
    #root.destroy()
    input_name()

def input_name():
    def callback():
        print(e.get())
        #root.quit()
    es_variable=tk.StringVar()
    e = tk.Entry(root, textvariable=es_variable)
    NORM_FONT = ("Helvetica", 10)
    label = tk.Label(root,text='Enter the name of the ROI', font=NORM_FONT)
    label.pack(side="top", fill="x", pady=10)
    e.pack(side = 'top',padx = 10, pady = 10)
    e.focus_set()
    b = tk.Button(root, text = "OK", width = 10, command = callback)
    b.pack()

def close_window():
    root.destory()

root = tk.Tk()
#root.withdraw()
open_dir_button = tk.Button(root, text = "Open Dialog", width = 10, command =open_directory)
open_dir_button.pack()
#dialogue box to select directory
#<perform certain operations>
#dialgue box for user input file name
root.mainloop()
#close_window() #exit the mainloop of tkinter
#<perform rest of the functions>
Samuel Kazeem
  • 787
  • 1
  • 8
  • 15