Python GUI拖放问题

时间:2012-04-19 09:42:36

标签: python user-interface

我正在处理我正在使用的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()

我试图尽可能多地取出不必要的代码;任何帮助将不胜感激!

干杯!

0 个答案:

没有答案
相关问题