在Phoenix之前,一个可编辑的listctrl用于通过event.Label
中的事件传回已编辑的文本。
使用wxpython 4.0.0b2(Linux)不再是这种情况
查看代码,文本应该在event.Item.Text
中传回,但它始终是空白的。
我发现绕过这个的唯一方法就是编辑“listctrl.py”mixin本身,在CloseEditor
函数中添加一行额外的代码,我设置event.String
返回已更改的文本,然后在触发wx.EVT_LIST_END_LABEL_EDIT
绑定时访问该文本
设置event.Label
本来会更好,但SetLabel
函数似乎一直在丢失。
我确信我肯定错过了某个地方的东西
简而言之,有没有人知道如何访问wxpython Phoenix中TextEditMixin中已更改的标签?
我正在努力的事情似乎是EVT_LIST_END_LABEL_EDIT
事件,无法访问标签本身
示例代码:
import wx
import wx.lib.mixins.listctrl as listmix
class EditableListCtrl(wx.ListCtrl, listmix.TextEditMixin):
''' TextEditMixin allows any column to be edited. '''
def __init__(self, parent, ID=wx.ID_ANY, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=0):
"""Constructor"""
wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
listmix.TextEditMixin.__init__(self)
class MyPanel(wx.Panel):
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent)
rows = [("Ford", "Taurus", "1996", "Blue"),
("Nissan", "370Z", "2010", "Green"),
("Porche", "911", "2009", "Red")
]
self.list_ctrl = EditableListCtrl(self, style=wx.LC_REPORT)
self.list_ctrl.InsertColumn(0, "Make")
self.list_ctrl.InsertColumn(1, "Model")
self.list_ctrl.InsertColumn(2, "Year")
self.list_ctrl.InsertColumn(3, "Color")
index = 0
for row in rows:
self.list_ctrl.InsertItem(index, row[0])
self.list_ctrl.SetItem(index, 1, row[1])
self.list_ctrl.SetItem(index, 2, row[2])
self.list_ctrl.SetItem(index, 3, row[3])
index += 1
self.text0 = wx.TextCtrl(self, -1, "Ford", size=(50,30))
self.text1 = wx.TextCtrl(self, -1, "Taurus", size=(50,30))
self.text2 = wx.TextCtrl(self, -1, "1996", size=(50,30))
self.text3 = wx.TextCtrl(self, -1, "Blue", size=(50,30))
self.list_ctrl.Select(0)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer2 = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
sizer2.Add(self.text0, 0, wx.ALL|wx.EXPAND, 5)
sizer2.Add(self.text1, 0, wx.ALL|wx.EXPAND, 5)
sizer2.Add(self.text2, 0, wx.ALL|wx.EXPAND, 5)
sizer2.Add(self.text3, 0, wx.ALL|wx.EXPAND, 5)
sizer.Add(sizer2,0,wx.ALL|wx.EXPAND,5)
self.SetSizer(sizer)
self.list_ctrl.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
self.list_ctrl.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnMixUpdate)
def OnItemSelected(self, event):
self.currentItem = event.GetIndex()
rowid = self.list_ctrl.GetItem(self.currentItem,0)
self.SetData()
event.Skip()
def SetData(self):
rowid = self.list_ctrl.GetItem(self.currentItem,0)
self.text0.SetValue(rowid.GetText())
rowid = self.list_ctrl.GetItem(self.currentItem,1)
self.text1.SetValue(rowid.GetText())
rowid = self.list_ctrl.GetItem(self.currentItem,2)
self.text2.SetValue(rowid.GetText())
rowid = self.list_ctrl.GetItem(self.currentItem,3)
self.text3.SetValue(rowid.GetText())
def OnMixUpdate(self, event):
self.currentItem = event.GetIndex()
rowid = event.GetIndex()
new_data = event.GetLabel()
new_data2 = event.Item.GetText()
colid = event.GetColumn ()
print ("row,col,new label:",rowid,colid,new_data)
print ("2nd attempt:",new_data2)
self.list_ctrl.SetItem(rowid,colid,new_data,)
#Update the textctrl on screen
self.SetData()
#Update database
#self.OnUpdate(None)
event.Skip()
class MyFrame(wx.Frame):
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, wx.ID_ANY, "Editable List Control")
panel = MyPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
答案 0 :(得分:0)
这结果是一个错误,并在wx.python 4.0.0
中得到修复