I am having problems with a GUI I'm working on. The idea is to have a tree list of signals and be able to drag them onto the plot. Eventually having a long list of signals and multiple plots etc.. However the code segmentation faults after a seemingly random number of drag & drops (sometimes just one). I've stripped the code to the bare bones so it plots the same curve each time and there is only one "signal" to choose from; in this case just x^2.
Below I've posted the code with the packages it requires.
#===================================================================#
# Import Packages #
#=================#
import wx
import random
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
from matplotlib.figure import Figure
#====================================================================#
class MainFrame(wx.Frame):
''' Create the mainframe on which all of the other panels are placed.
'''
def __init__(self):
wx.Frame.__init__(self, parent=None, title="GUI", size=(998,800))
self.SetBackgroundColour('#CCCCCC')
self.GUIBox = wx.BoxSizer(wx.HORIZONTAL)
self.P = PlotWindow(self)
self.DD = DragDrop(self)
self.GUIBox.Add(self.DD, 0, wx.LEFT | wx.ALIGN_TOP)
self.GUIBox.Add(self.P, 0, wx.LEFT | wx.ALIGN_TOP)
self.SetSizer(self.GUIBox)
return
class PlotWindow(wx.Panel):
def __init__(self, parent):
wx.Window.__init__(self, parent)
self.Figure = Figure()
self.Figure.set_size_inches(8.56, 9.115)
self.C = FigureCanvasWxAgg(self, -1, self.Figure)
self.SP = self.Figure.add_subplot(111)
self.a = [0,1,2,3,4,5]
self.b = [0,1,4,9,16,25]
self.signals = [self.b]
def rePlot(self):
self.SP.clear()
self.SP.plot(self.a,self.signals[0])
self.C.draw()
class MyTextDropTarget(wx.TextDropTarget):
def __init__(self, objt):
wx.TextDropTarget.__init__(self)
self.Objt = objt
def OnDropText(self, x, y, data):
self.Objt.rePlot()
class DragDrop(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, style=wx.BORDER_RAISED)
self.SetBackgroundColour('#CCCCCC')
self.tree = wx.TreeCtrl(self, -1, size=(270,700))
# Add root
root = self.tree.AddRoot("Signals")
self.tree.AppendItem(root, "Square")
dt = MyTextDropTarget(self.GetParent().P)
self.GetParent().P.SetDropTarget(dt)
self.Bind(wx.EVT_TREE_BEGIN_DRAG, self.OnDragInit)
self.VBox = wx.BoxSizer(wx.VERTICAL)
self.VBox.Add(self.tree, 0)
self.SetSizer(self.VBox)
def OnDragInit(self, event):
text = self.tree.GetItemText(event.GetItem())
tdo = wx.TextDataObject(text)
tds = wx.DropSource(self.tree)
tds.SetData(tdo)
tds.DoDragDrop(True)
#====================================================================#
# Create App #
#============#
class App(wx.App):
def OnInit(self):
self.dis = MainFrame()
self.dis.Show()
return True
app = App()
app.MainLoop()
#====================================================================#
I've tried to take out as much unnecessary code as possible; any help would be much appreciated!
Cheers!