Python 3.x matplotlib/tkinter绘图关闭后如何终止程序

Python 3.x matplotlib/tkinter绘图关闭后如何终止程序,python-3.x,matplotlib,tkinter,Python 3.x,Matplotlib,Tkinter,我已经编写了一个python3.x脚本来从matplotlib生成一个绘图,tkinter用于一个对话框窗口来输入标题,然后浏览要使用的数据文件。经过大量测试,一切都正常工作,除了关闭绘图窗口后,程序仍在运行。我必须关闭终端窗口或重置Ipython/jupyter中的内核。当pyplot关闭时,有没有简单的方法来终止程序 请注意,我对编程非常陌生,其中很多都是从教程和其他论坛问题中拼凑而成的。我想一个可能的问题是我回溯matplotlib的方式,matplotlib说使用不是最好的方式,也许可以

我已经编写了一个python3.x脚本来从matplotlib生成一个绘图,tkinter用于一个对话框窗口来输入标题,然后浏览要使用的数据文件。经过大量测试,一切都正常工作,除了关闭绘图窗口后,程序仍在运行。我必须关闭终端窗口或重置Ipython/jupyter中的内核。当pyplot关闭时,有没有简单的方法来终止程序

请注意,我对编程非常陌生,其中很多都是从教程和其他论坛问题中拼凑而成的。我想一个可能的问题是我回溯matplotlib的方式,matplotlib说使用不是最好的方式,也许可以通过设置一个循环来找到解决方案,询问我是否想制作另一个绘图。另一种可能是将绘图放入tkinter窗口,但老实说,今天我自学了tkinter,将其作为打开浏览器选择数据文件的一种方法

import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
import numpy as np
import tkinter
from tkinter import Tk
from tkinter import ttk
from tkinter.filedialog import askopenfilename


plot_title = ""  #placeholder variable
wavelength = ""  #placeholder variable

def get_plot_title():
    global plot_title 
    plot_title = title_entry.get()
    title_entry.delete(0, tkinter.END)
    return plot_title

def get_wavelength():
    global wavelength
    wavelength = wavelength_entry.get()
    wavelength_entry.delete(0, tkinter.END)
    return wavelength

root = Tk()
root.geometry('400x400')

rows = 0
while rows < 10:
    root.rowconfigure(rows, weight=1)
    root.columnconfigure(rows,weight=1)
    rows += 1

title_label = ttk.Label(root, text="Enter TeX style name for plot title.\n (e.g., SrTiO3 would be SrTiO_{3})")
title_label.grid(row=1, column=4)

title_entry = ttk.Entry(root)
title_entry.grid(row=2, column=4)
title_entry.insert(0, 'Enter plot title here')

title_button = ttk.Button(root)
title_button.configure(text='Enter Plot Title', command=get_plot_title)
title_button.grid(row=3, column=4)


title_label = ttk.Label(root, text="Enter diffraction wavelength in Angstroms")
title_label.grid(row=5, column=4)

wavelength_entry = ttk.Entry(root)
wavelength_entry.grid(row=6, column=4)
wavelength_entry.insert(0, 'Enter wavelength here')

wavelength_button = ttk.Button(root)
wavelength_button.configure(text='Enter Wavelength', command=get_wavelength)
wavelength_button.grid(row=7, column=4)

root.mainloop()

##############

Tk().withdraw()

obs_file  = askopenfilename(title = "Select the Observed data file.")
calc_file = askopenfilename(title = "Select the Calcualted data file.")
diff_file = askopenfilename(title = "Select the Difference data file.")
hkl_file  = askopenfilename(title = "Select the hkl data file.")

obs_x, obs_y = np.loadtxt(obs_file, unpack=True)
plt.plot(obs_x, obs_y, 'k.', label='Observed', markersize = 3)

plt.yticks([])

calc_x, calc_y = np.loadtxt(calc_file, unpack=True)
plt.plot(calc_x, calc_y, 'r', label='Calculated')

diff_x, diff_y = np.loadtxt(diff_file, unpack=True)
diff_offset = 100
plt.plot(diff_x, diff_y - diff_offset, color='grey', label='Difference')

rslt_h, rslt_k, rslt_l, rslt_m, rslt_d, rslt_Th2, rslt_lp = np.loadtxt(hkl_file, unpack=True)
rslt_Th2_offset = np.full((len(rslt_Th2), 1), (min(diff_y)-150))

plt.plot(rslt_Th2, rslt_Th2_offset, 'k|', label='hkl')


plt.axis([min(obs_x), max(obs_x), None, None])
plot_title = "$" + str(plot_title) + "$"
plt.text(((max(obs_x)-min(obs_x))/2 ), max(obs_y), plot_title, fontsize=12)
x_axis_label = r'$2\theta (\degree), \lambda = $' + wavelength + ' $\AA $'
plt.xlabel(x_axis_label)
plt.ylabel('Intensity (arb. uints)')
plt.legend()
plt.show()

我用linux命令来实现这一点,它显示了所有打开的python进程,然后我用它的PID杀死它

ps aux | grep python
kill 1234

请尝试root.draw,而不是Tk.draw。在完成文件对话框后,调用root.destroy。我已尝试按照建议替换Tk.draw,但当我这样做时,我得到一个错误:TclError:无法调用wm命令:应用程序已被销毁;不管怎样,我不认为这是问题,因为我可以简单地删除撤回线在同一个结束点。如果在askopenfilename行之后添加root.destroy,则会出现以下错误:TclError:无法调用destroy命令:应用程序已被销毁OK,这只是猜测。我不是tkinter方面的专家,也没有像您那样将它与matplotlib结合使用。到目前为止,我所做的是在tkinter窗口中嵌入matplotlib图形,例如,它是在中完成的,并且一直有效。也许,如果没有更好的办法,你可以试着用这种方式调整你的程序。。。