Search code examples
pythonwxpythonwxwidgets

wxPython wx.ScrolledWindow insert wx.Panel


I am trying to insert a wx.Panel into a wx.ScrolledWindow. I have a wx.Panel object named self.entTitle that have two input fields for Title and Date. I have a few other objects I want to add in the scrolledwindow, but I want to get this one working first before I go on to the others. Here is my code:

main.py

import wx
from EntryScrollPanel import EntryScrollPanel

class MyFrame(wx.Frame):
    def __init__(self, parent, ID, title):
        wx.Frame.__init__(self, parent, ID, title=title, size=(850,725))

        # Creating Panels
        self.main = wx.Panel(self)
        # Create a notebook on the panel
        self.nb = wx.Notebook(self.main, 1)

        # create the page windows as children of the notebook
        entryPg = EntryScrollPanel(self.nb, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, style=wx.VSCROLL)

        # add the pages to the notebook with the label to show on the tab
        self.nb.AddPage(self.userFCode, "FCodes")

        # Create sizers
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)

        # Adding Objects to mainSizer
        self.mainSizer.AddSpacer(10)
        #self.mainSizer.Add(self.mainLogin, 1, wx.ALL|wx.EXPAND)
        self.mainSizer.Add(self.nb, 1, wx.ALL|wx.EXPAND)

        # Set main sizer
        self.main.SetAutoLayout(True)
        self.main.SetSizer(self.mainSizer)
        self.mainSizer.Fit(self.main)
        self.Layout()
        self.Centre(wx.BOTH)
        self.Show()

app = wx.App(False)
frame = MyFrame(None, -1, 'App UI')
app.MainLoop()

EntryScrollPanel.py

import wx
from titlePanel import titlePanel

class EntryScrollPanel(wx.ScrolledWindow):
    def __init__(self, parent, ID, pos, size, style):
        #self.SetScrollRate( 5, 5 )

        self.entryPgBox = wx.BoxSizer(wx.VERTICAL)

        #self.entTitle = titlePanel(self, -1) i've tried this as well with no success
        self.entTitle = titlePanel(wx.Panel, -1)

        self.entryPgBox.AddSpacer(10)
        self.entryPgBox.Add(self.entTitle, 0, wx.EXPAND)
        self.entryPgBox.AddSpacer(10)

        self.SetAutoLayout(True)
        self.SetSizer(self.entryPgBox)
        self.entryPgBox.Fit(self)

titlePanel.py

import wx

class titlePanel(wx.Panel):
    def __init__(self, parent, ID):
        wx.Panel.__init__(self, parent, ID)
        titleSizer = wx.BoxSizer(wx.HORIZONTAL)

        titleLbl = wx.StaticText(self, label="Title: ")
        titleTxt = wx.TextCtrl(self, size=(140,-1))
        dateLbl = wx.StaticText(self, label="Date: ")
        dateCal = wx.DatePickerCtrl(self, wx.DP_DROPDOWN)

        titleSizer.Add(titleLbl,0,wx.EXPAND)
        titleSizer.Add(titleTxt,1,wx.EXPAND)
        titleSizer.Add(dateLbl,0,wx.EXPAND)
        titleSizer.Add(dateCal,0,wx.EXPAND)

This is the error I get:

Traceback (most recent call last):
  File "C:/Users/JLP_COM1/PycharmProjects/wxPython/wxPythonHelloWorld.py", line 283, in <module>
    frame = MyFrame(None, -1, 'Small editor')
  File "C:/Users/JLP_COM1/PycharmProjects/wxPython/wxPythonHelloWorld.py", line 71, in __init__
    entryPg = EntryScrollPanel(self.nb, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, style=wx.VSCROLL)
  File "C:\Users\JLP_COM1\PycharmProjects\wxPython\EntryScrollPanel.py", line 17, in __init__
    self.entTitle = titlePanel(self, -1)
  File "C:\Users\JLP_COM1\PycharmProjects\wxPython\titlePanel.py", line 5, in __init__
    wx.Panel.__init__(self, parent, ID)
  File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_windows.py", line 68, in __init__
    _windows_.Panel_swiginit(self,_windows_.new_Panel(*args, **kwargs))
TypeError: in method 'new_Panel', expected argument 1 of type 'wxWindow *'

How do I add self.entTitle to EntryScrollPanel?

Thank you for any and all help!


Solution

  • A Panel is not an wxWindow so that:

    self.entTitle = titlePanel(wx.Panel, -1)
    

    doesn't work and produces the traceback.
    The above code is instantiating an wx.Panel subclass (titlePanel) and sending an wx.Panel as the parent for its constructor in:

    class titlePanel(wx.Panel):
        def __init__(self, parent, ID):
            wx.Panel.__init__(self, parent, ID)
    

    but a Panel needs a window (p.e. a Frame) as parent.

    Thus you must do instead:

    self.entTitle = titlePanel(self, -1)
    

    as self, here, the parent of TitlePanel, is a wx.ScrolledWindow

    Also note you did not initialized your ScrolledWindow:

    class EntryScrollPanel(wx.ScrolledWindow):
        def __init__(self, parent, ID):
            wx.ScrolledWindow.__init__(self, parent, ID)    <--- addd this