Posts: 82
Threads: 11
Joined: Sep 2016
Jan-25-2017, 03:42 PM
(This post was last modified: Jan-26-2017, 01:05 PM by Yoriz.)
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?
Posts: 12,030
Threads: 485
Joined: Sep 2016
when calling, don't use self (unless the class your calling from is the parent widget), rather use refrence to parent widget
Posts: 82
Threads: 11
Joined: Sep 2016
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.
Posts: 12,030
Threads: 485
Joined: Sep 2016
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(...)
Posts: 2,168
Threads: 35
Joined: Sep 2016
Jan-25-2017, 10:30 PM
(This post was last modified: Jan-25-2017, 10:30 PM by Yoriz.)
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()
Posts: 82
Threads: 11
Joined: Sep 2016
Jan-26-2017, 12:51 PM
(This post was last modified: Jan-26-2017, 12:53 PM by merlem.)
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...?
Posts: 2,168
Threads: 35
Joined: Sep 2016
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)
Posts: 82
Threads: 11
Joined: Sep 2016
Oh... okay  !
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.
|