在wxPython中传递两个面板之间的元组

时间:2013-06-17 01:52:39

标签: python wxpython tuples panels

我试图将一个面板中已检查字符串的元组传递给另一个面板中的textCtrl小部件。我认为,如果我轮询包含复选框的checklistbox小部件的面板类,并将其设置为等于包含TextCtrl小部件的面板类的属性,我可以根据检查的框在TextCtrl小部件中执行操作

我也尝试过使用pubsub,但我遇到了困难,并且不确定是否会使用它。

作为免责声明,我是面向对象编程和python的新手。我在学习面向对象编程方面取得了进展,但仍然有些事情对我来说有点模糊。

这是我的代码:

#! /usr/bin/python

# Trying to do things in a separate panel based on
# events that happen in another panel

import wx

# ----- Functions




# ----- Classes
class chkbxPanel(wx.Panel):
    # This class defines the panel that will
    # contain the checkbox
    def __init__(self, parent):
        wx.Panel.__init__(self,parent=parent, id=-1)
        # Widgets
        List = ['one','two','three']
        Prompt = 'Please Make a Choice'
        self.ChkBx = wx.CheckListBox(self,id=-1,choices=List,size=(-1,200))
        self.PromptChkBx = wx.StaticText(self,id=-1,label=Prompt)

        # Page Sizers
        self.panel_vertSizer=wx.BoxSizer(wx.VERTICAL)
        self.panel_vertSizer.Add(self.PromptChkBx,proportion=0,flag=wx.ALIGN_LEFT)
        self.panel_vertSizer.Add(self.ChkBx,proportion=0,flag=wx.ALIGN_LEFT)

        # Invoke Sizer
        self.SetSizer(self.panel_vertSizer)

        # Make 'self' (the panel) shrink to the minimum size 
        # required by the controls
        self.Fit()
        # end __init__
    # ----- Functions

class resultsPanel(wx.Panel):
    # This class define the panel that will 
    # contain the textctrl 
    def __init__ (self,parent):
        wx.Panel.__init__(self,parent=parent,id=-1)
        # Widgets
        ResultsPanelTitle = 'Results:'
        self.ResultsTitle = wx.StaticText(self, id=-1, label= ResultsPanelTitle)
        self.Results = wx.TextCtrl(self, id=-1,size=(300,500),style=(wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_DONTWRAP))
        self.CheckedBxs = ()

        lenofcb = len(self.CheckedBxs)
        typeofcb = type(self.CheckedBxs)
        self.Results.AppendText('How Many Boxes are Checkd:\n')
        self.Results.AppendText(str(lenofcb)+'\n')
        self.Results.AppendText(str(typeofcb)+'\n')
        # Page Sizer
        self.panel_vertSizer = wx.BoxSizer(wx.VERTICAL)
        self.panel_vertSizer.Add(self.ResultsTitle,proportion=0, flag=wx.ALIGN_LEFT)
        self.panel_vertSizer.Add(self.Results, proportion=0, flag=wx.ALIGN_LEFT)

        # Invoke Sizer
        self.SetSizer(self.panel_vertSizer)

        # Make 'self' (the panel) shrink to the minimum size
        # required by the controls 
        self.Fit()

        # end __init__

    # ----- Functions


# ----- Main Frame
class MainFrame(wx.Frame):
    # A 2-Control Class With BoxSizers
    def __init__(self):
        # Configure the Figure
        titleText = 'Wx Question'
        wx.Frame.__init__(  self, None, title=titleText
                        ,size=(600,300), style=wx.DEFAULT_FRAME_STYLE)

        # First Frame Control automatically expands to the 
        # Frame's client size
        frame_panel = wx.Panel(self)

        # Create the Controls
        LeftPanel = chkbxPanel(frame_panel)
        RightPanel = resultsPanel(frame_panel)
        RightPanel.CheckedBxs = LeftPanel.ChkBx.GetCheckedStrings()
        RightPanel.CheckedBxs = ('one','two')

        # Create Sizers and add the controls
        panelCtrls_horzSizer = wx.BoxSizer(wx.HORIZONTAL)
        panelCtrls_horzSizer.Add(LeftPanel)
        panelCtrls_horzSizer.Add(RightPanel)

        framePanel_vertSizer = wx.BoxSizer(wx.VERTICAL)
        framePanel_vertSizer.Add(panelCtrls_horzSizer)

        frame_panel.SetSizer(framePanel_vertSizer)
        frame_panel.Fit()
        self.SetSize((600,600))






