[wxPython] wxTreeCtrl.SetItemImage problem

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[wxPython] wxTreeCtrl.SetItemImage problem

Amos Latteier-2
Hi,

I've started hacking again on a basic Zope IDE in wxPython. I want to
display icons next to tree items, but am having trouble.

I've created a wxImageList for my wxTreeCtrl. I assign images to tree items
with wxTreeCtrl.SetItemImage. However my tree control displays blank white
space where my images should be.

I am using wxPython 2.1.11 on win32.

Here are the relevant sections from my program:

...
# Make a tree for navigation
tID = NewId()
self.tree = wxTreeCtrl(splitter, tID)

# Add folder and document icons to tree
images=wxImageList(16, 16)
wxImage_AddHandler(wxPNGHandler())
images.Add(wxImage('folder.png', wxBITMAP_TYPE_PNG).ConvertToBitmap())
images.Add(wxImage('doc.png', wxBITMAP_TYPE_PNG).ConvertToBitmap())
self.tree.SetImageList(images)

...
# Creating some tree items and assigning images
for id in docIds:
    child=self.tree.AppendItem(item, id)
    self.tree.SetItemImage(child, 1)

I believe that my images are OK, since I can draw them on elsewhere on the
screen without problem.

Suggestions?

Thanks!

-Amos



_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] wxTreeCtrl.SetItemImage problem

Robin Dunn
> I've started hacking again on a basic Zope IDE in wxPython. I want to
> display icons next to tree items, but am having trouble.
>
> I've created a wxImageList for my wxTreeCtrl. I assign images to tree items
> with wxTreeCtrl.SetItemImage. However my tree control displays blank white
> space where my images should be.
>

[...]

> images=wxImageList(16, 16)
> wxImage_AddHandler(wxPNGHandler())
> images.Add(wxImage('folder.png', wxBITMAP_TYPE_PNG).ConvertToBitmap())
> images.Add(wxImage('doc.png', wxBITMAP_TYPE_PNG).ConvertToBitmap())
> self.tree.SetImageList(images)
>


wxWindows allows wxImageLists to be reused by multiple controls, so contrary
to most other things in wxWindows, the TreeCtrl won't take ownership or a
copy of the image list.  So to prevent memory leaks I have to destroy the
image list when it goes out of scope, so the images you are trying to use
don't exist any more!

You can fix this by just adding:

    self.images = images

to save a reference to the image list until the window goes away.

--
Robin Dunn
Software Craftsman
[hidden email]
http://AllDunn.com/robin/
http://AllDunn.com/wxPython/  Check it out!




_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

RE: [wxPython] wxTreeCtrl.SetItemImage problem

Cristian Echeverria
In reply to this post by Amos Latteier-2
The default behavior of wxImageList is to set a trasparency masc
for the bitmaps, but this dosnĀ“t work on wxWindows at this moment.
The next must solve your ploblem:

images = wxImageList(16,16,FALSE)

on the other hand, wxImageList.Add return the id of your
image, and this is wath you need to put in

self.tree.SetItemImage(child, id)

or you can put this id when you crete the item

self.tree.AppendItem(idp,text_label, id1,id2)

idp is the id of the node where your item will be appened,
id1 is the image id for the normal state and id2 is the image id
for the selected state

SetItemImage change the image for the normal state and
SetItemSelectedImage change the image for the selected state.

I hope this can help you

Cristian Echeverria

----------------------------

----- Original Message -----
From: Amos Latteier <[hidden email]>
To: <[hidden email]>
Sent: Thursday, December 23, 1999 5:24 PM
Subject: [wxPython] wxTreeCtrl.SetItemImage problem


> Hi,
>
> I've started hacking again on a basic Zope IDE in wxPython. I want to
> display icons next to tree items, but am having trouble.
>
> I've created a wxImageList for my wxTreeCtrl. I assign images to tree
items

> with wxTreeCtrl.SetItemImage. However my tree control displays blank white
> space where my images should be.
>
> I am using wxPython 2.1.11 on win32.
>
> Here are the relevant sections from my program:
>
> ...
> # Make a tree for navigation
> tID = NewId()
> self.tree = wxTreeCtrl(splitter, tID)
>
> # Add folder and document icons to tree
> images=wxImageList(16, 16)
> wxImage_AddHandler(wxPNGHandler())
> images.Add(wxImage('folder.png', wxBITMAP_TYPE_PNG).ConvertToBitmap())
> images.Add(wxImage('doc.png', wxBITMAP_TYPE_PNG).ConvertToBitmap())
> self.tree.SetImageList(images)
>
> ...
> # Creating some tree items and assigning images
> for id in docIds:
>     child=self.tree.AppendItem(item, id)
>     self.tree.SetItemImage(child, 1)
>
> I believe that my images are OK, since I can draw them on elsewhere on the
> screen without problem.
>
> Suggestions?
>
> Thanks!
>
> -Amos
>
>
>
> _______________________________________________
> wxPython-users maillist  -  [hidden email]
> http://starship.python.net/mailman/listinfo/wxpython-users
>


_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

[wxPython] Application global variables

Cliff Baeseman
In reply to this post by Robin Dunn
Dumb Newbie question

   How can I create a global instance variable. I have a application that has
to communicate in many different classes to one object. How can I create a
global variable that contains say a frame instance.

Cliff Baeseman

_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] Application global variables

Bob Kundus
Global variables are a bad idea.  There's pretty much always a better
alternative.  In this case, add an attribute to your application class.
I'm not at home, so I don't have the wxPython/wxWindows docs, but
there is a function to get the one and only wxApp instance.  Something
like this:

  app = wxGetApp() # or something like this
  app.theFrame = myFrame

You can also use module attributes.
 
  import mymodule
    ...
  mymodule.x = 5

I prefer the former approach.

The main reason global variables are bad is that they pollute the global
namespace.
That is, if you import a module that also uses global variables and you use
the
same name, you are in trouble.  The same could apply to application members
to
a lesser degree, so I recommend prefixing such attibutes with an
abbreviation
of the project you are working on, to make the name unique.

----------
> From: Cliff Baeseman <[hidden email]>
> To: [hidden email]
> Subject: [wxPython] Application global variables
> Date: Friday, December 24, 1999 2:24 AM
>
> Dumb Newbie question
>
>    How can I create a global instance variable. I have a application that
has
> to communicate in many different classes to one object. How can I create
a
> global variable that contains say a frame instance.
>
> Cliff Baeseman
>
> _______________________________________________
> wxPython-users maillist  -  [hidden email]
> http://starship.python.net/mailman/listinfo/wxpython-users


_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users