Python 无错误地退出tkinter gui

Python 无错误地退出tkinter gui,python,python-2.7,loops,user-interface,tkinter,Python,Python 2.7,Loops,User Interface,Tkinter,我知道有很多线程指向同一个方向,但我找不到解决方案。很多线程即将关闭GUI,但仍在运行python代码,这是我不想要的。但请看下面我的问题: 我目前正在使用Python2.7和Windows7。我正在开发一个程序,分析从传感器读取的数据。在我完成python程序后,我用cx_freeze将其冻结,以便在没有python或matplotlib的pc上执行它。我遇到的问题是,我想添加一个退出按钮,关闭我的应用程序。问题是我尝试了3种不同的可能性,见下文: import Tkinter import

我知道有很多线程指向同一个方向,但我找不到解决方案。很多线程即将关闭GUI,但仍在运行python代码,这是我不想要的。但请看下面我的问题:

我目前正在使用Python2.7和Windows7。我正在开发一个程序,分析从传感器读取的数据。在我完成python程序后,我用cx_freeze将其冻结,以便在没有python或matplotlib的pc上执行它。我遇到的问题是,我想添加一个退出按钮,关闭我的应用程序。问题是我尝试了3种不同的可能性,见下文:

import Tkinter
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,  NavigationToolbar2TkAgg
import globals
import data2plot
#from __builtin__ import file
globals.init()

def plot(x, aw,temperature,water):
#function to plot via matplotlib in the gui

#global file and if someone refresh before load data, default data is test.csv
file = "test"

#Version1
def close_window():
    sys.exit()
#Version2    
def close_window2():
    root.quit()
#Version3        
def close_window3():
    root.destroy()


# GUI
root = Tkinter.Tk()


draw_button = Tkinter.Button(root, text="Quit", command = close_window)
draw_button.grid(row=1, column=2)

draw_button = Tkinter.Button(root, text="Quit2", command = close_window2)
draw_button.grid(row=1, column=3)

draw_button = Tkinter.Button(root, text="Quit3", command = close_window3)
draw_button.grid(row=1, column=4)

# init figure with the 3 different values and axes
fig = matplotlib.pyplot.figure()


canvas = FigureCanvasTkAgg(fig, master=root)
canvas.get_tk_widget().grid(row=0,column=1)
toolbar = NavigationToolbar2TkAgg(canvas, root)
toolbar.grid(row=1,column=1)

#starts loop for the figure
root.mainloop()
我已经读过,通常我应该使用root.quit()选项。但是,唯一可以正常工作的按钮是第三个带有root.destroy()的按钮。问题是,如果我使用第三个按钮,GUI将关闭,但程序仍在运行?我是否也要退出mainloop,但我认为我要使用root.quit()退出mainloop

其他两个按钮显示错误消息,程序在Windows 7上崩溃,但至少整个程序关闭。我也试着像一些人建议root.quit没有括号,但根本不起作用

两个按钮的错误消息为:

致命的Python错误:PyEval_RestoreThread:NULL tstate

此应用程序已请求运行时在 不寻常的方式。请联系应用程序的支持团队以了解更多信息 信息

现在我的问题是如何确保我使用GUI,绘制一些东西等等,如果我按下退出按钮,GUI关闭,整个程序也关闭

非常感谢!Max

Hmm.
root.quit()
一直对我有效,没有错误,但如果不是对你,也许这是最好的方法,因此你可以关闭GUI和程序:

root.destroy()
sys.exit()

当我试图从另一个线程关闭应用程序时遇到了问题,所以我使用了这段代码(它将从GUI线程调用quit)

def exitApp(self):
    self.root.after(100,root.quit)