# Main if statement
if __name__ == '__main__':
    app = wx.PySimpleApp(redirect=False)
    appFrame = MainFrame().Show()
    app.MainLoop()

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

您可以在父类中进行事件绑定,因为它可以访问两个面板

#! /usr/bin/python

# Trying to do things in a separate panel based on
# events that happen in another panel

import wx

# ----- Functions




# ----- Classes
class ChkbxPanel(wx.Panel):
    # This class defines the panel that will
    # contain the checkbox
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent, id=-1)
        # Widgets
        choices = ['one', 'two', 'three']
        prompt = 'Please Make a Choice'
        self.chkBx = wx.CheckListBox(self, choices=choices, size=(-1, 200))
        self.PromptChkBx = wx.StaticText(self, label=prompt)

        # Page Sizers
        self.panel_vertSizer = wx.BoxSizer(wx.VERTICAL)
        self.panel_vertSizer.Add(self.PromptChkBx, proportion=0,
                                 flag=wx.ALIGN_LEFT)
        self.panel_vertSizer.Add(self.chkBx, proportion=0, flag=wx.ALIGN_LEFT)

        # Invoke Sizer
        self.SetSizer(self.panel_vertSizer)

        # Make 'self' (the panel) shrink to the minimum size
        # required by the controls
        self.Fit()
        # end __init__
    # ----- Functions


class ResultsPanel(wx.Panel):
    # This class define the panel that will
    # contain the textctrl
    def __init__ (self, parent):
        wx.Panel.__init__(self, parent=parent, id=-1)
        # Widgets
        resultsPanelTitle = 'Results:'
        self.resultsTitle = wx.StaticText(self, label=resultsPanelTitle)
        self.results = wx.TextCtrl(self, size=(300, 500),
                    style=(wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_DONTWRAP))
        self.CheckedBxs = ()

        lenofcb = len(self.CheckedBxs)
        typeofcb = type(self.CheckedBxs)
        self.results.AppendText('How Many Boxes are Checkd:\n')
        self.results.AppendText(str(lenofcb) + '\n')
        self.results.AppendText(str(typeofcb) + '\n')
        # Page Sizer
        self.panel_vertSizer = wx.BoxSizer(wx.VERTICAL)
        self.panel_vertSizer.Add(self.resultsTitle, proportion=0, flag=wx.ALIGN_LEFT)
        self.panel_vertSizer.Add(self.results, proportion=0, flag=wx.ALIGN_LEFT)

        # Invoke Sizer
        self.SetSizer(self.panel_vertSizer)

        # Make 'self' (the panel) shrink to the minimum size
        # required by the controls
        self.Fit()

        # end __init__

    # ----- Functions


# ----- Main Frame
class MainFrame(wx.Frame):
    # A 2-Control Class With BoxSizers
    def __init__(self):
        # Configure the Figure
        titleText = 'Wx Question'
        wx.Frame.__init__(self, None, title=titleText
                        , size=(600, 300), style=wx.DEFAULT_FRAME_STYLE)

        # First Frame Control automatically expands to the
        # Frame's client size
        frame_panel = wx.Panel(self)

        # Create the Controls
        leftPanel = ChkbxPanel(frame_panel)
        self.rightPanel = ResultsPanel(frame_panel)

        # Create Sizers and add the controls
        panelCtrls_horzSizer = wx.BoxSizer(wx.HORIZONTAL)
        panelCtrls_horzSizer.Add(leftPanel)
        panelCtrls_horzSizer.Add(self.rightPanel)

        framePanel_vertSizer = wx.BoxSizer(wx.VERTICAL)
        framePanel_vertSizer.Add(panelCtrls_horzSizer)

        frame_panel.SetSizer(framePanel_vertSizer)
        frame_panel.Fit()
        self.SetSize((600, 600))

        leftPanel.chkBx.Bind(wx.EVT_CHECKLISTBOX, self.onCheckBox)

    def onCheckBox(self, event):
        checked = event.EventObject.CheckedStrings
        self.rightPanel.results.AppendText(
            'Checked: {0}, Qty checked: {1}\n'.format(checked, len(checked)))



