wxPython: Disable a Panel from a different panel
I have multiple panels in my app, that reside in a wx.Notebook
. For sake of illustration, let's assume I have a panel called LaunchPanel
in a file called launchTab
, and a panel called ScanPanel
in a file called scanTab
.
I have a button on ScanPanel
that can lockup the GUI for a while, so I am currently disabling every widget on the panel itself when the long-running-task kicks off. This all works fine. But I want to disable other things in the other panels now that may conflict if the user goes trigger happy with the left mouse button. I found that you can disable a panel with panel.Disable()
, but I don't know how to, for instance, call Disable
for the panel in LaunchPanel from within ScanPanel
.
I've tried import launchTab
from within launchTab
to get access to ScanPanel
:
import launchTab
...
launchTab.LaunchPanel.Disable()
but get this error:
TypeError: unbound method Disable() must be called with LaunchPanel instance as first argument (got nothing instead)
I think the answer is a pubsub, but I don't know how to set one up to muck with the panel, I've only ever used them to update a widget...? There's an immense amount of sourcecode at the moment, so I don't want to paste all of it, but I can provide more clarification if it is needed.
Help? Thoughts?
EDIT PER BELOW ANSWER:
So -- I'm not quite sure I understand... there's the following components to the app. myAppGUI.py:
class myNotebook(wx.Notebook):
"""
The core layout for the app -- notebook pages are slotted here
"""
#----------------------------------------------------------------------
def __init__(self, parent):
wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=wx.BK_DEFAULT)
self.AddPage(launchTab.LaunchPanel(self), "Launch")
self.AddPage(scanTab.ScanPanel(self), "Scan")
self.AddPage(extractTab.ExtractPanel(self), "Extract")
self.AddPage(virtualsTab.VirtualsPanel(self), "Virtuals")
This is the main file that launches all my other notebook tabs. Then, I have, launchTab:
class LaunchPanel(wx.Panel):
"""
Launch Tab for finding and launching databases
"""
#----------------------------------------------------------------------
def __init__(self, parent):
""""""
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
super(LaunchPanel, self)
self.initialize()
def initialize(self):
global sizer
panel = self
sizer = wx.GridBagSizer(11, 3)
<snip>
And then I have, scanTab:
class ScanPanel(wx.Panel):
"""
Scan Tab for running Sonospy Database Scans, Updates and Repairs
"""
#----------------------------------------------------------------------
def __init__(self, parent):
""""""
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
panel = self
sizer开发者_JS百科 = wx.GridBagSizer(6, 5)
self.launchPanelRef = None
I've tried the answer below, but I think since my init is using parent=parent (which I took off sample code somewhere else to get it working originally), I get the following error:
File "gui/scanTab.py", line 223, in __init__
launchPanel = launchTab.LaunchPanel()
TypeError: __init__() takes exactly 2 arguments (1 given)
When you say to put:
def main():
scanPanel = ScanPanel()
launchPanel = LaunchPanel()
scanPanel.setInstanceLaunchPanel(launchPanel)
Does that go within scanTab? And is launchPanelRef
the name of the panel I want to control in this case?
Sorry -- I'm easily confused. :)
-Chow
The error you are getting is because you are calling a method of a class without an instance of an object of that class.
You need to pass the instance of your LaunchPanel class (your LaunchPanel object), into your ScanPanel class.
class ScanPanel:
def __init__:
self.launchPanelRef = None
<snip>
def setInstanceLaunchPanel(launchPanelRef):
self.launchPanelRef = launchPanelRef
def main():
scanPanel = ScanPanel()
launchPanel = LaunchPanel()
scanPanel.setInstanceLaunchPanel(launchPanel)
Now within ScanPanel you have a reference to your launchPanel object that you can call disable on.
Does that make any sense?
EDITS
I am guessing you want to be able to disable the "launchPanel" from the "scanPanel", right? You need to add the setInstanceLaunchPanel to the scan panel. All it does it allow you to store a reference to the launch panel. This means that within the scan panel you'll be able to control the launch panel instance.
class myNotebook(wx.Notebook):
"""
The core layout for the app -- notebook pages are slotted here
"""
#----------------------------------------------------------------------
def __init__(self, parent):
wx.Notebook.__init__(self, parent, id=wx.ID_ANY, style=wx.BK_DEFAULT)
launchPanel = launchTab.LauchPanel(self) #init launchPanel
scanPanel = scanTab.ScanPanel(self) #init scanPanel
scanPanel.setInstanceLaunchPanel(launchPanel) #store reference to launchPanel in scanPanel
self.AddPage(launchPanel, "Launch") #add launchPanel to notebook
self.AddPage(scanPanel, "Scan") #add scanPanel to notebook
self.AddPage(extractTab.ExtractPanel(self), "Extract") #init extractPanel and add to notebook
self.AddPage(virtualsTab.VirtualsPanel(self), "Virtuals")
精彩评论