The pitfall:
Each item needs to have data attached in order to be a sortable tree in
the end.
(This was what II was struggling w/)
The data is of type wx.TreeItemData
So all together now:
SetItemPyData(item, obj) # item is wx.TreeItemId and obj some bogus
Python object
So, in order to get the associated data and therefore make your tree
sortable
1: your tree needs to be s subclass of wx.TreeCtrl
2: that overrides OnCompareItems(1,2), where 1 and 2 are wx.TreeItemId
instances
Code (I have no clue where I originally got it from):
def OnCompareItems(self, item1, item2):
data1 = self.GetItemPyData(item1)
data2 = self.GetItemPyData(item2)
return cmp(data1, data2)
Cheers
--
--------------------------------------------------
Tobias Weber
CEO
The ROG Corporation GmbH
Donaustaufer Str. 200
93059 Regensburg
Tel: +49 941 4610 57 55
Fax: +49 941 4610 57 56
Gesch�ftsf�hrer: Tobias Weber
Registergericht: Amtsgericht Regensburg - HRB 8954
UStID DE225905250 - Steuer-Nr.184/59359
--------------------------------------------------
Geschäftsführer: Tobias Weber
Registergericht: Amtsgericht Regensburg - HRB 8954
UStID DE225905250 - Steuer-Nr.184/59359
--------------------------------------------------
--
To unsubscribe, send email to wxPython-user...@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en
def CreateProductsHyperTreeCtrl(self):
self.tree = HTL.HyperTreeList(self, -1, wx.Point(0, 0), wx.Size(500, 250),
wx.TR_DEFAULT_STYLE | wx.NO_BORDER,
wx.TR_HAS_BUTTONS | wx.TR_HAS_VARIABLE_ROW_HEIGHT)
imglist = wx.ImageList(16, 16, True, 2)
imglist.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER, wx.ART_OTHER,
wx.Size(16, 16)))
imglist.Add(wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER,
wx.Size(16, 16)))
self.tree.AssignImageList(imglist)
# add columns
self.tree.AddColumn("Model")
self.tree.SetColumnWidth(0,300)
# Version 2 w/ input field in exra column
self.tree.AddColumn("Amount")
self.tree.SetColumnWidth(1,50)
self.tree.AddColumn("Purchase Price")
self.tree.AddColumn("Yield)")
# the root entry of our tree ctrl
root = self.tree.AddRoot("ROOT
#Read in the database and create alist of models
# ...... dabase stuff
# run through the entire list again and create the tree ctrl structure
for model in model_list:
# add a folder for each model
items.append(self.tree.AppendItem(root, model, 0))
# query all items that match that product in order to populate each folder
print "Calling select"
dictionary2 = z.select_order('*','products','model',model,'model')
print "Found "
print len(dictionary2)
print " items that match model " + model
count = 1
# HERE I BEGIN POPULATING THE TREE
for item in dictionary2:
print "adding description of"
print item['id']
amount = wx.TextCtrl(self.tree.GetMainWindow(), -1, "", size=(20,20))
child = self.tree.AppendItem(items[(len(items)-1)],item['description'])
# as ITEM
#THE HEADACHE PART: I WANT TO BIND A RIGHT MOUSECLICK TO AN EVENT
#THE FOLLOWING LINE DOES NOTHING
self.Bind(wx.EVT_RIGHT_DCLICK, self.OnAbout, self.tree)
self.tree.SetItemWindow(child,amount,1)
self.tree.SetItemText(child, u'� ' + str('%10.2f' %
(item['purchaseprice'])),2)
self.tree.SetItemText(child, str(item['yield_5']),3)
self.tree.Expand(root)
return self.tree
I used the demo and some fresh simple panel etc .... I MUST be missing
something here ....
Anybody wanna jumpstart my brains ?
ThanX.
Cheers,
S.
Sorry for trying it again- I haven't received any reply on my yesterday's post si once more I am giving it a shot
Original post following ...
Also, since mouse events are not command events they do not propagate up
the containment hierarchy. This means that self will not ever see the
mouse events from self.tree (unless the tree rebroadcasts them) and so
doing self.Bind will not allow you to see those events. You'll have
better luck with self.tree.Bind(wx.EVT_whatever, self.OnAbout) for the
mouse events.
See http://wiki.wxpython.org/self.Bind_vs._self.button.Bind
--
Robin Dunn
Software Craftsman
http://wxPython.org
--
On Wed, Aug 24, 2011 at 4:39 PM, 최원준 <wonjun...@gmail.com> wrote:
> I runed the cody's code on my PC http://ompldr.org/vYTFobg
> I am using linux.
>
1) What code? From the wxPython Cookbook?
Assuming that its the CustomTreeCtrl recipe you will see that it
doesn't do any sorting. Blindly copying code is not a good way to
understand how it works, so you need to sit down and look at the code
to see where it's getting the list of files to display so you can add
the sorting. As another approach you can also look at the TreeCtrl
documentation and see how to use its internal sorting
(http://docs.wxwidgets.org/2.8/wx_wxtreectrl.html#wxtreectrlsortchildren).
2) What are you trying to do and why do you have all of those modules
from Editra in there? If your trying to learn how to program or create
a simple editor for a school assignment or something you will learn
allot more by doing something that is more simple so you can see how
everything works together.
Cody
2011/8/24 Wonjun, Choi <wonjun...@gmail.com>:
>> Assuming that its the CustomTreeCtrl recipe you will see that it
>> doesn't do any sorting. Blindly copying code is not a good way to
>> understand how it works, so you need to sit down and look at the code
>> to see where it's getting the list of files to display so you can add
>> the sorting.
> =====> I tested many all day.. I am tired to say this.
What have you tested? What problems did you have? Please make a small
runnable sample that reproduces the issue your having
(http://wiki.wxpython.org/MakingSampleApps).
>
> As another approach you can also look at the TreeCtrl
>> documentation and see how to use its internal sorting
>> (http://docs.wxwidgets.org/2.8/wx_wxtreectrl.html#wxtreectrlsortchildren).
>>
> ====> I tested on window. because someone tested it successfully on
> window
> it looked better than linux.
??
Again show the code you tried to and explain what didn't work. That
way someone can help you see what you are doing wrong.
>>
> ====> I bought your book and wxPython in Action. and I want to develop
> simple editor but it might be a little complicated.
> ---------------------------------------------------------------
> | File Menu
> |
> ---------------------------------------------------------------
> | Button Menu |
> ---------------------------------------------------------------
> | Solution Explorer | Editor |
> |
> | |
> |
> | |
> ---------------------------------------------------------------
> I want to make like this and add multi-lingual lexer and build button.
> and I want to know when can I meet you in wxpython irc channel.
>
I am more than happy to help if you have a question (on this list) and
you can show that you have put some effort into figuring it out on
your own first, but I do not have the time to give private
consultations.
Cody
On Wed, Aug 24, 2011 at 10:55 PM, 최원준 <wonjun...@gmail.com> wrote:
> def OnExpanding(self, event):
> item = event.GetItem()
> path = self.GetPyData(item)
> files = self._GetFiles(path)
>
> if files is None:
> self.SetItemImage(item, FileBrowser.ERROR)
> self.SetItemHasChildren(item, False)
> return
>
> for fname in files:
> fullpath = os.path.join(path, fname)
> if os.path.isdir(fullpath):
> self.AppendDir(item, fullpath)
> else:
> self.AppendFile(item, fullpath)
>
> pt = self.GetPosition();
> item, flags = self.HitTest(pt)
> if item:
> self.log.WriteText("OnLeftDClick: %s\n" %
> self.GetItemText(item))
> parent = self.GetItemParent(item)
> if parent.IsOk():
> self.SortChildren(parent)
>
Not sure what you want since all you have done is copy and paste some
code here and without mentioning anything about the problem your
having with it, so a little hard to give you any good advice.
I will take some guesses based on the code above differs from the
original cookbook recipe.
1) The code you added is mostly unnecessary.
* pt = self.GetPosition() # will return the position of the tree control
* item, flags = self.HitTest(pt) # The behavior on this will not
be predictable since you are passing screen coords to a method that
expects client coords from within the treectrl it self. Would expect
that most times it returns nothing or the wrong node.
* parent = self.GetItemParent(item) # Assuming item was the right
node this would return the parent of the node that was expanded. You
don't want that, you want the currently expanding node to sort its
newly added children. This node is already gotten at the top of the
method and was used a few lines above to add the children to.
2) If you look at the documentation for SortChildren
(http://docs.wxwidgets.org/2.8/wx_wxtreectrl.html#wxtreectrlsortchildren)
you will see that you must provide your own overridden implementation
of OnCompareItems. Did you override OnCompareItems to do the sorting?
3) Forgetting all of the above, Python list objects also have a sort()
method (or alternatively the sorted(iterable) method). That you could
use to sort the list of files before they are added to the tree to get
the same affect.
Cody
You seem to be wanting the sort to put all folders first and then files,
is that correct? If so then the problem is your comparison functions
used for the sorting are totally ignoring the fact that some items are
folders and some are files, they are only comparing the names. Since
Python can easily compare tuples, you can create a composite value that
contains a flag indicating whether an item is a dir or a file, plus the
name. So you can do something like this pseudo code in the comparison
function:
path1 = fullPathName of treeitem1
path2 = fullPathName of treeitem2
item1 = (os.path.isdir(path1), path1)
item2 = (os.path.isdir(path2), path2)
return cmp(item1, item2)
Also, you were calling SortChildren *before* the child nodes were added
to the tree item, so there was nothing in the tree for it to sort at
that moment in time.
Given some tree item id, (the root, or some other item you've added) you
can do:
tree.Expand(item)
or
tree.ExpandAllChildren(item)
or
tree.ExpandAll()
depending on what kind of expanding you want to do.