wxPython wx.ScrolledWindow插入wx.Panel

时间:2014-09-21 05:06:41

标签: python wxpython wxwidgets

我正在尝试将wx.Panel插入到wx.ScrolledWindow中。我有一个名为self.entTitle的wx.Panel对象,它有两个Title和Date输入字段。我还想在滚动窗口中添加一些其他对象,但我希望在继续使用其他对象之前先使用它。这是我的代码:

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)

这是我得到的错误:

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 *'

如何将self.entTitle添加到EntryScrollPanel?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

Panel不是wxWindow,因此:

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

不起作用并产生追溯 上面的代码实例化一个wx.Panel子类(titlePanel)并发送一个wx.Panel作为其构造函数的父类:

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

但Panel需要一个窗口(例如一个框架)作为父窗口。

因此你必须改为:

self.entTitle = titlePanel(self, -1)

作为self,这里是TitlePanel的父亲,是一个wx.ScrolledWindow

另请注意,您没有初始化ScrolledWindow:

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