Steven Sproat wrote:
TreeCtrl items can have a TreeDataItem associated with them, which is a
wrapper around an object, basically.
I also store a list which stores the references to each node in the tree
i.e, _id is an integer:
data = wx.TreeItemData(_id)
t = self.tree.AppendItem(self.root, "Tab " + str(_id + 1),
data=data)
self.tabs[_id] = t
self.tabs keeps a list of TreeItemIDs
then, to receive the data back:
def on_click(self, event):
"""
Changes to the selected tab is a tab node is double clicked upon,
otherwise pops up the
"""
item = self.tree.GetPyData(event.GetItem())
if not item:
return # click on the root node
if isinstance(item, int):
self.gui.tabs.SetSelection(item)
Hopefully that should give you an idea. I store relatively objects that
I have defined myself in my data tree, that just contain simply Python
data structures, so it's a matter of saving that list of objects and
then loading them back into the tree by re-creating its TreeDataItem (I
don't persist that)
Thanks Steven, I appreciate your time, however, to my shame, I must admit
that I am unable to follow your example particularly well, or rather, I
should say, I am unable to translate it to fit into my own code.
For example:
import wx
class clsMainFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.panel_OutMain = wx.Panel(self, -1,
style=wx.SUNKEN_BORDER|wx.TAB_TRAVERSAL)
self.tTREE = wx.TreeCtrl(self.panel_OutMain, -1,
style=wx.TR_HAS_BUTTONS|wx.TR_NO_LINES|wx.TR_DEFAULT_STYLE|wx.SUNKEN_BORDER)
self.__set_properties()
self.__do_layout()
troot = self.tTREE.AddRoot("Main Item")
tchild1 = self.tTREE.AppendItem(troot,"First Child Item")
tchild1a = self.tTREE.AppendItem(tchild1,"Sub item one")
tchild1b = self.tTREE.AppendItem(tchild1,"Sub item two")
tchild1ba = self.tTREE.AppendItem(tchild1b,"Sub Sub item one")
tchild2 = self.tTREE.AppendItem(troot,"Second Child item")
tchild2a = self.tTREE.AppendItem(tchild2,"Sub item one")
···
#
#
def __set_properties(self):
self.SetTitle("MainFrame")
self.SetSize((400, 300))
#
#
def __do_layout(self):
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_2 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(self.panel_OutMain, 1, wx.ALL|wx.EXPAND, 2)
sizer_2.Add(self.tTREE, 1, wx.ALL|wx.EXPAND, 2)
self.panel_OutMain.SetSizer(sizer_2)
self.SetSizer(sizer_1)
self.Layout()
self.Centre()
#
#
if __name__ == "__main__":
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
MainFrame = clsMainFrame(None, -1, "")
app.SetTopWindow(MainFrame)
MainFrame.Show()
app.MainLoop()
This is how my app is laid out.
The dummy tree structure can be saved to a file and loaded back again.
This however, is where the problems start. In the example above I want to be
able to add some dummy text as data for each item. I *think* I managed to
follow your code enough to add an integer ID to the TreeDataItem as I added
them to the tree. I also added this ID to an array, along with the dummy
text that I wanted to use.
However, if I tried to retrieve the data ID from the item that was clicked
(so that I could match the ID in the data list and get the data string), I
could not seem to get to the integer that should have been there as an ID.
Am I even following the right tracks here? I think I am (from looking at
your code), but for the life of me I cant seem to work out how to get the
integer back from the TreeDataItem so that I can match it up with the
correct item in the data list.
Sorry to be so dense, but if I don't ask Ill never know!
regards
MVK
--
View this message in context: http://www.nabble.com/Controlling-TreeCtrl-Structure-with-additional-data-tp22398485p22403888.html
Sent from the wxPython-users mailing list archive at Nabble.com.