python - 关闭窗口不会杀死所有进程

标签 python tkinter osx-yosemite

我有一个非常简单的程序,可以在按下按钮时显示一个简单的图表。我的问题是当我关闭应用程序窗口时,程序会一直运行,直到我从终端将其终止。以下是我的代码,我的调查显示问题是由

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 .

关于python - 关闭窗口不会杀死所有进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30479061/

相关文章:

python - 带有Python请求的Elasticsearch:msearch请求必须以换行符终止

python - 枕头,文本居中不起作用,这是如何实现的?

python - 使用Python进行DNA测序

python - 如何制作处理繁重计算负载的可中断程序?

mysql - 无法在 OS X 10.10 上停止 MySQL

coding-style - 良好的语义,子类还是模拟?

python - 在 tkinter 的 Tk.after() 方法中使用 async/await 关键字

events - Tkinter 中的多个键事件绑定(bind) - "Control + E" "Command (apple) + E"等

objective-c - 带有 setBackgroundColor 的纹理 NSWindow 具有淡灰色背景

usb - Mac Os X 10.10 Yosemite 禁用 USB 端口