toti08
toti08

Reputation: 2464

Tkinter menu showing pylab plot: quit button not working properly

I am writing a GUI application using TKinter. Basically I have a menu where I can select different functions. One of this is supposed to plot a graph, so it opens a figure plot. On the main GUI I placed a "QUIT" button to close the application. Here is a sample of my code:

Main.py

from Tkinter import *
import ALSV_Plots

tk = Tk()
tk.title('ALS Verification v01-00')
tk.geometry('500x282')

def doneButton():
    tk.quit()    

def plotCoarseX():
    plot = ALSV_Plots.CoarseXPlot(showImage = True)
    plot.plotFunction()

menubar = Menu(tk)

plotMenu = Menu(menubar, tearoff=0)
plotMenu.add_command(label="Coarse X plot", command=plotCoarseX)

quitButton = Button(tk, 
                compound = LEFT, 
                image = exitIcon, 
                text ="  QUIT", 
                font = ('Corbel', 10), 
                command = doneButton)

quitButton.place(x = 400, y = 240)   

tk.mainloop()

ALSV_Plots.py

import pylab
import sharedVar


class CoarseXPlot():
    def __init__(self, showImage = True):
        self.show = showImage

    def plotFunction(self):        
        xSrcSlice, xLightSetSlice] = sharedVar.coarseXResult            
        pylab.ioff()
        figNum = getFigNumber()
        fig = pylab.figure(figNum, figsize=(10.91954, 6.15042))
        text = 'Coarse X determination\nX=%.5e, beam 4-Sigma=%.5e' % (beamPosition, beam4SigmaSize)
        fig.text(0.5, 0.95, text, horizontalalignment='center', verticalalignment='center')

        pylab.xlabel('X')
        pylab.ylabel('Light')
        pylab.plot(xSrcSlice, xLightSetSlice, 'bd')                                                                     

        pylab.grid(True)
        if self.show:
            pylab.show()
            pylab.close()

        return fig

Problem: when I select the plot function from the menu the figure is correctly displayed. I close it manually, but when I try to quit the application by clicking the "quit" button I have to press it twice in order to close the application. Do you have any idea why this is happening?

Upvotes: 0

Views: 306

Answers (1)

toti08
toti08

Reputation: 2464

I found the solution myself. Apparently the "show()" method in my matplotlib was set as blocking by default. So I solved the issue by forcing the "block" parameter to "False":

pylab.show(block = False)   

I also removed the calls to:

pylab.ioff()
pylab.close()

Upvotes: 1

Related Questions