我正在使用TKinter编写一个GUI应用程序。基本上,我有一个菜单,在那里我可以选择不同的功能。其中一个应该绘制一个图形,所以它打开了一个图形图。在主GUI上,我放置了一个"退出"按钮来关闭应用程序。这是我的代码示例:
主要.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 determinationnX=%.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
问题:当我从菜单中选择绘图功能时,图形会正确显示。我手动关闭它,但当我试图通过单击"退出"按钮退出应用程序时,我必须按两次才能关闭应用程序。你知道为什么会发生这种事吗?
我自己找到了解决方案。显然,我的matplotlib中的"show()"方法默认设置为阻塞。因此,我通过将"block"参数强制设置为"False"来解决问题:
pylab.show(block = False)
我还删除了对的调用
pylab.ioff()
pylab.close()