Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter窗口弹出,无法关闭_Python_User Interface_Tkinter_Tk - Fatal编程技术网

Python Tkinter窗口弹出,无法关闭

Python Tkinter窗口弹出,无法关闭,python,user-interface,tkinter,tk,Python,User Interface,Tkinter,Tk,我有一个tkinter GUI python代码,它为我的代码创建了一个GUI接口,在后面的代码中使用了snack sound toolkit,它也使用Tk,并使用root=Tk创建了一个实例。因为,以前GUI应用程序的主循环已经在运行,所以每当调用snack函数时,就会弹出一个新的空默认tk窗口。由于这种情况经常发生,所以当代码执行时,屏幕上有数百个空tk窗口。我尝试过使用多种方法来关闭它们,如root.destroy、root.draw、WM_DELETE_WINDOW等,但都没有解决方案。

我有一个tkinter GUI python代码,它为我的代码创建了一个GUI接口,在后面的代码中使用了snack sound toolkit,它也使用Tk,并使用root=Tk创建了一个实例。因为,以前GUI应用程序的主循环已经在运行,所以每当调用snack函数时,就会弹出一个新的空默认tk窗口。由于这种情况经常发生,所以当代码执行时,屏幕上有数百个空tk窗口。我尝试过使用多种方法来关闭它们,如root.destroy、root.draw、WM_DELETE_WINDOW等,但都没有解决方案。 在tkinter有什么方法可以做到这一点吗

import tkSnack
import thread

import Tkinter as tk

class my_gui(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)

        self.button = tk.Button(self, text="Get", command=self.on_button)
        self.button.grid(row=8)

    def on_button(self):

        thread1 = thread.start_new_thread(run, (PATH_TO_WAVE_FILE,))

def run(path):

    for k in range(10):

        PITCH_VALUES = snack_work(path)
        print PITCH_VALUES

def snack_work(INPUT_WAVE_FILE):
    # initializing the snack tool
    root = tk.Tk()
    tkSnack.initializeSnack(root)
    # root.withdraw()
    mysound = tkSnack.Sound()

# processing original wave file
    mysound.read(INPUT_WAVE_FILE)

    PITCH_VALUES = mysound.pitch()
    return PITCH_VALUES

app = my_gui()
app.mainloop()
让run和Snapshot_工作到应用程序对象的实例方法中,以便它们可以轻松访问该对象的属性。为了使用不依赖外部库或文件的最小MCVE,我使用simple print I'm on Python 3和after调用(而不是零食)测试了以下内容,因为我只想检查其他函数是否可以访问tkinter对象


我向你保证你用错了工具。发布一篇文章,这样我们就可以解决问题。@TigerhawkT3添加了一个小代码,显示了我是如何做的,同时也重现了这个问题。在零食工作的第一行,零食工具包不会启动Tk,你会。Snapshot需要一个Tk实例来运行,但是您的my_gui类已经初始化了Tk。我自己不使用零食,但我猜在零食工作中没有必要使用root=tk.tk。无论如何,调用多个Tk实例都是个坏主意。谢谢@TigerhawkT3。。。它是有效的。。。现在,将这一变化整合到大代码中的大任务来了:D
import tkSnack
import thread
import Tkinter as tk

class my_gui(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        self.button = tk.Button(self, text="Get", command=self.on_button)
        self.button.grid(row=8)

    def on_button(self):
        thread1=thread.start_new_thread(self.run,(PATH_TO_WAVE_FILE,))

    def run(self, path):
        for k in range(10):
            PITCH_VALUES = self.snack_work(path)
            print PITCH_VALUES

    def snack_work(self, INPUT_WAVE_FILE):
        ## initializing the snack tool
        tkSnack.initializeSnack(self) # use self instead of the separate root object
        # self.withdraw()
        mysound=tkSnack.Sound()

        ## processing original wave file
        mysound.read(INPUT_WAVE_FILE)

        PITCH_VALUES= mysound.pitch()
        return PITCH_VALUES

app = my_gui()
app.mainloop()