首页 > 解决方案 > matplotlib/tkinter 绘图关闭后如何终止程序

问题描述

我已经编写了一个 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()

标签: python-3.xmatplotlibtkinter

解决方案


我使用 linux 命令执行此操作,它显示所有打开的 python 进程,然后我使用它的 PID 杀死它

ps aux | grep python
kill 1234

推荐阅读