wx.Python:在多个面板之间传递控制 [英] wx.Python: Passing control between multiple panels
问题描述
我是 wxPython 的新手,研究过类似的问题,但找不到我的问题的具体答案.我正在用分离器创建两个面板.每个面板都有许多小部件.我想在一个面板中有一个小部件控制另一个面板的一些属性,反之亦然)
I'm a newbie to wxPython, and have researched similar questions, but can't specifically find an answer to my question. I'm creating two panels with a splitter. Each panel has a number of widgets. I'd like to have a widget in one panel control some properties of the other and vice versa)
在示例中,我试图从 LeftPanel
中的按钮更改 RightPanel
的背景.我显然做错了什么,因为我收到错误:
In the example, I'm trying to change the background of RightPanel
from a button in LeftPanel
. I'm obviously doing something wrong as a I get an error:
TypeError: init() 只需要 2 个参数(给定 1 个)
TypeError: init() takes exactly 2 arguments (1 given)
代码:
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()
非常感谢任何帮助.问候
Any help greatly appreciated. Regards
推荐答案
在你的代码中
def on_button_pressed(Panel,event):
RightPanel().SetBackgroundColour("light blue")
在定义中 'Panel' 应该是 'self' 因为 'on_button_pressed' 是一个实例方法
In the definition 'Panel' should be 'self' as 'on_button_pressed' is a instance method
然后您将创建一个新的 RightPanel 而不是访问已创建的实例.
Then you are creating a new RightPanel instead of acces the already created instance.
我将绑定移动到父框架,以便它可以调用另一个子面板上的方法.请参阅下面的修改代码.
I moved the bind to the parent frame so it can call methods on the other child panel. See the modified code below.
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()
这篇关于wx.Python:在多个面板之间传递控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!