我已尝试使用wxpython进行应用程序。我需要为我的GUI(facebook加载)制作listview。所以我选择了ObjectListview。不幸的是,对于初学者来说这是非常艰苦的工作和缺乏信息。我测试了' SimpleExamples'。它很棒。但我需要改变图像。尽管我努力弄清楚自己,但我无法做到。从一些努力,我发现图像文件为python代码。但我不知道如何将图像转换为python代码
你帮我吗?你的答案可能是我最好的答案。 来自https://github.com/adrianer/ObjectListView 的源代码
这是ObjectListview
中的SimpleExample2.py# -*- coding: utf-8 -*-
#!/usr/bin/env python
import datetime
import wx
# Where can we find the ObjectListView module?
import sys
sys.path.append("..")
from ObjectListView import ObjectListView, ColumnDefn
import ExampleModel
import ExampleImages ******# We store our images as python code**Look at here please~****
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.Init()
def Init(self):
self.InitModel()
self.InitWidgets()
self.InitObjectListView()
def InitModel(self):
self.songs = ExampleModel.GetTracks()
def InitWidgets(self):
panel = wx.Panel(self, -1)
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(panel, 1, wx.ALL|wx.EXPAND)
self.SetSizer(sizer_1)
self.myOlv = ObjectListView(panel, -1, style=wx.LC_REPORT|wx.SUNKEN_BORDER)
sizer_2 = wx.BoxSizer(wx.VERTICAL)
sizer_2.Add(self.myOlv, 1, wx.ALL|wx.EXPAND, 4)
panel.SetSizer(sizer_2)
self.Layout()
def InitObjectListView(self):
groupImage = self.myOlv.AddImages(ExampleImages.getGroup16Bitmap(), ExampleImages.getGroup32Bitmap())
userImage = self.myOlv.AddImages(ExampleImages.getUser16Bitmap(), ExampleImages.getUser32Bitmap())
musicImage = self.myOlv.AddImages(ExampleImages.getMusic16Bitmap(), ExampleImages.getMusic32Bitmap())
soloArtists = ["Nelly Furtado", "Missy Higgins", "Moby", "Natalie Imbruglia",
"Dido", "Paul Simon", "Bruce Cockburn"]
def artistImageGetter(track):
if track.artist in soloArtists:
return userImage
else:
return groupImage
def sizeToNiceString(byteCount):
"""
Convert the given byteCount into a string like: 9.9bytes/KB/MB/GB
"""
for (cutoff, label) in [(1024*1024*1024, "GB"), (1024*1024, "MB"), (1024, "KB")]:
if byteCount >= cutoff:
return "%.1f %s" % (byteCount * 1.0 / cutoff, label)
if byteCount == 1:
return "1 byte"
else:
return "%d bytes" % byteCount
self.myOlv.SetColumns([
ColumnDefn("Title", "left", 120, "title", imageGetter=musicImage),
ColumnDefn("Artist", "left", 120, "artist", imageGetter=artistImageGetter),
ColumnDefn("Size", "center", 100, "sizeInBytes", stringConverter=sizeToNiceString),
ColumnDefn("Last Played", "left", 100, "lastPlayed", stringConverter="%d-%m-%Y"),
ColumnDefn("Rating", "center", 100, "rating")
])
self.myOlv.SetObjects(self.songs)
if __name__ == '__main__':
app = wx.PySimpleApp(1)
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, "ObjectListView Simple Example 2")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()
还有ExampleImages.py代码 我从未见过那些代码(x00 \ x10 \ x08)。我想如果我想在Objectlistview中使用图像,我需要更改像这样的代码(x00 \ x10 \ x08)。
from wx import ImageFromStream, BitmapFromImage, EmptyIcon
import cStringIO, zlib
def getGroup16Data():
return zlib.decompress(
'x\xda\x017\x01\xc8\xfe\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\
\x00\x00\x00\x10\x08\x02\x00\x00\x00\x90\x91h6\x00\x00\x00\x03sBIT\x08\x08\
\x08\xdb\xe1O\xe0\x00\x00\x00\xefIDAT(\x91\x85\x92\xc1u\xc30\x0cC\x117Sh\rt\
\r\xadA\xceD\xcc\x9150G\xc7P\x0f\xb2]5\xcf\xcf\xc1I\x96\xf8\x01\x89\xe6c\x8c\
\x01@\x92m\x00U\x85{\x8d1\xaaj\x8c1\xc6\x88\x88\x88\x18\x87\xaaj\xfd\x9c\xda\
&v&\x9c\x92\x14\x11$3s\xdd\xdfl\x03\x94\x9c\x99+2y\x92oF_\x00z\x8f\xde{kA\
\xf6\xd6\xba\x94\xad5+\x7f\xd0^\xafWk\xad\xf7\xfe\x97p\xaeH\x90\x98vRV\x85\
\x95\xb0\xc2\xca\xfc>\xcb\x9ek\xdc\x12n\xd8U\x01\x00\x16\xe4k\xe0]\xd6d\xff=\
\xba\xaa\xa4\xa5\x0fRD\xdc\x98l\x87\xd7\xbc\xbb\xbd\x1b\xf2\x03\xb0c$\x08I+`\
\x03\x8ck`)\xf2^\xea\xd9:K\x9aG\xb7\x8f\x9eW\xf3n\x91\x99$\x8f\xd1\xb0>\x90G\
\xf2\x13\xc7\x84f\xa6$\x92\xf6\xc5D\xecy\xe4c\x8e\xf7\xd49gUe[\xca\xfd_2H\
\xcev\xff\x02\x0b[\xa5cP|\xd5p\x00\x00\x00\x00IEND\xaeB`\x82\xf3\x99\x84Y' )
def getGroup16Bitmap():
return BitmapFromImage(getGroup16Image())
def getGroup16Image():
stream = cStringIO.StringIO(getGroup16Data())
return ImageFromStream(stream)
#----------------------------------------------------------------------
我错了理解??? 有没有其他方法来改变图像。 (我需要改变文字的图像大小,位置,字体和大小......但首先我想改变图像)
答案 0 :(得分:0)
答案 1 :(得分:0)
以下是解决方案:
from wx import BitmapFromImage, ImageFromStream
from cStringIO import StringIO
def getGroup16Data():
return \
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\
\x00\x00\x00\x10\x08\x02\x00\x00\x00\x90\x91h6\x00\x00\x00\x03sBIT\x08\x08\
\x08\xdb\xe1O\xe0\x00\x00\x00\xefIDAT(\x91\x85\x92\xc1u\xc30\x0cC\x117Sh\rt\
\r\xadA\xceD\xcc\x9150G\xc7P\x0f\xb2]5\xcf\xcf\xc1I\x96\xf8\x01\x89\xe6c\x8c\
\x01@\x92m\x00U\x85{\x8d1\xaaj\x8c1\xc6\x88\x88\x88\x18\x87\xaaj\xfd\x9c\xda\
&v&\x9c\x92\x14\x11$3s\xdd\xdfl\x03\x94\x9c\x99+2y\x92oF_\x00z\x8f\xde{kA\
\xf6\xd6\xba\x94\xad5+\x7f\xd0^\xafWk\xad\xf7\xfe\x97p\xaeH\x90\x98vRV\x85\
\x95\xb0\xc2\xca\xfc>\xcb\x9ek\xdc\x12n\xd8U\x01\x00\x16\xe4k\xe0]\xd6d\xff=\
\xba\xaa\xa4\xa5\x0fRD\xdc\x98l\x87\xd7\xbc\xbb\xbd\x1b\xf2\x03\xb0c$\x08I+`\
\x03\x8ck`)\xf2^\xea\xd9:K\x9aG\xb7\x8f\x9eW\xf3n\x91\x99$\x8f\xd1\xb0>\x90G\
\xf2\x13\xc7\x84f\xa6$\x92\xf6\xc5D\xecy\xe4c\x8e\xf7\xd49gUe[\xca\xfd_2H\
\xcev\xff\x02\x0b[\xa5cP|\xd5p\x00\x00\x00\x00IEND\xaeB`\x82\xf3\x99\x84Y'
def getGroup16Bitmap():
return BitmapFromImage(ImageFromStream(StringIO(getGroup16Data())))
要转换图片,请使用以下代码:Create text file of hexadecimal from binary
请检查我是否遗漏了十六进制字符串的起始部分而离开'\x89PNG...'
部分。
答案 2 :(得分:-1)
我终于找到了^^ 有用。但是Objectlistview根本不容易....我需要发现越来越多地改变喜欢和大小。无论如何这里是代码
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import datetime
import wx
import cStringIO
from PIL import Image
#from wx import ImageFromStream, BitmapFromImage, EmptyIcon
# Where can we find the ObjectListView module?
import sys
sys.path.append("..")
from ObjectListView import ObjectListView, ColumnDefn
import ExampleModel
import ExampleImages # We store our images as python code
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.Init()
def Init(self):
self.InitModel()
self.InitWidgets()
self.InitObjectListView()
def InitModel(self):
self.songs = ExampleModel.GetTracks()
def InitWidgets(self):
panel = wx.Panel(self, -1)
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(panel, 1, wx.ALL|wx.EXPAND)
self.SetSizer(sizer_1)
self.myOlv = ObjectListView(panel, -1, style=wx.LC_REPORT|wx.SUNKEN_BORDER)
sizer_2 = wx.BoxSizer(wx.VERTICAL)
sizer_2.Add(self.myOlv, 1, wx.ALL|wx.EXPAND, 4)
panel.SetSizer(sizer_2)
self.Layout()
def InitObjectListView(self):
groupImage = self.myOlv.AddImages(ExampleImages.getGroup16Bitmap(), ExampleImages.getGroup32Bitmap())
userImage = self.myOlv.AddImages(ExampleImages.getUser16Bitmap(), ExampleImages.getUser32Bitmap())
######################################################################3333
#open the image file
imageFile = '111.png'
data = open(imageFile, "rb").read()
# convert to a data stream
stream = cStringIO.StringIO(data)
# convert to a bitmap
bmp = wx.BitmapFromImage( wx.ImageFromStream(stream))
musicImage = self.myOlv.AddImages(bmp)
###################################################################
######### changed code ##########################################
soloArtists = ["Nelly Furtado", "Missy Higgins", "Moby", "Natalie Imbruglia",
"Dido", "Paul Simon", "Bruce Cockburn"]
def artistImageGetter(track):
if track.artist in soloArtists:
return userImage
else:
return groupImage
def sizeToNiceString(byteCount):
"""
Convert the given byteCount into a string like: 9.9bytes/KB/MB/GB
"""
for (cutoff, label) in [(1024*1024*1024, "GB"), (1024*1024, "MB"), (1024, "KB")]:
if byteCount >= cutoff:
return "%.1f %s" % (byteCount * 1.0 / cutoff, label)
if byteCount == 1:
return "1 byte"
else:
return "%d bytes" % byteCount
self.myOlv.SetColumns([
ColumnDefn("Title", "left", 120, "title", imageGetter=musicImage),
ColumnDefn("Artist", "left", 120, "artist", imageGetter=artistImageGetter),
ColumnDefn("Size", "center", 100, "sizeInBytes", stringConverter=sizeToNiceString),
ColumnDefn("Last Played", "left", 100, "lastPlayed", stringConverter="%d-%m-%Y"),
ColumnDefn("Rating", "center", 100, "rating")
])
self.myOlv.SetObjects(self.songs)
if __name__ == '__main__':
app = wx.PySimpleApp(1)
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, "ObjectListView Simple Example 2")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()