【发布时间】:2015-08-09 08:06:59
【问题描述】:
我有一个非常简单的程序,可以在按下按钮时显示一个简单的图。我的问题是当我关闭应用程序窗口时,程序会一直运行,直到我从终端中将其杀死。以下是我的代码,我的调查显示问题是由
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()
【问题讨论】:
-
上面的代码是正确的;必须有其他东西阻止该过程停止。您是创建子进程还是守护线程?
-
@AaronDigulla 绘制数据时会出现问题。否则退出正常。我现在已经添加了带有虚拟数据的整个代码,以便可以复制它。
-
在绘制数据时退出时是否挂起?绘图完成后它会挂起吗?
-
@AaronDigulla 在绘制数据后关闭窗口时它挂起。
-
这真的已经是一个最小的例子了吗?
标签: python tkinter osx-yosemite