Python Forum
[WxPython] Help with passing arguments into a wx.Panel ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[WxPython] Help with passing arguments into a wx.Panel ?
#1
Aim is to pass some information to a panel when it is prepared.

I tried:
class SizerPanel(wx.Panel):
    def __init__(self, parent, id, pos, size, style, setori, givenpanels):
        wx.Panel.__init__(self, parent, id,
                          pos = wx.DefaultPosition,
                          size = wx.DefaultSize,
                          style = wx.TAB_TRAVERSAL|wx.NO_BORDER,
                          setori = wx.VERTICAL, 
                          givenpanels = ())
     
in combination with
        verti1pan = SizerPanel(self, id, wx.DefaultPosition, wx.DefaultSize,
                               wx.TAB_TRAVERSAL|wx.NO_BORDER,
                               setori = wx.VERTICAL, 
                               givenpanels = (panelOne, panelTwo))
I got an error:
Error:
Traceback (most recent call last):   File "...mehrpan01a.py", line 106, in <module>     frame = MainFrame()   File "....mehrpan01a.py", line 100, in __init__     panel = MainPanel(self)   File "...mehrpan01a.py", line 77, in __init__     givenpanels = (panelOne, panelTwo))   File "...mehrpan01a.py", line 47, in __init__     givenpanels = ())   File "...site-packages\wx-2.8-msw-unicode\wx\_windows.py", line 68, in __init__     _windows_.Panel_swiginit(self,_windows_.new_Panel(*args, **kwargs)) TypeError: new_Panel() takes at most 6 arguments (7 given)
Aren't there 7 in the _init_: 1. parent, 2. id, 3. pos, 4. size, 5. style, 6. setori, 7. givenpanels?
And if this way will not work, how can I send things into the panel during the creation process  from a superordinated panel?
Reply
#2
when calling, don't use self (unless the class your calling from is the parent widget), rather use refrence to parent widget
Reply
#3
I don't understand...
Do you mean:
class MainPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
 
        self.parent = parent

        # ... 

        verti1pan = SizerPanel(parent, id, wx.DefaultPosition, wx.DefaultSize,
                               wx.TAB_TRAVERSAL|wx.NO_BORDER,
                               setori = wx.VERTICAL, 
                               givenpanels = (panelOne, panelTwo))
The error remains with that as it has been.
Reply
#4
almost if  MainPanel called from outside of the MainPanel class, like):
# assume class instanciated as
MyPannel = MainPanel(root)
vertipan = MyPannel.SizerPanel(...)

# if called from within MainPanel class:
vertipan = self.SizerPanel(...)
Reply
#5
Your derived SizerPanel can accept whatever parameters you choose so satori and givenpanels is ok, but when calling the __init__ method of wx.panel they are not accepted parameters, accepted parameters are self, parent, id, pos, size, style & name)

The following is a working snippet of code(always try to supply a small runnable snippet of code to go along with your question) with the unaccepted parameters commented out and panelOne & panelTwo changed to strings so they are not required variables.
import wx


class SizerPanel(wx.Panel):

   def __init__(self, parent, id_, pos, size, style, setori, givenpanels):
       wx.Panel.__init__(
           self, parent, id_, pos=wx.DefaultPosition, size=wx.DefaultSize,
           style=wx.TAB_TRAVERSAL | wx.NO_BORDER,
           #             setori=wx.VERTICAL, givenpanels=()
       )


class MainFrame(wx.Frame):

   def __init__(self, *args, **kwargs):
       super(MainFrame, self).__init__(*args, **kwargs)
       verti1pan = SizerPanel(
           self, -1, wx.DefaultPosition, wx.DefaultSize,
           wx.TAB_TRAVERSAL | wx.NO_BORDER, setori=wx.VERTICAL,
           givenpanels=('panelOne', 'panelTwo'))


if __name__ == '__main__':
   app = wx.App(False)
   main_frame = MainFrame(None)
   main_frame.Show(True)
   app.MainLoop()
Reply
#6
My actual code is:

import wx
import wx.grid
 
class GridPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.grid = wx.grid.Grid(self, style=wx.BORDER_SUNKEN)
        self.grid.CreateGrid(25,8)
 
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.grid, 1, wx.EXPAND)
        self.SetSizer(sizer)
        self.SetAutoLayout(True)

 

