繁体   English   中英

wxPython wx.ScrolledWindow插入wx.Panel

[英]wxPython wx.ScrolledWindow insert wx.Panel

我正在尝试将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?

感谢您提供的所有帮助!

面板不是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)

但是面板需要一个窗口(pe框架)作为父窗口。

因此,您必须改为:

self.entTitle = titlePanel(self, -1)

作为自身,这里是TitlePanel的父级,是wx.ScrolledWindow

还要注意,您没有初始化ScrolledWindow:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM