关闭 window 不会终止所有进程
Closing the window doesn't kill all processes
我有一个非常简单的程序,可以在按下按钮时显示一个简单的图表。我的问题是当我关闭应用程序 window 时,程序会保持 运行 直到我从终端将其终止。下面是我的代码,我的调查显示问题是由
引起的
matplotlib.use('TkAgg')
但是我不知道怎么解决!如果有帮助,我在 OSX 上 运行。
#!/usr/bin/python
from Tkinter import *
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
# ------ End of imports
class Ops:
def show_plot(self):
self.f, self.figarray = plt.subplots(1, sharex=True, sharey=True)
self.figarray.plot((1,2,3),(1,2,3))
plt.tight_layout()
self.canvas = FigureCanvasTkAgg(self.f, master=self.mainFrame)
self.canvas._tkcanvas.config(background='white', borderwidth=0, highlightthickness=0)
self.canvas._tkcanvas.pack(side=TOP, fill=BOTH)
class GUI(Ops):
def __init__(self, master):
self.master = master
self.width = self.master.winfo_screenwidth() # Width of the screen
self.height = self.master.winfo_screenheight() # Height of the screen
self.x = (self.width / 2)
self.y = (self.height / 2)
self.master.geometry("%dx%d+%d+%d" % (self.width, self.height, self.x, self.y))
self.mainFrame = Frame(self.master) # Generate the main container
self.mainFrame.pack()
# ---------- TOP FRAME ----------
self.topFrame = Frame(self.mainFrame)
self.topFrame.pack()
self.browse_button = Button(self.topFrame, text="Plot", command=self.show_plot)
self.browse_button.grid()
class App:
def __init__(self):
self.file_handler = Ops()
self.root = Tk()
self.gui_handler = GUI(self.root)
def run(self):
self.root.mainloop()
Application = App()
Application.run()
您需要调用 root.quit()
来结束 Tk.mainloop()。例如,参见 answer here.
解决方法很简单。只需使用
from matplotlib.figure import Figure
而不是
import matplotlib.pyplot as plt
在函数外部使用 root.mainloop
,应该可以解决您的问题。
我有一个非常简单的程序,可以在按下按钮时显示一个简单的图表。我的问题是当我关闭应用程序 window 时,程序会保持 运行 直到我从终端将其终止。下面是我的代码,我的调查显示问题是由
引起的matplotlib.use('TkAgg')
但是我不知道怎么解决!如果有帮助,我在 OSX 上 运行。
#!/usr/bin/python
from Tkinter import *
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
# ------ End of imports
class Ops:
def show_plot(self):
self.f, self.figarray = plt.subplots(1, sharex=True, sharey=True)
self.figarray.plot((1,2,3),(1,2,3))
plt.tight_layout()
self.canvas = FigureCanvasTkAgg(self.f, master=self.mainFrame)
self.canvas._tkcanvas.config(background='white', borderwidth=0, highlightthickness=0)
self.canvas._tkcanvas.pack(side=TOP, fill=BOTH)
class GUI(Ops):
def __init__(self, master):
self.master = master
self.width = self.master.winfo_screenwidth() # Width of the screen
self.height = self.master.winfo_screenheight() # Height of the screen
self.x = (self.width / 2)
self.y = (self.height / 2)
self.master.geometry("%dx%d+%d+%d" % (self.width, self.height, self.x, self.y))
self.mainFrame = Frame(self.master) # Generate the main container
self.mainFrame.pack()
# ---------- TOP FRAME ----------
self.topFrame = Frame(self.mainFrame)
self.topFrame.pack()
self.browse_button = Button(self.topFrame, text="Plot", command=self.show_plot)
self.browse_button.grid()
class App:
def __init__(self):
self.file_handler = Ops()
self.root = Tk()
self.gui_handler = GUI(self.root)
def run(self):
self.root.mainloop()
Application = App()
Application.run()
您需要调用 root.quit()
来结束 Tk.mainloop()。例如,参见 answer here.
解决方法很简单。只需使用
from matplotlib.figure import Figure
而不是
import matplotlib.pyplot as plt
在函数外部使用 root.mainloop
,应该可以解决您的问题。