问题
I am a beginner with matplotlib. I am trying to generate bar chart on click of a button, which is working as expected. The problem is, I dont want the parent window i.e the window with "Hello World" button to be closed when I close the pyplot i.e bar chart window. I want only the pyplot to be closed when I click "x" on it.
import pygtk
pygtk.require('2.0')
import gtk
import matplotlib.pyplot as plt; plt.rcdefaults()
import numpy as np
import matplotlib.pyplot as plt
class HelloWorld:
def __init__(self):
self.a=[10,20,30,25]
self.b=[12,2,30,14]
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("destroy", self.destroy)
self.window.set_border_width(10)
self.button = gtk.Button("Hello World")
self.button.connect("clicked", self.hello, None)
self.window.add(self.button)
self.button.show()
self.window.show()
def hello(self, widget, data=None):
y_pos = np.arange(len(self.a))
error = np.random.rand(len(self.a))
plt.barh(y_pos, self.b, xerr=error, align='center', alpha=0.4)
plt.yticks(y_pos, self.a)
plt.xlabel('Players')
plt.title('Fastest Player')
plt.show()
def destroy(self, widget, data=None):
gtk.main_quit()
def main(self):
gtk.main()
if __name__ == "__main__":
hello = HelloWorld()
hello.main()
Any help will be appreciated! Thanks
来源:https://stackoverflow.com/questions/23804957/closing-of-matplotlib-pyplot-closes-parent-window-also