【发布时间】:2015-01-24 04:50:11
【问题描述】:
我知道有很多线程指向同一个方向,但我找不到解决方案。许多线程即将关闭 GUI,但仍在运行 python 代码,这就是我不想要的。但请参阅下面的问题:
我目前正在使用 Python 2.7 和 Windows 7。我正在开发一个程序来分析我从传感器读取的数据。完成我的 python 程序后,我用 cx_freeze 冻结它,以便在没有 python 或 matplotlib 等的情况下在 pc 上执行它......我有 atm 的问题是我想添加一个关闭我的应用程序的退出按钮。问题是我尝试了 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 正在关闭,但程序仍在运行?我是否也退出了主循环,但我以为我用 root.quit() 退出了主循环?
其他 2 个按钮显示错误消息,程序在 Windows 7 上崩溃,但至少整个程序已关闭。我也尝试过像一些建议 root.quit 不带括号的人一样,但根本不起作用。
两个按钮的错误信息是:
致命的 Python 错误:PyEval_RestoreThread: NULL tstate
此应用程序已请求运行时终止它 不寻常的方式。请联系应用程序的支持团队了解更多信息 信息。
现在我的问题是如何确保我使用我的 GUI,绘制一些东西等等,如果我按下退出按钮,GUI 关闭并且整个程序也关闭?
非常感谢!最大
【问题讨论】:
标签: python python-2.7 loops user-interface tkinter