add button hide in canvas wxpython4

73 views
Skip to first unread message

Maj

unread,
May 24, 2019, 3:57:23 AM5/24/19
to wxPython-users

Capture du 2019-05-24 09-53-06.png

hi ,

i need to add hide button in canvas i the same panel how can i do that how i can have space to add the button ?

i have no idea if is possible to add button in two canvas button ' plot" like the picture :

that is my code with 2 panels and 2 canvas

import wx
from numpy import arange, sin, pi,cos
import numpy as np
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.widgets import RectangleSelector
from matplotlib.figure import Figure



class MainFrame(wx.Frame):
    def __init__(self, parent ):
        wx.Panel.__init__(self, parent,name="Main", size = (600,800))
        Top = PanelTop(self)
        Bottom = PanelBottom(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(Top, 1, wx.EXPAND)
        sizer.Add(Bottom, 1, wx.EXPAND)
        self.SetSizer(sizer)


class PanelTop(wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self,parent,size = (300,300))
        self.SetBackgroundColour('white')
        self.figure = Figure(figsize = (4,5))
        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)


        t = arange(0.5, 3.0, 0.01)
        s = cos(2 * pi * t)
        self.axes.plot(t, s)
       



class PanelBottom(wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self, parent, size = (300,300))
        self.SetBackgroundColour('grey77')
        self.figure = Figure(figsize = (4,5))
        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)

        t = arange(0.0, 3.0, 0.01)
        s = sin(2 * pi * t)
        self.axes.plot(t, s)
       

app = wx.App()
frame = MainFrame(None).Show()
app.MainLoop()

thank you

Tim Roberts

unread,
May 24, 2019, 1:25:57 PM5/24/19
to wxpytho...@googlegroups.com
Maj wrote:
>
> i need to add hide button in canvas i the same panel how can i do that
> how i can have space to add the button ?
> i have no idea if is possible to add button in two canvas button '
> plot" like the picture :

Of course it is.  You just need to decide where they should go. Here's a
version of your code with the buttons in between the two panels.

One little comment -- you had this in your code:

    class MainFrame(wx.Frame):
        def __init__(*self, parent):
            wx.Panel.__inot__(self, parent, name="Main, size=(600,800))

Note that you derived from wx.Frame, but you called the wx.Panel
constructor.  That's not proper.  It IS somewhat common you create a
frame and have it contain a panel which then holds all the other
objects, but the panel has to be created separately.


import wx
import numpy as np
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as
FigureCanvas
from matplotlib.figure import Figure


class MainFrame(wx.Frame):
    def __init__(self, parent ):
        wx.Frame.__init__(self, parent,name="Main", size = (600,800))
        Top = PanelTop(self)
        Bottom = PanelBottom(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(Top, 1, wx.EXPAND)

        buttonsizer = wx.BoxSizer(wx.HORIZONTAL)
        btn1 = wx.Button(self, -1, "Hide top" )
        btn2 = wx.Button(self, -1, "Hide bottom" )
        buttonsizer.AddMany((
            ((0,0), 1, wx.EXPAND),
            (btn1, 0, 0),
            (btn2, 0, 0),
            ((0,0), 1, wx.EXPAND),
        ))
        sizer.Add( buttonsizer, 0, wx.EXPAND )
        self.Bind( wx.EVT_BUTTON, self.onToggleTop, btn1 )
        self.Bind( wx.EVT_BUTTON, self.onToggleBot, btn2 )
        self.top = Top
        self.bot = Bottom

        sizer.Add(Bottom, 1, wx.EXPAND)
        self.SetSizer(sizer)

    def onToggleTop( self, evt ):
        evt.Skip()
        btn1 = evt.GetEventObject()
        if self.top.IsShown():
            btn1.SetLabel( "Hide top" )
        else:
            btn1.SetLabel( "Show top" )
        self.top.Show( not self.top.IsShown() )

    def onToggleBot( self, evt ):
        evt.Skip()
        btn1 = evt.GetEventObject()
        if self.bot.IsShown():
            btn1.SetLabel( "Hide bottom" )
        else:
            btn1.SetLabel( "Show bottom" )
        self.bot.Show( not self.bot.IsShown() )

class PanelTop(wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self,parent,size = (300,300))
        self.SetBackgroundColour('white')
        self.figure = Figure(figsize = (4,5))
        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)

        t = np.arange(0.5, 3.0, 0.01)
        s = np.cos(2 * np.pi * t)
        self.axes.plot(t, s)

class PanelBottom(wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self, parent, size = (300,300))
        self.SetBackgroundColour('grey77')
        self.figure = Figure(figsize = (4,5))
        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)

        t = np.arange(0.0, 3.0, 0.01)
        s = np.sin(2 * np.pi * t)
        self.axes.plot(t, s)

app = wx.App(0)
frame = MainFrame(None).Show()
app.MainLoop()


--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Majda El yacouby

unread,
May 25, 2019, 5:35:34 AM5/25/19
to wxPython-users
hi, 

thank you for your help that is good just what i need is to add the button just in PANELTOP not in frame or betwen the two panels just inside PANELTOP it possible to do that ? thank you very much 

Majda 

--
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 wxpython-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/1d6e7860-68bc-0b7a-7d06-e5b450d50c2e%40probo.com.
For more options, visit https://groups.google.com/d/optout.

Majda El yacouby

unread,
May 25, 2019, 5:36:38 AM5/25/19
to wxPython-users
that what i need is just in PANELTOP 

Le ven. 24 mai 2019 à 19:25, Tim Roberts <ti...@probo.com> a écrit :

Tim Roberts

unread,
May 26, 2019, 2:23:42 AM5/26/19
to 'Khanh D Dang' via wxPython-users
On May 25, 2019, at 2:35 AM, Majda El yacouby <majdaelya...@gmail.com> wrote:
>
> thank you for your help that is good just what i need is to add the button just in PANELTOP not in frame or betwen the two panels just inside PANELTOP it possible to do that ?

Why would you do that? When you hide PanelTop, the buttons will go way as well. If you need to hide the panels, then I don't think you want the buttons to be part of a panel.

Still, I would hope it is obvious how to make that change.

Majda El yacouby

unread,
May 26, 2019, 5:29:27 PM5/26/19
to wxPython-users
i need to do that cause i have lot of panel in my app when i can't add button in frame 

majda

--
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 wxpython-user...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages