我是 python 的新手,我有一个无法克服的严重问题。我使用 wxpython 创建了一个 gui,它有两个文本字段和一个按钮。按下此按钮时,我调用一个函数,该函数调用另一个函数,该函数根据文本框中的输入创建一个饼图。问题是,如果用户不关闭图形并向文本框输入新值并再次按下按钮,程序就会崩溃而不是显示第二个图形。我试图在按下按钮时创建不同的线程,结果是一样的。
更具体地说:
这是单击按钮时调用的函数:
def statistics_button(self,event):
t=threading.Thread(target=self.m_button21OnButtonClick,args=(event,))
t.start()
print t
def m_button21OnButtonClick( self, event ):
'''some code here'''
fig=statistics.mmr_dist(mmr1,mmr2)
show()
首先调用 statistics_button,然后调用 m_button21OnButtonClick。 statistics.mmr_dist 函数如下:
'''some code'''
fig=pylab.figure(tit,figsize=(8,8),frameon=False)
ax=pylab.axes([0.1, 0.1, 0.8, 0.8])
pita=pylab.pie(db.values(), labels=tuple(db.keys()), autopct='%1.1f%%', shadow=True)
ti="%s-%s\n Maximum Distance = %s m\n Minimum Distance = %s m" % (ddf1.label,ddf2.label,maxdist,mindist)
title(ti, bbox={'facecolor':'0.8', 'pad':5})
'''some code'''
return fig
到目前为止,我已经了解 show() 命令会阻止函数 m_button21OnButtonClick 完成,因此在单击按钮时无法再次调用它,除非图窗已关闭。但这就是我实现不同线程的原因。虽然它似乎不起作用。
最佳答案
参见 this page有关让 pylab 与 wxPython 一起工作的建议——您可能真的不应该尝试(请参阅下一段)。问题是 pylab 使用的 Tkinter 与 wxPython 的运行副本不兼容。
最终,您应该只需要 embed your plots in wxPython .这非常有效,无论如何都是更好的用户体验。
关于python - 如何从 wxpython gui 的同一个按钮打开两个不同的 pylab 图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9814861/