Python GUI Drag/Drop Problems - python

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 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()
I've tried to take out as much unnecessary code as possible; any help would be much appreciated!
Cheers!

Related

wxPython ScrolledWindow not working when used in a panel

I am having trouble nesting a ScrolledWindow inside a wx.Panel.
If I create a scrolled window on its own it seems to work, however when I create it inside a wx.Panel and add the wx.Panel to the frames sizer it does not. Is there anything that I am missing?
Note:
#pa = AScrolledWindow(self) <-- if uncommented this works
pa = ScrolledWindowHolder(self) # However this does not!
import wx
class ScrolledWindowHolder(wx.Panel):
def __init__(self, parent):
super(ScrolledWindowHolder, self).__init__(parent=parent)
mysizer = wx.GridBagSizer()
self.myscrolledWindow = AScrolledWindow(self)
mysizer.Add(self.myscrolledWindow, pos=(0, 0), flag=wx.EXPAND)
self.SetSizerAndFit(mysizer)
class AScrolledWindow(wx.ScrolledWindow):
def __init__(self, parent):
super(AScrolledWindow, self).__init__(parent)
gb = wx.GridBagSizer()
self.sizer = gb
self._labels = []
for y in xrange(1, 30):
self._labels.append(wx.StaticText(self, -1, "Label #%d" % (y,)))
gb.Add(self._labels[-1], (y, 1), (1, 1))
self.SetSizer(self.sizer)
self.SetScrollRate(5, 5)
self.EnableScrolling(True, True)
class TestFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'Programmatic size change')
sz = wx.BoxSizer(wx.VERTICAL)
#pa = AScrolledWindow(self)
pa = ScrolledWindowHolder(self)
sz.Add(pa, 1, wx.EXPAND)
self.SetSizer(sz)
def main():
wxapp = wx.App()
fr = TestFrame()
fr.Show(True)
wxapp.MainLoop()
if __name__ == '__main__':
main()
Not sure why but the issue appears to be with the fact that you are using a GridBagSizer with a single widget in ScrolledWindowHolder, which itself contains a GridBagSizer.
If you change ScrolledWindowHolder to use a BoxSizer it works, as expected.
class ScrolledWindowHolder(wx.Panel):
def __init__(self, parent):
super(ScrolledWindowHolder, self).__init__(parent=parent)
mysizer = wx.BoxSizer(wx.HORIZONTAL)
self.myscrolledWindow = AScrolledWindow(self)
mysizer.Add(self.myscrolledWindow, 1, wx.EXPAND,0)
self.SetSizerAndFit(mysizer)
Also, change the value of y to for y in range(1, 60): will demonstrate the scrolled window more effectively.

Update wxpython figure canvas with new figure

