我是python的新手并且正在努力学习。我正在创建一个桌面应用程序来记录表单中的信息。我想创建一些我将在不同的框架中重用的面板对象,并且我想从MyFrame类中的面板对象绑定一个事件。我不确定这是否可行。这是我的代码:
main.py:
import wx
from testPanel import testPanel
class MyFrame(wx.Frame):
def OnClick(self,event):
print "Clicked"
def __init__(self, parent, ID, title):
wx.Frame.__init__(self, parent, ID, title=title, size=(500,200))
...
myList = ['Project1', 'Project2', 'Project3', 'Project4']
myPanel = testPanel(self,-1,name="myPanel",lbl="Label: ", List=myList)
self.authorTxt = wx.TextCtrl(self, size=(140,-1))
self.button =wx.Button(self, label="Save")
self.Bind(wx.EVT_BUTTON, self.OnClick, self.button)
...
box = wx.BoxSizer(wx.VERTICAL)
box.Add(myPanel, 1, wx.EXPAND)
box.Add(self.authorTxt, 1, wx.EXPAND)
box.Add(self.button, 1, wx.EXPAND)
...
testPanel.py
import wx
class testPanel(wx.Panel):
def EvtComboBox(self, event):
print"%s was selected" % event.GetString()
def __init__(self, parent, ID, name, lbl, List = []):
wx.Panel.__init__(self, parent, ID)
sizer = wx.BoxSizer(wx.HORIZONTAL)
lbl = wx.StaticText(self, label=lbl, size=(-1,1))
cb = wx.ComboBox(self, size=(-1, 1), choices=List, style=wx.CB_DROPDOWN)
self.Bind(wx.EVT_COMBOBOX, self.EvtComboBox, cb)
...
我正在尝试制作文字字段" authorTxt"可编辑和不可编辑,具体取决于选择的ComboBox项。有没有办法从main.py中的testPanel.py绑定ComboBox事件?
非常感谢任何帮助!
答案 0 :(得分:1)
我认为您正在寻找以下内容:
首先,当您在testPanel中定义cb时,请将其命名为:
self.cb = ...
然后你可以这样做:
self.Bind(wx.EVT_COMBOBOX, self.EvtComboBox, myPanel.cb)
self.EvtComboBox
将成为您需要在MyFrame中定义的新功能
那就是说,我强烈建议你在这种情况下使用PubSub:
http://wiki.wxpython.org/WxLibPubSub
http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern
编辑:cb需要成为testPanel的成员