我有一个tkinter GUI python代码,它为我的代码创建了一个gui接口,在代码中使用了后来的snack sound工具包(它也使用了Tk并使用root = Tk()创建了一个实例)。因为,以前的GUI应用程序的主循环已经运行到每次小吃功能被称为弹出一个新的空默认tk窗口。由于这种情况发生了很多,当这段代码执行时,屏幕上有数百个空的tk窗口。我试图使用root.destroy,root.withdraw,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()
答案 0 :(得分:0)
将run()
和snack_work()
放入app
对象的实例方法中,以便他们可以轻松访问该对象的属性。为了使用不依赖于外部库或文件的更小的MCVE,我使用简单的print()
(我在Python 3上)和after()
调用而不是snack
这些东西,因为我想检查的是其他函数可以访问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(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()