In a wxPython application I have an embedded a FigureCanvas with a matplotlib figure. I want to be able to switch the figure by loading a new one. However, the figure is not being updated.
Answers to similar topics suggests that panel.canvas.draw() and panel.Refresh() should do the trick, but I've also tried panel.Update() and panel.canvas.Refresh(). I fear that this only works if you want to redraw the canvas with the same figure?
So my question is: how do you replace the figure inside a canvas and make it update?
Below is a small (non-working) example. First a figure is loaded with a single axis. If you from the embedded shell type panel.LoadFigure() a new figure with 2x2 subplots is created and put into the canvas. But the new figure is not shown.
import numpy as np
import wx
from wx.py.shell import Shell
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
import matplotlib.pyplot as plt
class ShellPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.shell = Shell(self)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.shell, 1, wx.GROW)
self.SetSizer(self.sizer)
self.Layout()
self.Fit()
class FigurePanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.parent = parent
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.figure, ax = plt.subplots()
self.canvas = FigureCanvas(self, -1, self.figure)
self.shellpanel = ShellPanel(self)
s1 = wx.BoxSizer(wx.VERTICAL)
s1.Add(self.canvas, 0, wx.GROW)
s1.Add(self.shellpanel, 1 , wx.EXPAND)
self.sizer.Add(s1, 5, wx.GROW)
self.SetSizer(self.sizer)
self.Layout()
self.Fit()
def LoadFigure(self):
self.figure, ax = plt.subplots(2, 2)
self.canvas.draw()
self.Refresh()
class FigureFrame(wx.Frame):
def __init__(self, parent, id, title, size):
wx.Frame.__init__(self, parent, id, title, size=size)
if __name__ == "__main__":
app = wx.App(False)
fr = FigureFrame(None, -1, title='Figure Loader', size=(wx.DisplaySize()[0]/2, wx.DisplaySize()[1]*3/4))
panel = FigurePanel(fr)
fr.Show()
app.MainLoop()
Is there any reason you are trying to create a new Figure and not simply new Axes on the already defined Figure?
I would write your code like so:
def LoadFigure(self):
self.figure.clf() # clear current figure
# add arbitrary number of new Axes
self.figure.add_subplot(221)
self.figure.add_subplot(222)
self.figure.add_subplot(223)
self.figure.add_subplot(224)
self.canvas.draw() # refresh canvas
EDIT: following your comment, I think the problem is that you're creating a new figure, but your Canvas is still referencing the old one. I don't know if you can change that directly in the FigureCanvas properties, maybe someone with more experience can provide a better answer. For the moment, I would Destroy the previous canvas, and create a new FigureCanvas object with your new figure.
def LoadFigure(self):
self.figure, ax = plt.subplots(2, 2)
self.canvas.Destroy()
self.canvas = FigureCanvas(self, -1, self.figure)
self.canvas.draw()
Try using the Figure itself to make your figure, not pyplot. It has usually worked for me because it gives you more room to improvise.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import numpy as np
import wx
from wx.py.shell import Shell
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
import matplotlib.pyplot as plt
class ShellPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.shell = Shell(self)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.shell, 1, wx.GROW)
self.SetSizer(self.sizer)
self.Layout()
self.Fit()
class FigurePanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.parent = parent
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.figure = Figure(figsize = (8,6.1), dpi =60)
self.ax = self.figure.add_subplot(1,1,1)
self.enlarged_figure = Figure(figsize = (8,6.1), dpi = 100)
self.ax2 = self.enlarged_figure.add_subplot(2,1,2)
self.canvas = FigureCanvas(self, -1, self.figure)
self.canvas.Show()
self.enlarged_canvas = FigureCanvas(self,-1,self.enlarged_figure)
self.enlarged_canvas.Show()
self.shellpanel = ShellPanel(self)
s1 = wx.BoxSizer(wx.VERTICAL)
s1.Add(self.canvas, 0, wx.GROW)
s1.Add(self.shellpanel, 1 , wx.EXPAND)
self.sizer.Add(s1, 5, wx.GROW)
self.SetSizer(self.sizer)
self.Layout()
self.Fit()
def LoadFigure(self):
self.figure, ax = plt.subplots(2, 2)
self.canvas.draw()
self.Refresh()
class FigureFrame(wx.Frame):
def __init__(self, parent, id, title, size):
wx.Frame.__init__(self, parent, id, title, size=size)
if __name__ == "__main__":
app = wx.App(False)
fr = FigureFrame(None, -1, title='Figure Loader', size=(wx.DisplaySize()[0]/2, wx.DisplaySize()[1]*3/4))
panel = FigurePanel(fr)
fr.Show()
app.MainLoop()
In the shell type in 'panel.LoadFigure()' and the new figure will show up very quickly and then disappear. Then type 'panel.canvas.draw()' and the new figure will be there. Then type 'panel.LoadFigure()' to load the old figure back. Repeat.
I don't know why the figure remains hidden after the MainLoop() continues but this is sort of a quick fix to your problem.

Matplotlib in PyQt5: How to remove the small space along the edge

