显示 pylab 图的 Tkinter 菜单:退出按钮无法正常工作
Tkinter menu showing pylab plot: quit button not working properly
我正在使用 TKinter 编写 GUI 应用程序。
基本上我有一个菜单,我可以在其中 select 不同的功能。其中一个应该绘制图形,因此它打开了图形图。
在主 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
问题:当我 select 菜单中的绘图功能时,图形正确显示。我手动关闭它,但是当我尝试通过单击 "quit" 按钮退出应用程序时,我必须按两次才能关闭应用程序。
你知道为什么会这样吗?
我自己找到了解决方案。显然我的 matplotlib 中的 "show()" 方法默认设置为阻塞。所以我通过将 "block" 参数强制为 "False":
解决了这个问题
pylab.show(block = False)
我还删除了对以下电话的调用:
pylab.ioff()
pylab.close()
我正在使用 TKinter 编写 GUI 应用程序。 基本上我有一个菜单,我可以在其中 select 不同的功能。其中一个应该绘制图形,因此它打开了图形图。 在主 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
问题:当我 select 菜单中的绘图功能时,图形正确显示。我手动关闭它,但是当我尝试通过单击 "quit" 按钮退出应用程序时,我必须按两次才能关闭应用程序。 你知道为什么会这样吗?
我自己找到了解决方案。显然我的 matplotlib 中的 "show()" 方法默认设置为阻塞。所以我通过将 "block" 参数强制为 "False":
解决了这个问题pylab.show(block = False)
我还删除了对以下电话的调用:
pylab.ioff()
pylab.close()