Python Matplotlib show()和tk messagebox
我将此代码与python2一起使用,在Python Matplotlib show()和tk messagebox,python,python-3.x,matplotlib,tkinter,Python,Python 3.x,Matplotlib,Tkinter,我将此代码与python2一起使用,在show()之后保存一个绘图: 现在使用python3,以下代码无法按预期工作: from tkinter import * import numpy as np import matplotlib.pyplot as plt import tkinter.messagebox class Main_app(Frame): def init(self): self.allplotbutton=Button(self, comm
show()之后保存一个绘图:
现在使用python3
,以下代码无法按预期工作:
from tkinter import *
import numpy as np
import matplotlib.pyplot as plt
import tkinter.messagebox
class Main_app(Frame):
def init(self):
self.allplotbutton=Button(self, command = self.allplot)
self.allplotbutton.configure(text="TEST")
self.allplotbutton.pack()
def allplot(self):
ta=np.linspace(0,2*np.pi,1000)
plt.plot(ta, np.sin(ta))
fig=plt.gcf()
plt.show()
if tkinter.messagebox.askyesno('askyesno','save plot?'):
print("Save plot")
fig.savefig("test.png")
fig.close()
def __init__(self, master=None):
Frame.__init__(self, master)
self.init()
self.pack()
def main():
app = Main_app()
app.pack()
app.mainloop()
if __name__ == "__main__":
main()
关闭打印窗口后,对话框不会出现。
当主窗口关闭时,将显示该对话框
似乎show()
函数接受了tkinter根窗口的某些内容
有什么解决办法吗
from tkinter import *
import numpy as np
import matplotlib.pyplot as plt
import tkinter.messagebox
class Main_app(Frame):
def init(self):
self.allplotbutton=Button(self, command = self.allplot)
self.allplotbutton.configure(text="TEST")
self.allplotbutton.pack()
def allplot(self):
ta=np.linspace(0,2*np.pi,1000)
plt.plot(ta, np.sin(ta))
fig=plt.gcf()
plt.show()
if tkinter.messagebox.askyesno('askyesno','save plot?'):
print("Save plot")
fig.savefig("test.png")
fig.close()
def __init__(self, master=None):
Frame.__init__(self, master)
self.init()
self.pack()
def main():
app = Main_app()
app.pack()
app.mainloop()
if __name__ == "__main__":
main()