在python中使用wxwidgets,如何将事件绑定到列表框,以便每次单击新的列表框条目时,文本框中都会显示有关列表框条目的信息?
这是我的代码:
import wx
from ConfigParser import *
class settings(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Frame aka window', size=(500,500))
panel=wx.Panel(self)
wx.StaticText(panel, -1, "Field Type:", pos=(200,20))
wx.TextCtrl(panel,-1,"",pos=(270,20))
for msg_num in self.ACTIVE_MESSAGES:
self.MESSAGE_FIELDS[msg_num] = configuration.get("MESSAGE_FIELDS", msg_num).replace(' ', '').split(',')
self.MESSAGE_FIELD_TYPES[msg_num] = configuration.get("MESSAGE_TYPES", msg_num).replace(' ', '').split(',')
cont=wx.ListBox(panel, -1, (20,20), (150,400), self.MESSAGE_FIELDS['1'], wx.LB_SINGLE)
cont.SetSelection(3)
if __name__=='__main__':
app=wx.PySimpleApp()
frame=settings(parent=None, id=-1)
frame.Show()
app.MainLoop()
答案 0 :(得分:1)
这是我提出的解决方案:
import wx
from ConfigParser import *
class settings(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Frame aka window', size=(500,500))
panel=wx.Panel(self)
configuration = ConfigParser()
configuration.read('SerialReader.conf')
self.ACTIVE_MESSAGES = configuration.get("GENERAL_SETTINGS", "ACTIVE_MESSAGES").split(',')
self.fieldLabel = wx.StaticText(panel, -1, "Field Type:", pos=(200,20))
self.text = wx.TextCtrl(panel,-1,"",pos=(270,20))
self.MESSAGE_FIELDS = {}
self.MESSAGE_FIELD_TYPES = {}
for msg_num in self.ACTIVE_MESSAGES:
self.MESSAGE_FIELDS[msg_num] = configuration.get("MESSAGE_FIELDS", msg_num).replace(' ', '').split(',')
self.MESSAGE_FIELD_TYPES[msg_num] = configuration.get("MESSAGE_TYPES", msg_num).replace(' ', '').split(',')
cont=wx.ListBox(panel, 26, (20,20), (150,400), self.MESSAGE_FIELDS['1'], wx.LB_SINGLE)
cont.SetSelection(3)
self.Bind(wx.EVT_LISTBOX, self.OnSelect, id = 26)
def OnSelect(self, event):
index = event.GetSelection()
self.text.SetValue(self.MESSAGE_FIELD_TYPES['1'][index])
if __name__=='__main__':
app=wx.PySimpleApp()
frame=settings(parent=None, id=-1)
frame.Show()
app.MainLoop()