class SizerPanel(wx.Panel):
    def __init__(self, parent, id, pos, size, style, setori, givenpanels):
        wx.Panel.__init__(self, parent, id,
                          pos = wx.DefaultPosition,
                          size = wx.DefaultSize,
                          style = wx.TAB_TRAVERSAL|wx.NO_BORDER,
                          setori = wx.VERTICAL, 
                          givenpanels = ())
        
        self.orient = setori
        self.panellist = givenpanels
        
        sizer = wx.BoxSizer(self.orient)
        for gp in givenpanels:
            sizer.Add(gp, 1, wx.EXPAND)

        self.setsizer(sizer)
        


class MainPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
 
        self.parent = parent

        panelOne = GridPanel(self)
        panelTwo = GridPanel(self)
        panelThree = GridPanel(self)
        panelFour = GridPanel(self)
        
 
        horisizer = wx.BoxSizer(wx.HORIZONTAL)
        verti1pan = SizerPanel(self, id, wx.DefaultPosition, wx.DefaultSize,
                               wx.TAB_TRAVERSAL|wx.NO_BORDER,
                               setori = wx.VERTICAL, 
                               givenpanels = (panelOne, panelTwo))
        verti2pan = SizerPanel(self, id, wx.DefaultPosition, wx.DefaultSize,
                               wx.TAB_TRAVERSAL|wx.NO_BORDER, wx.VERTICAL,
                               (panelThree, panelFour))
        horisizer.Add(verti1pan)
        horisizer.Add(verti2pan)
        self.SetSizer(horisizer)


class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title= " Demo ")
        panel = MainPanel(self)
        self.Show()

 
if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()
Yoritz: How can I pass setori and givenpanels to the panel, then?
In the mainframe I set them, I don't need them there in the _init_. I need them in the _init_ of the panel, as I know no other way to give the information about the panel contents into the panel during creation (at least not in a way that makes them available for use there). I could try to abuse the 'name' (by creating a name based to the information I want to set and 'decode' the name in the panel itself again), but that seems not the designed way...
 
Larz60+:
I'm still confused abotu the reference to the parent.
If I change line 50 to
verti1pan = self.SizerPanel(self, id, wx.DefaultPosition, wx.DefaultSize,
I get an error:
Error:
Traceback (most recent call last):   File "expl_mehrpan_demo01.py", line 71, in <module>     frame = MainFrame()   File "expl_mehrpan_demo01.py", line 65, in __init__     panel = MainPanel(self)   File "expl_mehrpan_demo01.py", line 50, in __init__     verti1pan = self.SizerPanel(self, id, wx.DefaultPosition, wx.DefaultSize, AttributeError: 'MainPanel' object has no attribute 'SizerPanel'
The version without 'self' seems to work better...?
Reply
#7
setori and givenpanels have already been passed into your derived panel in the line
def __init__(self, parent, id, pos, size, style, setori, givenpanels):
you just dont need to pass them to the base class panels __init__

So as stated before remove them so your SizerPanel class will be as follows.
class SizerPanel(wx.Panel):
   def __init__(self, parent, id, pos, size, style, setori, givenpanels):
       wx.Panel.__init__(self, parent, id,
                         pos = wx.DefaultPosition,
                         size = wx.DefaultSize,
                         style = wx.TAB_TRAVERSAL|wx.NO_BORDER)
        
       self.orient = setori
       self.panellist = givenpanels
        
       sizer = wx.BoxSizer(self.orient)
       for gp in givenpanels:
           sizer.Add(gp, 1, wx.EXPAND)

       self.setsizer(sizer)
Reply
#8
Oh... okay  Idea Exclamation !


I've never seen this construction before, having something in the def-brackets but not in the _init_.


But thank you very, very much! In this ways, it works.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Passing arguments into function, tkinter nanok66 3 4,964 Apr-18-2020, 11:53 PM
Last Post: nanok66
  [Tkinter] Help setting text/title in dock/panel in tkinter gui FluxApex 2 4,401 Mar-18-2020, 07:15 PM
Last Post: FluxApex
  video player inside a frame/panel in python raspberry pi desktop application MATHEWS 1 2,844 Dec-12-2018, 11:42 AM
Last Post: Larz60+
  Box Sizer inside of panel managed by wx.lib.agw.aui Larz60+ 1 3,457 Jun-13-2017, 07:25 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020