【发布时间】:2013-07-06 07:46:54
【问题描述】:
考虑运行以下代码(注意这是一个非常简化的版本来演示问题):
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也启动了它自己的主循环,我怀疑这是你的问题。请参阅matplotlib.org/examples/user_interfaces 了解如何将 mpl 嵌入到您选择的 gui 中 -
有没有办法强制关闭 pyplot 主循环?该问题会影响 pyplot 调用后的所有后续 tkinter 窗口,即使在不同的模块中也是如此。
标签: python matplotlib tkinter