wx.Python:在多个面板之间传递控件

时间:2013-05-12 07:08:22

标签: wxpython

我是wxPython的新手,并研究过类似的问题,但无法专门找到我的问题的答案。我用分离器创建了两个面板。每个面板都有许多小部件。我想在一个面板中控制一个小部件控制另一个面板的某些属性,反之亦然)

在示例中,我尝试从RightPanel中的按钮更改LeftPanel的背景。因为我收到错误,我显然做错了什么:

  

TypeError: init ()只需要2个参数(给定1个)

代码:

import wx
import wx.grid as gridlib
import  pyodbc

class RightPanel(wx.Panel):
""""""

def __init__(self, parent):
    """Constructor"""
    wx.Panel.__init__(self, parent=parent)        

    grid = gridlib.Grid(self)
    grid.CreateGrid(5,5)

    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(grid, 0, wx.EXPAND)
    self.SetSizer(sizer)

class LeftPanel(wx.Panel):
""""""

def __init__(self, parent):
    """Constructor"""
    wx.Panel.__init__(self, parent=parent)  

    self.create_controls()
    self.SetBackgroundColour("light green")

def create_controls(self):

    self.h_sizer = wx.BoxSizer(wx.HORIZONTAL)
    self.v_sizer = wx.BoxSizer(wx.VERTICAL)

    self.button = wx.Button(self, label="Press me!")
    self.button.Bind(wx.EVT_BUTTON, self.on_button_pressed)     

    self.v_sizer.Add(self.button, 0)

    self.v_sizer.Add(self.h_sizer, 0, wx.EXPAND)
    self.SetSizer(self.v_sizer)

def on_button_pressed(Panel,event):

        RightPanel().SetBackgroundColour("light blue")

class MyForm(wx.Frame):

def __init__(self):
    wx.Frame.__init__(self, None, wx.ID_ANY, "DB Viewer",size=(350, 250))

    splitter = wx.SplitterWindow(self)
    leftP = LeftPanel(splitter)
    rightP = RightPanel(splitter)

    splitter.SplitVertically(leftP, rightP)
    splitter.SetMinimumPaneSize(20)

    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(splitter, 1, wx.EXPAND)
    self.SetSizer(sizer)

if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()

任何帮助非常感谢。此致

2 个答案:

答案 0 :(得分:2)

使用pubsub可以实现干净的设计:

import wx
import wx.grid as gridlib
from wx.lib.pubsub import pub

#import pyodbc

class RightPanel(wx.Panel):
    """"""

    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)        

        grid = gridlib.Grid(self)
        grid.CreateGrid(5,5)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(grid, 0, wx.EXPAND)
        self.SetSizer(sizer)

        pub.subscribe(self.changeColourEvent, "MOOD_CHANGE")


    def changeColourEvent(self, value):
        self.SetBackgroundColour(value)
        self.Refresh()


class LeftPanel(wx.Panel):
    """"""

    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)  

        self.create_controls()
        self.SetBackgroundColour("grey")


    def create_controls(self):

        self.h_sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.v_sizer = wx.BoxSizer(wx.VERTICAL)

        self.bbutton = wx.Button(self, label="Got dem blues?!")
        self.bbutton.Bind(wx.EVT_BUTTON, self.blues_button_pressed)     

        self.hbutton = wx.Button(self, label="Happy happy!")
        self.hbutton.Bind(wx.EVT_BUTTON, self.happy_button_pressed)     

        self.v_sizer.Add(self.bbutton, 0)
        self.v_sizer.Add(self.hbutton, 0)

        self.v_sizer.Add(self.h_sizer, 0, wx.EXPAND)
        self.SetSizer(self.v_sizer)


    def blues_button_pressed(self,event):
        pub.sendMessage("MOOD_CHANGE", value = "blue")        


    def happy_button_pressed(self,event):
        pub.sendMessage("MOOD_CHANGE", value = "yellow")        


class MyForm(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "DB Viewer",size=(350, 250))

        splitter = wx.SplitterWindow(self)
        leftP = LeftPanel(splitter)
        rightP = RightPanel(splitter)

        splitter.SplitVertically(leftP, rightP)
        splitter.SetMinimumPaneSize(20)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(splitter, 1, wx.EXPAND)
        self.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

这种方法带来的好处是,它意味着没有窗格依赖于任何其他窗格的设计。您可以看到MyForm和RightPanel都不需要知道LeftPanel是否决定是时候根据按钮或复选框或任何其他机制更改颜色。在此代码中,MyForm仅关注实例化两个窗格。它不会纠结于它们之间的逻辑。

在对象(在本例中为窗格)可以互相传递的信息类型中,它也很容易扩展。

它还允许将其他元素添加到设计中,关注相同类型的事物(在我的示例中,情绪变化),而不会影响除自身以外的任何代码。

答案 1 :(得分:0)

在您的代码中

def on_button_pressed(Panel,event):

    RightPanel().SetBackgroundColour("light blue")

在定义中,'Panel'应为'self',因为'on_button_pressed'是一个实例方法

然后,您将创建一个新的RightPanel,而不是访问已创建的实例。

我将绑定移动到父框架,以便它可以调用另一个子面板上的方法。 请参阅下面的修改后的代码。

import wx
import wx.grid as gridlib
# import  pyodbc


class RightPanel(wx.Panel):
    """"""

    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)

        grid = gridlib.Grid(self)
        grid.CreateGrid(5, 5)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(grid, 0, wx.EXPAND)
        self.SetSizer(sizer)


class LeftPanel(wx.Panel):
    """"""

    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)

        self.create_controls()
        self.SetBackgroundColour("light green")

    def create_controls(self):

        self.h_sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.v_sizer = wx.BoxSizer(wx.VERTICAL)

        self.button = wx.Button(self, label="Press me!")

        self.v_sizer.Add(self.button, 0)

        self.v_sizer.Add(self.h_sizer, 0, wx.EXPAND)
        self.SetSizer(self.v_sizer)


class MyForm(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "DB Viewer", size=(350, 250))

        splitter = wx.SplitterWindow(self)
        leftP = LeftPanel(splitter)
        self.rightP = RightPanel(splitter)

        splitter.SplitVertically(leftP, self.rightP)
        splitter.SetMinimumPaneSize(20)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(splitter, 1, wx.EXPAND)
        self.SetSizer(sizer)

        leftP.button.Bind(wx.EVT_BUTTON, self.on_button_pressed)

        self.Layout()

    def on_button_pressed(self, event):
            self.rightP.SetBackgroundColour("light blue")
            self.Refresh()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()