Вопрос

I'm new to Python and I want to display string with specific format in wx.ListBox.

For example: with proper alignment, but It display as below.

enter image description here

CODE:

fileName = fileName + str(" " * (38 - len(fileName)))
self.displayList.append( "   %02d             %s  %0.2fKB" % (index+1, fileName, fileSize ))

When I Print self.displayList, It display with proper alignment.

enter image description here

Let me know where I am wrong, or what should be other way to display string with proper alignment.

EDIT

First thanks for the reply.

By applying your(Sundar Nataraj) solution, Output is same as you mention but its on Console, While in wx.ListBox, again not in proper formatting.

See Below image.

enter image description here

I just pass displayList as argument.

self.autoNcListbox = wx.ListBox(self.autoProgListPanel, wx.ID_ANY, wx.Point( 0, 25), wx.Size( 512, 510 ), self.displayList, wx.RAISED_BORDER)
Это было полезно?

Решение

try this way,

index=[1,2,3,4]
text=['animal','gv','asdfasdfasd','adfasdfasdf']
size=['1kb','2kb','3kb','4kb']
displaylist=[]

for i in zip(index,text,size):
    s=""
    for j in range(0,3):
        s +='{0:{fill}{align}16}'.format(i[j], fill=' ', align='<')

    print s
    displaylist.append(s)

for i in displaylist:
    print i

output

1                animal           1kb             
2                gv               2kb             
3                asdfasdfasd      3kb             
4                adfasdfasdf      4kb  

Edit:

import wx
import sys
from wx.lib.mixins.listctrl import ListCtrlAutoWidthMixin


index=[1,2,3,4]
text=['animal','gv','asdfasdfasd','adfasdfasdf']
size=['1kb','2kb','3kb','4kb']
displaylist=[]
for i,j,l in zip(index,text,size):
    displaylist.append((i,j,l))




class AutoWidthListCtrl(wx.ListCtrl, ListCtrlAutoWidthMixin):
    def __init__(self, parent):
        wx.ListCtrl.__init__(self, parent, -1, style=wx.LC_REPORT)
        ListCtrlAutoWidthMixin.__init__(self)


class Actresses(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(380, 230))

        hbox = wx.BoxSizer(wx.HORIZONTAL)

        panel = wx.Panel(self, -1)

        self.list = AutoWidthListCtrl(panel)
        self.list.InsertColumn(0, 'index', width=140)
        self.list.InsertColumn(1, 'text', width=130)
        self.list.InsertColumn(2, 'filesize', wx.LIST_FORMAT_RIGHT, 90)

        for i in displaylist:
            index = self.list.InsertStringItem(sys.maxint, str(i[0]))
            self.list.SetStringItem(index, 1, i[1])
            self.list.SetStringItem(index, 2, i[2])

        hbox.Add(self.list, 1, wx.EXPAND)
        panel.SetSizer(hbox)

        self.Centre()
        self.Show(True)

app = wx.App()
Actresses(None, -1, 'file')
app.MainLoop()
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top