0

我是 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()

非常感谢任何帮助。问候

4

2 回答 2

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 只关心实例化两个窗格。它不会纠缠于它们之间的逻辑。

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

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

于 2014-02-22T06:34:37.723 回答
0

在您的代码中,您有

def on_button_pressed(Panel,event):

    RightPanel().SetBackgroundColour("light blue")

在定义中,“面板”应该是“自我”,因为“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()
于 2013-05-12T21:09:07.320 回答