I have a matplotlib.pyplot object inside a QVBoxLayout in PyQt5 widget. There are small spaces along the edge which is really annoying. How do I remove it?
See the screensnap below.
I already tried setlayoutSpacing(0), which have no effect.
Here is the breviate code (Cutout many data processing codes):
class MpWidget_SCF(Qt.QWidget):
def __init__(self, parent=None, y=[]):
super(MpWidget_SCF, self).__init__()
self.setParent(parent)
self.dpi = 50
self.fig = MpPyplot.figure(figsize=(2, 2), dpi=self.dpi, )
self.SCF_subplot = MpPyplot.subplot(1, 1, 1)
self.canvas = MpFigureCanvas(self.fig)
self.canvas.setParent(self)
self.SCF_subplot.clear()
self.SCF_subplot.plot(range(len(y)), y, 'r')
self.canvas.draw()
self.vLayout = Qt.QVBoxLayout()
self.vLayout.addWidget(self.canvas)
self.vLayout.setSpacing(0)
self.setLayout(self.vLayout)
class myWidget(Qt.QWidget):
def __init__(self):
super(myWidget, self).__init__()
self.main = Ui_OutputWindow_Form()
self.main.setupUi(self)
self.main.SCF_Graphic_Widget = MpWidget_SCF(self)
self.main.verticalLayout_2.addWidget(self.main.SCF_Graphic_Widget)
self.show()
You can remove the default margins:
self.vLayout.setContentsMargins(0, 0, 0, 0)

Plot figure problems with python and matplotlib

