gpt4 book ai didi

python - 如何在 wxPython 中使用 matplotlib 事件处理?

转载 作者:太空宇宙 更新时间:2023-11-03 18:36:41 25 4
gpt4 key购买 nike

我正在尝试修改 "Draggable Rectangle Exercise"在 matplotlib 文档中找到,以便它在 wxPython 窗口中工作。

到目前为止我有以下内容:

import wx

import matplotlib
matplotlib.interactive(True)
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

import numpy as np

class DraggableRectangle:
def __init__(self, rect):
self.rect = rect
self.press = None

def connect(self):
'connect to all the events we need'
self.cidpress = self.rect.figure.canvas.mpl_connect(
'button_press_event', self.on_press)
self.cidrelease = self.rect.figure.canvas.mpl_connect(
'button_release_event', self.on_release)
self.cidmotion = self.rect.figure.canvas.mpl_connect(
'motion_notify_event', self.on_motion)

def on_press(self, event):
'on button press we will see if the mouse is over us and store some data'
if event.inaxes != self.rect.axes: return

contains, attrd = self.rect.contains(event)
if not contains: return
print 'event contains', self.rect.xy
x0, y0 = self.rect.xy
self.press = x0, y0, event.xdata, event.ydata

def on_motion(self, event):
'on motion we will move the rect if the mouse is over us'
if self.press is None: return
if event.inaxes != self.rect.axes: return
x0, y0, xpress, ypress = self.press
dx = event.xdata - xpress
dy = event.ydata - ypress
#print 'x0=%f, xpress=%f, event.xdata=%f, dx=%f, x0+dx=%f'%(x0, xpress, event.xdata, dx, x0+dx)
self.rect.set_x(x0+dx)
self.rect.set_y(y0+dy)

#self.rect.figure.canvas.draw()
self.rect.figure.canvas.draw_idle()

def on_release(self, event):
'on release we reset the press data'
self.press = None
#self.rect.figure.canvas.draw()
self.rect.figure.canvas.draw_idle()

def disconnect(self):
'disconnect all the stored connection ids'
self.rect.figure.canvas.mpl_disconnect(self.cidpress)
self.rect.figure.canvas.mpl_disconnect(self.cidrelease)
self.rect.figure.canvas.mpl_disconnect(self.cidmotion)

class Frame(wx.Frame):
def __init__(self, title):
wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(800,600))

self.panel = wx.Panel(self)

self.figure = Figure(figsize=(6, 4), dpi=100)
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self.panel, wx.ID_ANY, self.figure)

rects = self.axes.bar(range(10), 20*np.random.rand(10))
drs = []
for rect in rects:
dr = DraggableRectangle(rect)
dr.connect()
drs.append(dr)

app = wx.App(redirect=False)
top = Frame("test")
top.Show()
app.MainLoop()

不幸的是,这并不像宣传的那样有效;条形出现在图表上,但根本不可拖动。按原样复制并粘贴示例工作正常,只有当我尝试将它与 wxPython 一起使用时才会遇到问题。

最佳答案

不要问我为什么,但将 drs 更改为 self.drs (第 75 和 79 行)似乎有效。就好像您必须阻止 drs 和所有 dr 被垃圾收集一样。 (与 Wx Matplotlib Event Handling 相关?)

import wx

import matplotlib
matplotlib.interactive(True)
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

import numpy as np

class DraggableRectangle:
def __init__(self, rect):
self.rect = rect
self.press = None

def connect(self):
'connect to all the events we need'
self.cidpress = self.rect.figure.canvas.mpl_connect(
'button_press_event', self.on_press)
self.cidrelease = self.rect.figure.canvas.mpl_connect(
'button_release_event', self.on_release)
self.cidmotion = self.rect.figure.canvas.mpl_connect(
'motion_notify_event', self.on_motion)

def on_press(self, event):
print "on_press"
'on button press we will see if the mouse is over us and store some data'
if event.inaxes != self.rect.axes: return

contains, attrd = self.rect.contains(event)
if not contains: return
print 'event contains', self.rect.xy
x0, y0 = self.rect.xy
self.press = x0, y0, event.xdata, event.ydata

def on_motion(self, event):
print "on_motion"
'on motion we will move the rect if the mouse is over us'
if self.press is None: return
if event.inaxes != self.rect.axes: return
x0, y0, xpress, ypress = self.press
dx = event.xdata - xpress
dy = event.ydata - ypress
#print 'x0=%f, xpress=%f, event.xdata=%f, dx=%f, x0+dx=%f'%(x0, xpress, event.xdata, dx, x0+dx)
self.rect.set_x(x0+dx)
self.rect.set_y(y0+dy)

#self.rect.figure.canvas.draw()
self.rect.figure.canvas.draw_idle()

def on_release(self, event):
print "on_release"
'on release we reset the press data'
self.press = None
#self.rect.figure.canvas.draw()
self.rect.figure.canvas.draw_idle()

def disconnect(self):
'disconnect all the stored connection ids'
self.rect.figure.canvas.mpl_disconnect(self.cidpress)
self.rect.figure.canvas.mpl_disconnect(self.cidrelease)
self.rect.figure.canvas.mpl_disconnect(self.cidmotion)

class Frame(wx.Frame):
def __init__(self, title):
wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(800,600))

self.panel = wx.Panel(self)

self.figure = Figure(figsize=(6, 4), dpi=100)
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self.panel, wx.ID_ANY, self.figure)

rects = self.axes.bar(range(10), 20*np.random.rand(10))
self.drs = []
for rect in rects:
dr = DraggableRectangle(rect)
dr.connect()
self.drs.append(dr)

print "test"
app = wx.PySimpleApp()#redirect=False)
top = Frame("test")
top.Show()
app.MainLoop()

关于python - 如何在 wxPython 中使用 matplotlib 事件处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21461597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com