Tkinter在Python3.6中给出了以下错误:TclError:NULL主窗口

Tkinter在Python3.6中给出了以下错误:TclError:NULL主窗口,python,python-3.x,tkinter,tk,Python,Python 3.x,Tkinter,Tk,我正在编写一个python程序,它执行以下序列: 1.打开/选择目录的对话框 2.执行某些操作 3.使用tkinter对话框重命名文件 4.执行其余的操作 我编写了以下代码: def open_directory(): directory_name = filedialog.askdirectory(title='Ordner Auswählen',parent=root) print(directory_name) root.destroy() def input_n

我正在编写一个python程序,它执行以下序列: 1.打开/选择目录的对话框 2.执行某些操作 3.使用tkinter对话框重命名文件 4.执行其余的操作

我编写了以下代码:

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>
但是我得到了以下错误 错误:主窗口为空 我认为这与将root声明为主窗口有关,但我似乎没有发现我在哪里犯了错误。
对于我在这里尝试做的事情,还有其他更好的方法吗?

正如@CommonSense所提到的,当您使用draw隐藏主窗口时,您需要使用deiconify方法再次使用根。因此,将函数change_directory更改如下:

def open_directory():
    directory_name = filedialog.askdirectory(title='Ordner Auswählen',parent=root)
    print(directory_name)
    root.deiconify()
如果不去定义窗口,则无法调用函数input_name,该函数使用根窗口

我已经测试了这段代码,它是有效的

PS:销毁窗口时,关闭窗口的功能也有输入错误。

您使用.destroy and.quit时,正如@CommonSense所说的那样,似乎并没有很好的计划

不仅如此,您还需要使用触发器或事件来控制函数调用,否则它们会直接运行,就像代码中的情况一样,阻止另一个运行

您还应控制何时使用事件调用close_window:

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>

在open_目录中的root.destroy背后有什么想法吗?这是您的问题的根源。看起来您需要在请求目录之前撤消根窗口,然后再对其进行解密。看看这个。@CommonSense:试过了,还是same@CommonSense当我在open_directroy中添加root.dectroy时,我得到了tcl错误。如果没有这些代码,在打开目录后就不会执行函数。你的答案是有效的,但是大卫·杜兰给出的答案与我的需要非常相关。