I'm making an app that plots a figure after some processing. This is done after the user has introduced some values and pushes a button. However I don't get the figure plotted. Below there is a simplified code. This works fine if I plot directly the values of t and s, but not if it is done after pushing the button. What am I missing? Is there another better way to do so?
from numpy import arange, sin, pi
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure
import wx
class Input_Panel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
# Input variables
self.button = wx.Button(self, label="Go")
# Set sizer for the panel content
self.sizer = wx.GridBagSizer(1, 1)
self.sizer.Add(self.button, (1, 2), (3, 6), flag=wx.EXPAND)
self.SetSizer(self.sizer)
class Output_Panel_Var(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
# Output variables
self.tittle = wx.StaticText(self, label="OUTPUTS:")
self.font = wx.Font(12, wx.DECORATIVE, wx.BOLD, wx.NORMAL)
self.tittle.SetFont(self.font)
self.lblt = wx.StaticText(self, label="t:")
self.resultt = wx.StaticText(self, label="", size=(100, -1))
self.lbls = wx.StaticText(self, label="s:")
self.results = wx.StaticText(self, label="", size=(100, -1))
# Set sizer for the panel content
self.sizer = wx.GridBagSizer(2, 2)
self.sizer.Add(self.tittle, (1, 3))
self.sizer.Add(self.lblt, (3, 1))
self.sizer.Add(self.resultt, (3, 2))
self.sizer.Add(self.lbls, (4, 1))
self.sizer.Add(self.results, (4, 2))
self.SetSizer(self.sizer)
class Output_Panel_Fig(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(self, -1, self.figure)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.SetSizer(self.sizer)
def draw(self,t,s):
self.axes.plot(t, s)
class Main_Window(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title = title, pos = (0, 0), size = wx.DisplaySize())
# Set variable panels
self.main_splitter = wx.SplitterWindow(self)
self.out_splitter = wx.SplitterWindow(self.main_splitter)
self.inputpanel = Input_Panel(self.main_splitter)
self.inputpanel.SetBackgroundColour('#c4c4ff')
self.outputpanelvar = Output_Panel_Var(self.out_splitter)
self.outputpanelvar.SetBackgroundColour('#c2f1f5')
self.outputpanelfig = Output_Panel_Fig(self.out_splitter)
self.main_splitter.SplitVertically(self.inputpanel, self.out_splitter)
self.out_splitter.SplitHorizontally(self.outputpanelvar, self.outputpanelfig)
# Set event handlers
self.inputpanel.button.Bind(wx.EVT_BUTTON, self.OnButton)
def OnButton(self, e):
t = arange(0.0, 1.0, 0.01)
s = sin(2 * pi * t)
#self.outputpanelvar.resultt.SetLabel('%.5f' % t)
#self.outputpanelvar.resultt.SetLabel('%.5f' % s)
self.outputpanelfig.draw(t,s)
def main():
app = wx.App(False)
frame = Main_Window(None, "T-Matrix Codes GUI")
frame.Show()
app.MainLoop()
if __name__ == "__main__" :
main()
I think you are missing a redraw of the canvas. It is not enough to do a new plot but a refresh of the drawing pane must be done! Add a self.canvas.draw() after your plot command in the draw method of the Output_Panel_Fig this should help.
import ...
class Input_Panel(wx.Panel):
def __init__(self, parent):
...
class Output_Panel_Var(wx.Panel):
def __init__(self, parent):
...
class Output_Panel_Fig(wx.Panel):
def __init__(self, parent):
...
def draw(self,t,s):
self.axes.plot(t, s)
self.canvas.draw()
class Main_Window(wx.Frame):
def __init__(self, parent, title):
...
def OnButton(self, e):
...
def main():
...
if __name__ == "__main__" :
main()

wxPython: How to synchronize a grid with a matplotlib canvas

I'm trying to build a multypane wxAUI application with a multipane structure.
I have two class of objects that are controlled by an AuiManager (_mgr) that are:
1) wx.Grid.CSheet objects inserted into an AuiNotebook inside a pane
2) Matplotlib.figure objects also incapsulated into an other AuiNotebook inside the same AuiManager.
Here are the two classes defining the corresponding objects.
class MySheet(sheet.CSheet):
""" A Copy&Paste enabled grid class"""
def __init__(self, parent,NRows,NCols):
sheet.CSheet.__init__(self, parent)
wx.EVT_KEY_DOWN(self, self.OnKey)
self.SetLabelBackgroundColour('#DBD4D4')
self.SetNumberRows(NRows)
self.SetNumberCols(NCols)
self.NRows = NRows
self.NCols = NCols
def selection(self):
MORE CODE HERE
def OnKey(self, event):
MORE CODE HERE
def copy(self):
MORE CODE HERE
def paste(self):
MORE CODE HERE
def delete(self):
MORE CODE HERE
class Plot(wx.Panel):
def __init__(self, parent, id = -1, dpi = None, **kwargs):
wx.Panel.__init__(self, parent, id=id, **kwargs)
self.figure = mpl.figure.Figure(dpi=dpi, figsize=(2,2))
self.canvas = Canvas(self, -1, self.figure)
self.toolbar = Toolbar(self.canvas)
self.toolbar.Realize()
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.canvas,1,wx.EXPAND)
sizer.Add(self.toolbar, 0 , wx.LEFT | wx.EXPAND)
self.SetSizer(sizer)
class PlotNotebook(wx.Panel):
def __init__(self, parent, id = -1):
wx.Panel.__init__(self, parent, id=id)
self.nb = wx.aui.AuiNotebook(self)
sizer = wx.BoxSizer()
sizer.Add(self.nb, 1, wx.EXPAND)
self.SetSizer(sizer)
def add(self,name="plot"):
page = Plot(self.nb)
self.nb.AddPage(page,name)
return page.figure
AS default I have set two figures and two grid with two columns each correponding to X and Y data respectively. I would like to synchronize the X,Y data in a grid with the corresponding figure so that when these are changed also the figure is updated.
At creation time I'm able to fill the grids and figures with my data content, but I'm not able to updated the figure content after a change event such as when new data are pasted into the grid. I've tryed using after the update figure.canvas.draw() and _mgr.Update() but they seems not to work despite I don't get any error.

Categories

Resources