显示pylab图的Tkinter菜单:退出按钮不能正常工作

时间:2015-03-05 08:13:01

标签: python matplotlib tkinter

我正在使用TKinter编写GUI应用程序。 基本上我有一个菜单,我可以选择不同的功能。其中一个应该是绘制图形,因此它打开了一个图形图。 在主GUI上,我放置了一个" QUIT"按钮关闭应用程序。 以下是我的代码示例:

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

问题:当我从菜单中选择绘图功能时,图形会正确显示。我手动关闭它,但当我尝试通过单击"退出"退出应用程序时按钮我必须按两次才能关闭应用程序。 你知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

我自己找到了解决方案。显然,我的matplotlib中的“show()”方法默认设置为阻塞。所以我通过将“block”参数强制为“False”解决了这个问题:

pylab.show(block = False)   

我也删除了对:

的调用
pylab.ioff()
pylab.close()