考虑运行以下代码(请注意,这是一个极为简化的版本,以演示问题):
import matplotlib.pyplot as plot
from tkinter import * #Tkinter if your on python 2
def main():
fig = plot.figure(figsize=(16.8, 8.0))
root = Tk()
w = Label(root, text="Close this and it will hang!")
w.pack()
root.mainloop()
print('Code never reaches this point')
if __name__ == '__main__':
main()
关闭第一个窗口没问题,但关闭第二个窗口会导致代码挂起,因为 root.mainloop()
引起了无限循环。这个问题是因为调用了 fig = plot.figure(figsize=(16.8, 8.0))
所引起的。有谁知道如何在执行 matplotlib.pyplot 调用后成功关闭 root?
pyplot
还会启动自己的主循环,我猜这可能是你的问题所在。请参考 http://matplotlib.org/examples/user_interfaces/ 了解如何将 mpl 嵌入到你选择的 GUI 中。 - tacaswell