How can I sort the item in wxTreeCtrl?
+--root
+-folder-A
+-folder-B
+-folder-C
+-file-1
+-file-2
+-file-3
sorting by alphabatically.
Wonjun, Choi
How can I sort the item in wxTreeCtrl?
+--root
+-folder-A
+-folder-B
+-folder-C
+-file-1
+-file-2
+-file-3
sorting by alphabatically.
Wonjun, Choi
Since I ave been keeping myself busy w/ trees lately:
(code parts untested)
Sorting elements in a tree is done through
Sort- Children(item)
where item is an instance of wx.TreeItemId
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
Am 11.08.11 09:04, schrieb Wonjun, Choi:
How can I sort the item in wxTreeCtrl?
+--root
+-folder-A
+-folder-B
+-folder-C
+-file-1
+-file-2
+-file-3sorting by alphabatically.
Wonjun, Choi
--
--------------------------------------------------
Tobias Weber
CEO
The ROG Corporation GmbH
Donaustaufer Str. 200
93059 Regensburg
Tel: +49 941 4610 57 55
Fax: +49 941 4610 57 56
www.roglink.com
Gesch�ftsf�hrer: Tobias Weber
Registergericht: Amtsgericht Regensburg - HRB 8954
UStID DE225905250 - Steuer-Nr.184/59359
--------------------------------------------------
ok i understand. thank you for your comment!
Wonjun, Choi
2011/8/11 Tobias Weber tobias.weber@roglink.net
Am 11.08.11 09:04, schrieb Wonjun, Choi:
How can I sort the item in wxTreeCtrl?
±-root
±folder-A
±folder-B
±folder-C
+-file-1
+-file-2
+-file-3
sorting by alphabatically.
Wonjun, Choi
Since I ave been keeping myself busy w/ trees lately:
(code parts untested)
Sorting elements in a tree is done through
Sort- Children(item)
where item is an instance of wx.TreeItemId
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
–
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en
I know that it is some basic understanding/ thinking prob in my head:
I am playing around w/ an Application design concept. I came across the
AGW Aui Docking library demo in wxPython Demo-> AUI
So far I have exchanged the tree pane of the left frame of the demo
(Tree Pane) w/ a HyperTreeCtrl
I am building and populationg my tree now like this: (problem part is
the self.Bind part in the lower section)
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 ...
I know that it is some basic understanding/ thinking prob in my head:
I am playing around w/ an Application design concept. I came across the
AGW Aui Docking library demo in wxPython Demo-> AUI
So far I have exchanged the tree pane of the left frame of the demo
(Tree Pane) w/ a HyperTreeCtrl
I am building and populationg my tree now like this: (problem part is
the self.Bind part in the lower section)
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.
Hi,
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 …
You really should post a working sample demonstrating the problem. However:
#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)
If you want a right mouse click, you should bind to wx.EVT_RIGHT_UP. wx.EVT_RIGHT_DCLICK is for right double-click events, as far as I remember (I never used that event myself).
Secondly, you need to distinguish if you want the “OnAbout” method to trigger if you right click anywhere in the tree or just when the right-click happens on tree items. If it’s the latter, you really should be using the wx.EVT_TREE_ITEM_RIGHT_CLICK event.
Andrea.
“Imagination Is The Only Weapon In The War Against Reality.”
http://xoomer.alice.it/infinity77/
On 12 August 2011 09:44, Tobias Weber wrote:
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 self.Bind vs. self.button.Bind - wxPyWiki
On 8/12/11 1:09 AM, Andrea Gavana wrote:
Hi,
On 12 August 2011 09:44, Tobias Weber wrote:
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 ...<snip>
You really should post a working sample demonstrating the problem. However:
> #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)If you want a right mouse click, you should bind to wx.EVT_RIGHT_UP.
wx.EVT_RIGHT_DCLICK is for right double-click events, as far as I
remember (I never used that event myself).
--
Robin Dunn
Software Craftsman
I have tested but I couln’t. so please help me!
DDR: I want to ask you something related wxpython
I’m sorry I didn’t come come home until one in the morning.
Oh, about wxPython. Sure.
I want to sort the directory
I am using wxTreectrl
Right…
http://ompldr.org/vYTEwcg this is the folder what I want to sort.
I’ve never used wxTreectrl before, so don’t hold your breath.
Right…?
ok
what is mean that holding one’s breath
It means, don’t wait on it.
wait on what/>
That means, ‘I probably can’t help, but I will try to help.’
I see. may I continue what I am talking to you?
Of course.
Continue, please.
I want to do this http://www.manaware.net/wxpython/sorting-elements-of-a-tree-control.html
but I cannot understand exactly.
It’s a bit dense.
one moment…
ok
So, do you have the tree control actually working at the moment?
yes
And it’s not sorted right?
yes
the control displays like this http://ompldr.org/vYTEwcg
OK, so it’s sorted alphabetically. How do you want it sorted?
±-root
±folder-A
±folder-B
±folder-C
±file-1
±file-2
±file-3
Ah, right.
Hm, how to get the folders to the top…
I get files list from files = self._GetFiles(path)
So, the way I’m reading the manaware entry is that you need to make your own sorting method to do that.
ok
right
Basically, you want to follow the ‘ADVANCED’ section.
In the manaware entry.
yes maybe.
http://groups.google.com/group/wxpython-users/browse_thread/thread/dfb676ecc78051aa/caf032453c64dbdb?lnk=gst&q=Sorting+wxTreeCtrl+
I’ve never done anything like that, however, so I’m not really sure how to do it.
ok
So, in one line, subclass, override sorting method to rank folders first, otherwise sort alphabetically.
I cannot understand that part
Which part is that?
The subclassing part? The overriding part?
making subclass and override sorting method
Ah, when you subclass, you are making a class which contains a class.
So, if I have class A with the method foo(1)…
and I have class B as the subclass of class A…
I made class inherited wx.TreeCtrl and I made def OnCompareItems(self, item1, item2) function
Calling B.foo will have the same effect as calling A.foo.
maum: That sounds like it should work.
I don’t know how to use the OnCompareItems method
from files = self._GetFiles(path) code.
Return -1 if the first item should go below the second, or 1 if it should go above, or 0 if they are equal.
item1, item2 must be index of item?
I think they are the items.
But I’m not sure. Make the function print some debug output, I guess.
the web mentioned wx.TreeItemId
Yeah…
What do you get when you print(item1)?
I have no idea what should I pass to item1
I think it’s called automatically.
When you make the list, it just… happens.
you mean that I just have to call self.SortChildren function?
I don’t think you have to do even that.
But, keep in mind, I have all of five minutes of experience on the topic.
item = event.GetItem(); self.SortChildren(item) when I wrote like this. the code was not work
ok
but any error didn’t occur
What do you get from the print(item1)?
I just declared the function in the inherited subclass, it have not been called.
because I had no idea how to call its function
oookay, I guess you do have to call it manually.
I get the file and folder list in files list like this : files = self._GetFiles(path)
Right. So, you set them to the tree and they are just unordered?
Or are they set via strict alphabetical order?
it is unordered but files.sort(cmp=None, key=None, reverse=False) code can sort them(folder, file) but I want to seperate folder and file
the code can sort them alphabetically
but I want to sort like above.
Hm. I see the problem, but not the solution. It’s impossible to separate folders from files, right?
I think so so I tested some code like this == for file in files: if os.path.isdir(file) == True: files.sort(cmp=None, key=None, reverse=False)
but it doesn’t work
there was no error
maum: try having cmp set to your sort function.
Ihttp://docs.python.org/library/stdtypes.html#mutable-sequence-types
er, http://docs.python.org/library/stdtypes.html#mutable-sequence-types
2011/8/11 최원준 wonjunchoi001@gmail.com
ok i understand. thank you for your comment!
Wonjun, Choi
2011/8/11 Tobias Weber tobias.weber@roglink.net
Am 11.08.11 09:04, schrieb Wonjun, Choi:
How can I sort the item in wxTreeCtrl?
±-root
±folder-A
±folder-B
±folder-C
+-file-1
+-file-2
+-file-3
sorting by alphabatically.
Wonjun, Choi
Since I ave been keeping myself busy w/ trees lately:
(code parts untested)
Sorting elements in a tree is done through
Sort- Children(item)
where item is an instance of wx.TreeItemId
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
–
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en
Hi,
Why not just download the full code from the Packt Publishing website for the wxPython cookbook? Then you can compare what’s different between yours and Cody’s.
I runed the cody’s code on my PC http://ompldr.org/vYTFobg
I am using linux.
2011/8/24 Mike Driscoll kyosohma@gmail.com
Hi,
Why not just download the full code from the Packt Publishing website for the wxPython cookbook? Then you can compare what’s different between yours and Cody’s.
Mike Driscoll
–
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en
Hi,
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
(wxTreeCtrl).
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
On Wed, Aug 24, 2011 at 4:39 PM, 최원준 <wonjunchoi001@gmail.com> wrote:
Hi,
> I runed the cody's code on my PChttp://ompldr.org/vYTFobg
> I am using linux.1) What code? From the wxPython Cookbook?
=====> /Downloads/wxPython_Cookbook_example/1780_04_Code/02/
customtree.py
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.
As another approach you can also look at the TreeCtrl
documentation and see how to use its internal sorting
(wxTreeCtrl).
====> I tested on window. because someone tested it successfully on
window
it looked better than linux.
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.
====> I bought your book and wxPython in Action. and I want to develop
simple editor but it might be a little complicated.
On 8월25일, 오전7시44분, Cody Precord <codyprec...@gmail.com> wrote:
On Wed, Aug 24, 2011 at 4:39 PM, 최원준 <wonjunchoi...@gmail.com> wrote:
---------------------------------------------------------------
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.
Wonjun, Choi
Hi,
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
(MakingSampleApps - wxPyWiki).
As another approach you can also look at the TreeCtrl
documentation and see how to use its internal sorting
(wxTreeCtrl).====> 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
2011/8/24 Wonjun, Choi <wonjunchoi001@gmail.com>:
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)
2011/8/25 Cody Precord codyprecord@gmail.com
Hi,
2011/8/24 Wonjun, Choi wonjunchoi001@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
–
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en
Hi,
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
(wxTreeCtrl)
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
On Wed, Aug 24, 2011 at 10:55 PM, 최원준 <wonjunchoi001@gmail.com> wrote:
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)
above code is from wx example and it worked well. it was in Click event.
but I don’t want it because it sorted not properly.
±-working directory
±-aub(folder)
±-bkwf.c(file)
±-goo.so(file)
±-tke(folder)
it sorted like this.
but I want this.
±-working directory
±-aub(folder)
±-tke(folder)
±-bkwf.c(file)
±-goo.so(file)
or
±-working directory
±folder-A
±folder-B
±folder-C
±file-1
±file-2
±file-3
also when a file is added in the folder, the control automatically refresh the folder.
and I want to do this when I load the IDE form. so I thought this method should be in maybe constructor(init)
you will see that you must provide your own overridden implementation
of OnCompareItems. Did you override OnCompareItems to do the sorting?
==> answer :
def OnCompareItems(self, item1, item2):
data1 = self.GetItemPyData(item1)
data2 = self.GetItemPyData(item2)
return cmp(data1, data2)
I did it. and inherited the wxTreeCtrl.
but it didn’t work.
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.
==> answer :
I tried it already. like this => files.sort(cmp=None, key=None, reverse=False)
but it sorts not what I want.
also I tried files.sort(cmp=OnCompareItems, key=None, reverse=False) and etc…
but it didn’t work.http://ompldr.org/vYTEwcg
the code I have tried is here : http://pastebin.com/gzdQYAgV
I know it is from wx example but I want to sort the items in here.
Wonjun, Choi
I found some example:
and I want to get the list of working directory and display on wxTreeCtrl
def CreateTreeCtrl(self):
tree = wx.TreeCtrl(self, -1, wx.Point(0, 0), wx.Size(160, 250),
wx.TR_DEFAULT_STYLE | wx.NO_BORDER)
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)))
tree.AssignImageList(imglist)
root = tree.AddRoot("AUI Project", 0)
items = []
items.append(tree.AppendItem(root, "Item 1", 0))
items.append(tree.AppendItem(root, "Item 2", 0))
items.append(tree.AppendItem(root, "Item 3", 0))
items.append(tree.AppendItem(root, "Item 4", 0))
items.append(tree.AppendItem(root, "Item 5", 0))
for item in items:
tree.AppendItem(item, "Subitem 1", 1)
tree.AppendItem(item, "Subitem 2", 1)
tree.AppendItem(item, "Subitem 3", 1)
tree.AppendItem(item, "Subitem 4", 1)
tree.AppendItem(item, "Subitem 5", 1)
tree.Expand(root)
return tree
I want to use this source!
2011/8/26 최원준 wonjunchoi001@gmail.com
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.
==> answer :
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)
above code is from wx example and it worked well. it was in Click event.
but I don’t want it because it sorted not properly.±-working directory
±-aub(folder)
±-bkwf.c(file)
±-goo.so(file)
±-tke(folder)
it sorted like this.but I want this.
±-working directory±-aub(folder)
±-tke(folder)±-bkwf.c(file)
±-goo.so(file)
or
±-working directory±folder-A
±folder-B
±folder-C
±file-1
±file-2
±file-3
also when a file is added in the folder, the control automatically refresh the folder.
and I want to do this when I load the IDE form. so I thought this method should be in maybe constructor(init)
- 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?
==> answer :
def OnCompareItems(self, item1, item2): data1 = self.GetItemPyData(item1) data2 = self.GetItemPyData(item2) return cmp(data1, data2)
I did it. and inherited the wxTreeCtrl.
but it didn’t work.
- 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.
==> answer :
I tried it already. like this => files.sort(cmp=None, key=None, reverse=False)
but it sorts not what I want.
also I tried files.sort(cmp=OnCompareItems, key=None, reverse=False) and etc…but it didn’t work.http://ompldr.org/vYTEwcg
the code I have tried is here : http://pastebin.com/gzdQYAgV
I know it is from wx example but I want to sort the items in here.
Wonjun, Choi
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.
On 8/25/11 5:12 PM, 최원준 wrote:
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.
==> answer :
I tried it already. like this => files.sort(cmp=None, key=None,
reverse=False)
but it sorts not what I want.
also I tried files.sort(cmp=OnCompareItems, key=None, reverse=False) and
etc.....
but it didn't work.http://ompldr.org/vYTEwcgthe code I have tried is here : # Chapter 4: An Applications Building Blocks, Advanced Controls# Recipe 2: Cus - Pastebin.com
I know it is from wx example but I want to sort the items in here.
--
Robin Dunn
Software Craftsman
thank you for your reply.
Wonjun, Choi
On 8월30일, 오전3시47분, Robin Dunn <ro...@alldunn.com> wrote:
On 8/25/11 5:12 PM, 최원준 wrote:
> 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.
> ==> answer :
> I tried it already. like this => files.sort(cmp=None, key=None,
> reverse=False)
> but it sorts not what I want.
> also I tried files.sort(cmp=OnCompareItems, key=None, reverse=False) and
> etc.....
> but it didn't work.http://ompldr.org/vYTEwcg> the code I have tried is here :# Chapter 4: An Applications Building Blocks, Advanced Controls# Recipe 2: Cus - Pastebin.com
> I know it is from wx example but I want to sort the items in here.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.--
Robin Dunn
Software Craftsmanhttp://wxPython.org
I could expand wx.treectrl by this
self.Bind(wx.EVT_TREE_ITEM_EXPANDING, self.OnExpanding)
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)
self.SortChildren(item)
but how can I expand the wx.treectrl when I load the form?
Wonjun, Choi
2011년 8월 30일 오전 7:08, Wonjun, Choi wonjunchoi001@gmail.com님의 말:
thank you for your reply.
Wonjun, Choi
On 8월30일, 오전3시47분, Robin Dunn ro...@alldunn.com wrote:
On 8/25/11 5:12 PM, 최원준 wrote:
- 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.
==> answer :
I tried it already. like this => files.sort(cmp=None, key=None,
reverse=False)
but it sorts not what I want.
also I tried files.sort(cmp=OnCompareItems, key=None, reverse=False) and
etc…
but it didn’t work.http://ompldr.org/vYTEwcg
the code I have tried is here :http://pastebin.com/gzdQYAgV
I know it is from wx example but I want to sort the items in here.
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.
–
Robin Dunn
Software Craftsmanhttp://wxPython.org
–
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en