wxPython Layout

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

wxPython Layout

Mark Donoghue
Hello,

Below is the code I'm working on that uses matplotlib to graph an equation with wx.TextCtrl below to enter numbers in the equation. The problem I'm having is when I set the TextCtrls to auto expand when the window in resized it leaves a large grey gap between the matplotlib canvas and the numbers. Can anybody help me out and show me what I'm doing wrong? The code is below:

'''
Shows a matplotlib graph in a frame with y = a + bx + cx^2 + dx^3 in range (-1,1).
A,B,C, and D are pre-defined for now.

'''

import wx
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigCanvas

import numpy as np
 
a = float(0)
b = float(0)
c = float(0)
d = float(0)
 
class MyFrame(wx.Frame):
 def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.SetTitle("wx plot")
        self.__do_layout()
        self.draw_figure()    
   
 def __do_layout(self):
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer)

        # Create matplotlib figure
        self.fig = Figure(figsize=(5.0, 4.0), dpi=100)
        self.canvas = FigCanvas(self, -1, self.fig)
        self.ax = self.fig.add_subplot(111)
        # Set event when plot is clicked
    self.canvas.mpl_connect('pick_event', self.on_pick)
        # Add figure
        sizer.Add(self.canvas, 0, wx.EXPAND)

        self.btn1 = wx.Button(self, -1, 'Graph')
        self.Bind(wx.EVT_BUTTON, self.draw_figure1, self.btn1)
        sizer.Add(self.btn1, 0, wx.EXPAND)
       
        self.textbox1 = wx.TextCtrl(self, size=(1,-1), style=wx.TE_PROCESS_ENTER)
        self.Bind(wx.EVT_TEXT, self.draw_figure1, self.textbox1)
        sizer.Add(self.textbox1, 0, wx.EXPAND)
       
        self.textbox2 = wx.TextCtrl(self, size=(1,-1), style=wx.TE_PROCESS_ENTER)
        self.Bind(wx.EVT_TEXT, self.draw_figure1, self.textbox2)
        sizer.Add(self.textbox2, 0, wx.EXPAND)
       
        self.textbox3 = wx.TextCtrl(self, size=(1,-1), style=wx.TE_PROCESS_ENTER)
        self.Bind(wx.EVT_TEXT, self.draw_figure1, self.textbox3)
        sizer.Add(self.textbox3, 0, wx.EXPAND)
       
        self.textbox4 = wx.TextCtrl(self, size=(1,-1), style=wx.TE_PROCESS_ENTER)
        self.Bind(wx.EVT_TEXT, self.draw_figure1, self.textbox4)
        sizer.Add(self.textbox4, 0, wx.EXPAND)
       
        '''
        #Set default text values
        self.textbox1.SetValue('0')
        self.textbox2.SetValue('0')
        self.textbox3.SetValue('0')
        self.textbox4.SetValue('0')
        '''
       
        box = wx.BoxSizer(wx.HORIZONTAL)
        box.Add(self.btn1, 1 )
        box.Add(self.textbox1, 1)
        box.Add(self.textbox2, 1)
        box.Add(self.textbox3, 1)
        box.Add(self.textbox4, 1)

        sizer.Add(box)
        self.Centre()
        sizer.Fit(self)
        self.Layout()

 def draw_figure1(self, event):
        global a
        global b
        global c
        global d
        try:
                if not(self.textbox1.IsEmpty()):
                        a = float(self.textbox1.GetValue())
                else:
                        a = float(0)
                if not(self.textbox2.IsEmpty()):
                        b = float(self.textbox2.GetValue())
                else:
                        b = float(0)
                if not(self.textbox3.IsEmpty()):
                        c = float(self.textbox3.GetValue())
                else:
                        c = float(0)
                if not(self.textbox4.IsEmpty()):
                        d = float(self.textbox4.GetValue())
                else:
                        d = float(0)
        except ValueError:
                wx.MessageBox("Invalid input. Must be floating point numbers","Error",
                    wx.OK|wx.ICON_EXCLAMATION)
                return
        self.draw_figure()
       
 def draw_figure(self):
        self.ax.clear()
        x = np.arange(0,1,0.001)
        y = my_formula(x)
        i = 0
        print 'Line being graphed: ',a, '+',b,'x+',c,'x^2+',d,'x^3'
        print 'Every 100th plot of the line:'
        while i<len(x):
                print i, ':', x[i], ',', y[i]
                i = i + 100
        self.ax.plot(x,y,picker=5)
        self.canvas.draw()
 def on_pick(self, event):
        print 'hello world'

