簡體   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