我已经编写了一个python3.x脚本来从matplotlib绘制图,并且tkinter用于对话框窗口中输入标题,然后浏览要使用的数据文件。经过大量测试后,一切都按我的要求运行,除了关闭绘图窗口后程序仍在运行。我必须关闭终端窗口或在Ipython / jupyter中重置内核。 pyplot关闭时,有没有一种简单的方法可以终止程序?
注意,我真的是编程新手,其中很多都是通过教程和其他论坛问题组合而成的。我在想一个可能的问题是我的后端方式(matplotlib说use()不是首选的方式),也许可以通过建立一个循环来询问是否要创建另一个循环来找到解决方案。情节。另一种可能性是将绘图放入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()
答案 0 :(得分:-1)
我使用linux命令执行此操作,它显示了所有打开的python进程,然后使用其PID杀死了它
ps aux | grep python
kill 1234