我正在使用的 GUI 有问题。这个想法是有一个信号树列表,并能够将它们拖到图上。最终会有一长串信号和多个绘图等。但是,在看似随机数量的拖放(有时只有一个)之后,代码分段错误。我已经将代码剥离到了最简单的部分,因此它每次都绘制相同的曲线,并且只有一个“信号”可供选择;在这种情况下只是 x^2。
下面我发布了代码及其所需的包。
import wx
import random
import scipy
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()
c = scipy.zeros(6)
for i in range(0, 6, 1):
c[i] = self.b[i]*random.uniform(0, 2)
self.SP.plot(self.a,c)
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)
class App(wx.App):
def OnInit(self):
self.dis = MainFrame()
self.dis.Show()
return True
app = App()
app.MainLoop()
我试图去掉尽可能多的不必要的代码;任何帮助将非常感激!
干杯!