# Main if statement
if __name__ == '__main__':
    app = wx.App(redirect=False)
    appFrame = MainFrame().Show()
    app.MainLoop()

答案 1 :(得分:0)

我删除了所有注释,空格以减少行数。并在我更改/添加代码的地方添加了评论。 (1),(2),(3)

import wx

class chkbxPanel(wx.Panel):
    def __init__(self, parent, resultsPanel):
        wx.Panel.__init__(self,parent=parent, id=-1)
        List = ['one','two','three']
        Prompt = 'Please Make a Choice'
        self.ChkBx = wx.CheckListBox(self,id=-1,choices=List,size=(-1,200))
        ### (1) Bind CHECKLISTBOX event (which is triggered when checkbox is toggled: checked/unchecked)
        self.ChkBx.Bind(wx.EVT_CHECKLISTBOX, lambda e: resultsPanel.changed(self.ChkBx.GetCheckedStrings()))
        ###
        self.PromptChkBx = wx.StaticText(self,id=-1,label=Prompt)
        self.panel_vertSizer=wx.BoxSizer(wx.VERTICAL)
        self.panel_vertSizer.Add(self.PromptChkBx,proportion=0,flag=wx.ALIGN_LEFT)
        self.panel_vertSizer.Add(self.ChkBx,proportion=0,flag=wx.ALIGN_LEFT)
        self.SetSizer(self.panel_vertSizer)
        self.Fit()

class resultsPanel(wx.Panel):
    def __init__ (self,parent):
        wx.Panel.__init__(self,parent=parent,id=-1)
        ResultsPanelTitle = 'Results:'
        self.ResultsTitle = wx.StaticText(self, id=-1, label= ResultsPanelTitle)
        self.Results = wx.TextCtrl(self, id=-1,size=(300,500),style=(wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_DONTWRAP))
        self.panel_vertSizer = wx.BoxSizer(wx.VERTICAL)
        self.panel_vertSizer.Add(self.ResultsTitle,proportion=0, flag=wx.ALIGN_LEFT)
        self.panel_vertSizer.Add(self.Results, proportion=0, flag=wx.ALIGN_LEFT)
        self.SetSizer(self.panel_vertSizer)
        self.Fit()

    ### (2): Called when checkbox is toggled with checkbox strings as parameter. See (1)
    def changed(self, checked_strings):
        self.Results.AppendText('How Many Boxes are Checkd:\n')
        self.Results.AppendText(str(len(checked_strings))+'\n')
        self.Results.AppendText(str(checked_strings)+'\n')
    ###

class MainFrame(wx.Frame):
    def __init__(self):
        titleText = 'Wx Question'
        wx.Frame.__init__(self, None, title=titleText ,size=(600,300), style=wx.DEFAULT_FRAME_STYLE)
        frame_panel = wx.Panel(self)
        ### (3): Changed creation order. Passed resultsPanel instance to chkbxPanel creator
        RightPanel = resultsPanel(frame_panel)
        LeftPanel = chkbxPanel(frame_panel, RightPanel)
        ###
        RightPanel.CheckedBxs = LeftPanel.ChkBx.GetCheckedStrings()
        RightPanel.CheckedBxs = ('one','two')
        panelCtrls_horzSizer = wx.BoxSizer(wx.HORIZONTAL)
        panelCtrls_horzSizer.Add(LeftPanel)
        panelCtrls_horzSizer.Add(RightPanel)
        framePanel_vertSizer = wx.BoxSizer(wx.VERTICAL)
        framePanel_vertSizer.Add(panelCtrls_horzSizer)
        frame_panel.SetSizer(framePanel_vertSizer)
        frame_panel.Fit()
        self.SetSize((600,600))

if __name__ == '__main__':
    app = wx.PySimpleApp(redirect=False)
    appFrame = MainFrame().Show()
    app.MainLoop()