# end of class MyFrame
def my_formula(x):
        return a+b*x+c*x**2+d*x**3
 
if __name__ == "__main__":
 app = wx.PySimpleApp(0)
 frame = MyFrame(None, -1, "")
 app.SetTopWindow(frame)
 frame.Show()
 app.MainLoop()

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: wxPython Layout

Tim Roberts
[hidden email] wrote:
> Below is the code I'm working on that uses matplotlib to graph an equation with wx.TextCtrl below to enter numbers in the equation. The problem I'm having is when I set the TextCtrls to auto expand when the window in resized it leaves a large grey gap between the matplotlib canvas and the numbers. Can anybody help me out and show me what I'm doing wrong? The code is below:

There are several problems here.

First, you are adding the text controls to BOTH the vertical box sizer
and the horizontal box sizer.  You can see that when you change the
window; the boxes realign themselves in unpredictable ways.  You want
the button and text controls to belong to the horizontal sizer, and the
horizontal sizeer should be part of the vertical sizer.  So, you should
remove the "sizer.Add" calls for everything except the canvas and the
horizontal sizer.

Next, if you want the plot to expand to fill the vertical space, you
need to give it a proportion value.  Change this:
        # Add figure
        sizer.Add(self.canvas, 0, wx.EXPAND)
to this:
        # Add figure
        sizer.Add(self.canvas, 1, wx.EXPAND)

I suspect you probably want the horizontal sizer to expand to fit the
width, so you should change
        sizer.Add(box, 0)
to this:
        sizer.Add(box, 0, wx.EXPAND)

After that, your example seems to work for me.

--
Tim Roberts, [hidden email]
Providenza & Boekelheide, Inc.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: wxPython Layout

Mark Donoghue
Thank you do much! I'm still a noob to Python.

On Wed, Apr 19, 2017 at 12:36 PM, Tim Roberts <[hidden email]> wrote:
[hidden email] wrote:
> Below is the code I'm working on that uses matplotlib to graph an equation with wx.TextCtrl below to enter numbers in the equation. The problem I'm having is when I set the TextCtrls to auto expand when the window in resized it leaves a large grey gap between the matplotlib canvas and the numbers. Can anybody help me out and show me what I'm doing wrong? The code is below:

There are several problems here.

First, you are adding the text controls to BOTH the vertical box sizer
and the horizontal box sizer.  You can see that when you change the
window; the boxes realign themselves in unpredictable ways.  You want
the button and text controls to belong to the horizontal sizer, and the
horizontal sizeer should be part of the vertical sizer.  So, you should
remove the "sizer.Add" calls for everything except the canvas and the
horizontal sizer.

Next, if you want the plot to expand to fill the vertical space, you
need to give it a proportion value.  Change this:
        # Add figure
        sizer.Add(self.canvas, 0, wx.EXPAND)
to this:
        # Add figure
        sizer.Add(self.canvas, 1, wx.EXPAND)

I suspect you probably want the horizontal sizer to expand to fit the
width, so you should change
        sizer.Add(box, 0)
to this:
        sizer.Add(box, 0, wx.EXPAND)

After that, your example seems to work for me.

--
Tim Roberts, [hidden email]
Providenza & Boekelheide, Inc.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.



--

-Mark

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...