我试图让一些eventlisteners工作,而且在课堂上似乎没有做任何事情 - 甚至没有打印信息:
from pylab import *
from cmath import *
class integrator:
def __init__(self, func, fig):
self.debug = False
self.pressed = False
self.firstPt = None
self.lastPt = None
self.isum = 0
cid = fig.canvas.mpl_connect('button_press_event', self.onclick)
fig.canvas.mpl_connect('motion_notify_event', self.onmove)
fig.canvas.mpl_connect('button_release_event', self.onUp)
#def plotpt(self, z, mark=','):
#ax.plot(z.real, z.imag, 'k'+mark)#black point.
#try:
#result = eval(func)
#if debug:
#print 'result',result
#ax.plot(result.real, result.imag, 'r'+mark)#red result point.
#except ZeroDivisionError:
#print "Undefined/infinite (/0) at", z
def onclick(self, event):
self.lastPt = event.xdata + 1j*event.ydata
self.firstPt = self.lastPt
self.isum = 0
print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
event.button, event.x, event.y, event.xdata, event.ydata)
def onmove(self, event):
if event.xdata is not None and event.ydata is not None and lastPt is not None :
print event.xdata, event.ydata
pt = event.xdata + 1j*event.ydata
diff = abs(pt - lastPt)
z = lastPt
nth = eval(func)
z = pt
nxt = eval(func)
part = (nth+nxt)/2 * diff
print nth,nxt,diff
isum += part
def onUp(self, event):
print 'UP button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
event.button, event.x, event.y, event.xdata, event.ydata)
print "From",self.firstPt,"to",self.lastPt,"along f(z) =",func,':'
print self.isum
print "Note: phase(z) is argument, z.conjugate() is conj, z.real, z.imag are re/im parts."
func = raw_input('f(z)=')
fig = plt.figure("Complex function f(z)="+func)
ax = fig.add_subplot(1,1,1)
ax.set_xbound(-3,3)
ax.set_ybound(-3,3)
#ax.plot(np.random.rand(10))
fig.set_label('F(z)='+func)
plt.draw()
integrator(func, fig)
plt.draw()
show()#looks like nothing after this line runs.
在我进入课堂之前至少部分工作:这里至少会打印出一些东西,但是找不到你在屏幕上点击并拖动的任何曲线的复杂曲线积分(总是说它是0):
from pylab import *
from cmath import *
debug = False
pressed = False
firstPt = None
lastPt = None
isum = 0
def plotpt(z, mark=','):
ax.plot(z.real, z.imag, 'k'+mark)#black point.
try:
result = eval(func)
if debug:
print 'result',result
ax.plot(result.real, result.imag, 'r'+mark)#red result point.
except ZeroDivisionError:
print "Undefined/infinite (/0) at", z
def onclick(event):
lastPt = event.xdata + 1j*event.ydata
firstPt = lastPt
isum = 0
print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
event.button, event.x, event.y, event.xdata, event.ydata)
def onmove(event):
if event.xdata is not None and event.ydata is not None and lastPt is not None :
print event.xdata, event.ydata
pt = event.xdata + 1j*event.ydata
diff = abs(pt - lastPt)
z = lastPt
nth = eval(func)
z = pt
nxt = eval(func)
part = (nth+nxt)/2 * diff
print nth,nxt,diff
isum += part
def onUp(event):
print 'UP button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
event.button, event.x, event.y, event.xdata, event.ydata)
print "From",firstPt,"to",lastPt,"along f(z) =",func,':'
print isum
print "Note: phase(z) is argument, z.conjugate() is conj, z.real, z.imag are re/im parts."
func = raw_input('f(z)=')
fig = plt.figure("Complex function f(z)="+func)
ax = fig.add_subplot(1,1,1)
ax.set_xbound(-3,3)
ax.set_ybound(-3,3)
#ax.plot(np.random.rand(10))
fig.set_label('F(z)='+func)
plt.draw()
cid = fig.canvas.mpl_connect('button_press_event', onclick)
fig.canvas.mpl_connect('motion_notify_event', onmove)
fig.canvas.mpl_connect('button_release_event', onUp)
plt.draw()
show()#looks like nothing after this line runs.
答案 0 :(得分:0)
这段代码存在很多问题,但是关于为什么在显示绘图窗口之后没有发生任何事情的问题,答案很简单:
您需要保留对所使用对象的引用。
这意味着,您需要将类实例存储在变量
中integ = integrator(func, fig)
您还需要将事件处理程序存储在类变量
中self.cid = fig.canvas.mpl_connect('button_press_event', self.onclick)
self.cid2 = fig.canvas.mpl_connect('motion_notify_event', self.onmove)
self.cid3 = fig.canvas.mpl_connect('button_release_event', self.onUp)