I use tkinter to build a simple GUI window with 3 buttons, and use matplotlib to show a simple figure. The process is: Push the 1st button to enable the 2nd button. Push the 2nd button to show a figure, and then enable the 3rd button. Push the 3rd button to show a figure and save the figure. However, the 3rd button can't be enabled after pushing the 2nd button. If I close the program, the errors occur. Another problem: why the saved picture is empty? Thanks.
import matplotlib.pyplot as plt
from tkinter import *
import numpy as np
class MyGUI:
def __init__(self):
Win=Tk()
Win.geometry('750x640')
Win.resizable(False,False)
Win.update()
w=Win.winfo_width()
h=Win.winfo_height()
Btn1=Button(Win,text='enable button 2',command=self.Btn1Listener)
Btn1.place(x=20,y=20)
Btn1.update()
self.Btn2=Button(Win,text='Plot 1',command=self.Btn2Listener)
self.Btn2.place(x=Btn1.winfo_x()+Btn1.winfo_width()+10,y=20,width=150)
self.Btn2.configure(state='disabled')
self.Btn2.update()
self.Btn3=Button(Win,text='plot 2',command=self.Btn3Listener)
self.Btn3.place(x=self.Btn2.winfo_x()+self.Btn2.winfo_width()+10,y=20,width=150)
self.Btn3.configure(state='disabled')
self.Btn3.update()
mainloop()
#-------------------------------------------------------
def Btn1Listener(self):
self.Btn2.configure(state='normal')
def Btn2Listener(self):
global v,s
v,s=self.B1()
self.Btn3.configure(state='normal')
def Btn3Listener(self):
self.B2(s,v)
#-------------------------------------------------------------
def B1(self):
x=np.arange(0,5,0.1)
y1=np.sin(x)
plt.plot(x,y1)
plt.show()
return 0,5
def B2(self, s,v):
x=np.arange(s,v,0.1)
y1=np.sin(x)
plt.plot(x,y1)
plt.show()
plt.savefig('aaa.png')
#------------------------------------------------------------------
if __name__ =='__main__':
MyApp=MyGUI()