Pages : 1
#1 Le 16/07/2006, à 20:15
- Yakulu
Listctrl + images + wxpython
Salut,
je voudrais savoir comment il est possible d'ajouter des images ou icones dans une listCtrl en wxpython. Je
sais comment mettre du texte mais par contre je ne sais pas comment inserer une image. Donc si vous avez des idées merci de m'en faire part
#2 Le 17/07/2006, à 08:03
- gapz
Re : Listctrl + images + wxpython
Pour voir ça tu peux installer les exemples de wxpython via tes dépôts :
sudo apt-get install wx2.6-examples
Et en l'occurence le code qui t'interesse :
import wx
import images
#----------------------------------------------------------------------
class TestVirtualList(wx.ListCtrl):
def __init__(self, parent, log):
wx.ListCtrl.__init__(
self, parent, -1,
style=wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES
)
self.log = log
self.il = wx.ImageList(16, 16)
self.idx1 = self.il.Add(images.getSmilesBitmap())
self.SetImageList(self.il, wx.IMAGE_LIST_SMALL)
self.InsertColumn(0, "First")
self.InsertColumn(1, "Second")
self.InsertColumn(2, "Third")
self.SetColumnWidth(0, 175)
self.SetColumnWidth(1, 175)
self.SetColumnWidth(2, 175)
self.SetItemCount(1000000)
self.attr1 = wx.ListItemAttr()
self.attr1.SetBackgroundColour("yellow")
self.attr2 = wx.ListItemAttr()
self.attr2.SetBackgroundColour("light blue")
self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated)
self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected)
def OnItemSelected(self, event):
self.currentItem = event.m_itemIndex
self.log.WriteText('OnItemSelected: "%s", "%s", "%s", "%s"\n' %
(self.currentItem,
self.GetItemText(self.currentItem),
self.getColumnText(self.currentItem, 1),
self.getColumnText(self.currentItem, 2)))
def OnItemActivated(self, event):
self.currentItem = event.m_itemIndex
self.log.WriteText("OnItemActivated: %s\nTopItem: %s\n" %
(self.GetItemText(self.currentItem), self.GetTopItem()))
def getColumnText(self, index, col):
item = self.GetItem(index, col)
return item.GetText()
def OnItemDeselected(self, evt):
self.log.WriteText("OnItemDeselected: %s" % evt.m_itemIndex)
#---------------------------------------------------
# These methods are callbacks for implementing the
# "virtualness" of the list... Normally you would
# determine the text, attributes and/or image based
# on values from some external data source, but for
# this demo we'll just calculate them
def OnGetItemText(self, item, col):
return "Item %d, column %d" % (item, col)
def OnGetItemImage(self, item):
if item % 3 == 0:
return self.idx1
else:
return -1
def OnGetItemAttr(self, item):
if item % 3 == 1:
return self.attr1
elif item % 3 == 2:
return self.attr2
else:
return None
#----------------------------------------------------------------------
def runTest(frame, nb, log):
win = TestVirtualList(nb, log)
return win
#----------------------------------------------------------------------
overview = """\
This example demonstrates the ListCtrl's Virtual List features. A Virtual list
can contain any number of cells, but data is not loaded into the control itself.
It is loaded on demand via virtual methods <code>OnGetItemText(), OnGetItemImage()</code>,
and <code>OnGetItemAttr()</code>. This greatly reduces the amount of memory required
without limiting what can be done with the list control itself.
"""
if __name__ == '__main__':
import sys,os
import run
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
Avec image.py qui ressemble à :
import wx
from Main import opj
#----------------------------------------------------------------------
def runTest(frame, nb, log):
bmp = wx.Image(opj('bitmaps/image.bmp'), wx.BITMAP_TYPE_BMP).ConvertToBitmap()
gif = wx.Image(opj('bitmaps/image.gif'), wx.BITMAP_TYPE_GIF).ConvertToBitmap()
png = wx.Image(opj('bitmaps/image.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap()
jpg = wx.Image(opj('bitmaps/image.jpg'), wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
panel = wx.Panel(nb, -1)
pos = 10
wx.StaticBitmap(panel, -1, bmp, (10, pos), (bmp.GetWidth(), bmp.GetHeight()))
pos = pos + bmp.GetHeight() + 10
wx.StaticBitmap(panel, -1, gif, (10, pos), (gif.GetWidth(), gif.GetHeight()))
pos = pos + gif.GetHeight() + 10
wx.StaticBitmap(panel, -1, png, (10, pos), (png.GetWidth(), png.GetHeight()))
pos = pos + png.GetHeight() + 10
wx.StaticBitmap(panel, -1, jpg, (10, pos), (jpg.GetWidth(), jpg.GetHeight()))
return panel
#----------------------------------------------------------------------
overview = """\
<html>
<body>
This class encapsulates a platform-independent image. An image can be created
from data, or using <code>wxBitmap.ConvertToImage</code>. An image can be loaded from
a file in a variety of formats, and is extensible to new formats via image
format handlers. Functions are available to set and get image bits, so it can
be used for basic image manipulation.
<p>The following image handlers are available. wxBMPHandler is always installed
by default. To use other image formats, install the appropriate handler or use
<code>wx.InitAllImageHandlers()</code>.
<p>
<table>
<tr><td width=25%>wxBMPHandler</td> <td>For loading and saving, always installed.</td></tr>
<tr><td>wxPNGHandler</td> <td>For loading and saving.</td> </tr>
<tr><td>wxJPEGHandler</td> <td>For loading and saving.</td> </tr>
<tr><td>wxGIFHandler</td> <td>Only for loading, due to legal issues.</td> </tr>
<tr><td>wxPCXHandler</td> <td>For loading and saving.</td> </tr>
<tr><td>wxPNMHandler</td> <td>For loading and saving.</td> </tr>
<tr><td>wxTIFFHandler</td> <td>For loading and saving.</td> </tr>
<tr><td>wxIFFHandler</td> <td>For loading only.</td> </tr>
<tr><td>wxXPMHandler</td> <td>For loading and saving.</td> </tr>
<tr><td>wxICOHandler</td> <td>For loading and saving.</td> </tr>
<tr><td>wxCURHandler</td> <td>For loading and saving.</td> </tr>
<tr><td>wxANIHandler</td> <td>For loading only.</td> </tr>
</table>
<p>When saving in PCX format, wxPCXHandler will count the number of different
colours in the image; if there are 256 or less colours, it will save as 8 bit,
else it will save as 24 bit.
<p>Loading PNMs only works for ASCII or raw RGB images. When saving in PNM format,
wxPNMHandler will always save as raw RGB.
</body>
</html>"""
if __name__ == '__main__':
import sys,os
import run
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
Real programmers code in binary :: http://gapz.tuxfamily.org
Hors ligne
#3 Le 17/07/2006, à 16:50
- Yakulu
Re : Listctrl + images + wxpython
Merci de ta reponse je vais regarder ca
#4 Le 17/07/2006, à 21:40
- Yakulu
Re : Listctrl + images + wxpython
Bon j'ai essayé de le faire marcher mais j'ai toujours un probleme. En effet, les images ne s'affichent toujours pas pourtant je fais comme c'est montré dans les exemples:
import wx
class Configuration(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent = None, id = -1, title = u"Preference",style=wx.DEFAULT_FRAME_STYLE,size=(400,400))
bmp = wx.Image('bitmaps/image.bmp', wx.BITMAP_TYPE_BMP).ConvertToBitmap()
gif = wx.Image('bitmaps/image.gif', wx.BITMAP_TYPE_GIF).ConvertToBitmap()
png = wx.Image('bitmaps/image.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap()
jpg = wx.Image('bitmaps/image.jpg', wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
self.list = wx.ListCtrl(self, -1, style=wx.LC_REPORT)
self.list.il = wx.ImageList(16, 16)
self.list.idx1 = self.list.il.Add(bmp)
self.list.idx2 = self.list.il.Add(gif)
self.list.idx3 = self.list.il.Add(png)
self.list.idx4 = self.list.il.Add(jpg)
print self.list.SetImageList(self.list.il, wx.IMAGE_LIST_NORMAL)
[i][b]self.list.InsertImageItem(?, ?) // Je pense qu'il faut utiliser cette fonction[/i][/b]
self.list.InsertColumn(0, "First")
self.list.InsertColumn(1, "Second")
self.list.InsertColumn(2, "Third")
self.list.SetColumnWidth(0, 175)
self.list.SetColumnWidth(1, 175)
self.list.SetColumnWidth(2, 175)
self.list.InsertStringItem(0, "ligne0 colonne0")
self.list.SetStringItem(0, 1,"ligne0 colonne1")
self.list.SetStringItem(0, 2,"ligne0 colonne2")
class Fenetre(wx.App):
def OnInit(self):
fen = Configuration()
fen.Show(True)
self.SetTopWindow(fen)
return True
app = Fenetre()
app.MainLoop()
#5 Le 18/07/2006, à 10:08
- bipede
Re : Listctrl + images + wxpython
Je t'ai répondu sur developpez.com
Desktop: MSI - Intel® Core™ i5-3330 CPU @ 3.00GHz × 4 - RAM 8 go- Kubuntu 21.04 - Système sur SSD 64 Go - /home sur HDD 500 Go.
Laptop: DELL Inspiron-15 3567 - Intel® Core™ i5-7200 CPU @ 2.50GHz × 4 - RAM 8 go - HDD 1 To - Ubuntu 20.10 avec /home séparé.
Mon site: Les contributions du bipède
Hors ligne
Pages : 1