如何按列wxListCtrl排序

时间:2014-05-10 21:09:01

标签: python sorting wxpython wxwidgets

我按照这些样本[http://www.blog.pythonlibrary.org/2011/01/04/wxpython-wx-listctrl-tips-and-tricks/][1]  通过解决它我设法使我的wxlistctrl可编辑为其中的每个元素,我完成了列的排序跟踪排序后的行。 我的问题是我在Notebook中有两个选项卡,在第一个选项卡中wxlistctrl正在我需要但在第二个选项卡中第二个wxlistctrl我提供了数据,是可编辑的,但当我单击列时排序不起作用

import wx
import datetime,re,os,time,urllib,errno
import sys
import wx.lib.mixins.listctrl  as  listmix


musicdata = {
0 : ("Bad English", "The Price Of Love", "Rock"),
1 : ("DNA featuring Suzanne Vega", "Tom's Diner", "Rock"),
2 : ("George Michael", "Praying For Time", "Rock"),
3 : ("Gloria Estefan", "Here We Are", "Rock"),
4 : ("Linda Ronstadt", "Don't Know Much", "Rock"),
5 : ("Michael Bolton", "How Am I Supposed To Live Without You", "Blues"),
6 : ("Paul Young", "Oh Girl", "Rock"),
}

musicdata2 = {
0 : ("Bad English", "The Price Of Love", "Rock"),
1 : ("DNA featuring Suzanne Vega", "Tom's Diner", "Rock"),
2 : ("George Michael", "Praying For Time", "Rock"),
3 : ("Gloria Estefan", "Here We Are", "Rock"),
4 : ("Linda Ronstadt", "Don't Know Much", "Rock"),
5 : ("Michael Bolton", "How Am I Supposed To Live Without You", "Blues"),
6 : ("Paul Young", "Oh Girl", "Rock"),
}
class Friend_info(object):
    def __init__(self, idd, name, photo):
        # id is used to keep tracking the rows after sorting
        self.id = id(self)
        self.idd = idd
        self.name = name
        self.photo = photo




class Messages(object):
    def __init__(self, fidd, message, time_sent):
        # id is used to keep tracking the rows after sorting
        self.id = id(self)
        self.fidd = fidd
        self.message = message
        self.time_sent = time_sent


class EditableListCtrl(wx.ListCtrl, listmix.TextEditMixin):
    ''' TextEditMixin allows any column to be edited. '''

    #----------------------------------------------------------------------
    def __init__(self, parent, ID=wx.ID_ANY, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=0):
        """Constructor"""
        wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
        listmix.TextEditMixin.__init__(self)


class Example(wx.Frame,listmix.ColumnSorterMixin):


    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs) 

        self.InitUI()

    def InitUI(self):    


        #create the panel to present the  contact data with columns
        bSizer1 = wx.BoxSizer(wx.HORIZONTAL)
        sbSizer1 = wx.StaticBoxSizer(wx.StaticBox(self,wx.ID_ANY,u'User Info'),wx.VERTICAL)
        sbSizer1.SetMinSize(wx.Size(-1,400))

        bSizer3 = wx.BoxSizer(wx.VERTICAL)
        text3 =  wx.StaticText(self, wx.ID_ANY, u'Device User Photo',wx.DefaultPosition, wx.DefaultSize,0)
        text3.Wrap( -1 )
        bSizer3.Add(text3, 0, wx.ALL|wx.ALIGN_CENTER , 5 )


        bSizer4 = wx.BoxSizer(wx.HORIZONTAL)
        #bSizer4.SetMinSize(wx.Size(100,100))
        text4 =  wx.StaticText(self, wx.ID_ANY, u'Name:',wx.DefaultPosition, wx.DefaultSize,0)
        text4.Wrap( -1 )
        bSizer4.Add(text4, 0, wx.ALL , 5 )


        bSizer3.Add(bSizer4, 1, wx.ALL , 5 )

        bSizer5 = wx.BoxSizer(wx.HORIZONTAL)    
        #bSizer5.SetMinSize(wx.Size(100,100))

        text6 =  wx.StaticText(self, wx.ID_ANY, u'ID:',wx.DefaultPosition, wx.DefaultSize,0)
        text6.Wrap( -1 )
        bSizer5.Add(text6, 0, wx.ALL , 5 )


        bSizer3.Add(bSizer5, 1, wx.ALL , 5 )

        bSizer6 = wx.BoxSizer(wx.HORIZONTAL)    

        text8 =  wx.StaticText(self, wx.ID_ANY, u'User Email:',wx.DefaultPosition, wx.DefaultSize,0)
        text8.Wrap( -1 )
        bSizer6.Add(text8, 0, wx.ALL , 5 )


        bSizer3.Add(bSizer6, 1, wx.ALL , 5 )

        text10 =  wx.StaticText(self, wx.ID_ANY, u'Profile Link:',wx.DefaultPosition, wx.DefaultSize,0)
        text10.Wrap( -1 )
        bSizer3.Add(text10, 0, wx.ALL , 5 )


        sbSizer1.Add(bSizer3,1, wx.ALL , 5 )
        bSizer1.Add(sbSizer1,0, wx.ALL , 5 )

        notebook1 = wx.Notebook(self,wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.panel1 = wx.Panel(notebook1,wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )

        bSizer2 = wx.BoxSizer(wx.VERTICAL)    
        self.list_ctrl = EditableListCtrl(self.panel1 ,size=(-1,350), style=wx.LC_REPORT|wx.BORDER_SUNKEN|wx.LC_SORT_ASCENDING)
        self.list_ctrl.InsertColumn(0, 'ID', width=50)
        self.list_ctrl.InsertColumn(1, 'Display Name', width=150)
        self.list_ctrl.InsertColumn(2, 'Photo Profile',width=150)

        #wx.EVT_LIST_ITEM_ACTIVATED ---> with Enter it shows the photos
        self.list_ctrl.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.onItemSelected)
        bSizer2.Add(  self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5 )

        gSizer1 = wx.GridSizer( 0, 4, 0, 0 )

        text1 =  wx.StaticText(self.panel1, -1, 'Photo Profile',wx.Point(50,0),style=wx.ALIGN_CENTRE)
        text1.Wrap( -1 )
        gSizer1.Add( text1, 0, wx.ALL , 5 )

        text2 =  wx.StaticText(self.panel1, -1, 'Cover Photo',  style=wx.ALIGN_CENTRE)
        text2.Wrap( -1 )
        gSizer1.Add( text2, 0, wx.ALL , 5 )

        text11 =  wx.StaticText(self.panel1, -1, '',  style=wx.ALIGN_CENTRE)
        text11.Wrap( -1 )
        gSizer1.Add( text11, 0, wx.ALL , 5 )

        btn2 = wx.Button( self.panel1, wx.ID_ANY, u"Show information", wx.DefaultPosition, wx.DefaultSize, 0 )
        btn2.Bind(wx.EVT_BUTTON, self.add_line)
        gSizer1.Add( btn2, 0, wx.ALL |wx.ALIGN_RIGHT, 5 )

        bitmap1 = wx.StaticBitmap(self.panel1, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.Size(100,100), 0 )
        bitmap1.Bind(wx.EVT_BUTTON, self.onItemSelected)
        gSizer1.Add( bitmap1, 0, wx.ALL, 5 )

        bitmap2 = wx.StaticBitmap(self.panel1, wx.ID_ANY, wx.NullBitmap, wx.DefaultPosition, wx.Size(100,100), 0 )
        bitmap2.Bind(wx.EVT_BUTTON, self.onItemSelected)
        gSizer1.Add( bitmap2, 0, wx.ALL |wx.ALIGN_RIGHT, 5 )
        bSizer2.Add( gSizer1, 1, wx.EXPAND, 5 )

        self.panel1.SetSizer(bSizer2)
        self.panel1.Layout()
        bSizer2.Fit(self.panel1)
        notebook1.AddPage(self.panel1, u"Contacts Info", True )
        self.panel2 = wx.Panel( notebook1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        notebook1.AddPage( self.panel2, u"User Chat", False )

        bSizer7 = wx.BoxSizer(wx.VERTICAL)
#format=wx.LIST_FORMAT_CENTRE
        self.list_ctrl2 = EditableListCtrl(self.panel2 ,size=(-1,450), style=wx.LC_REPORT|wx.BORDER_SUNKEN |wx.LC_SORT_ASCENDING)
        self.list_ctrl2.InsertColumn(0, 'Fid', width=150)
        self.list_ctrl2.InsertColumn(1,'Message', width=250)
        self.list_ctrl2.InsertColumn(2, 'Time Sent',width=150)

        self.list_ctrl2.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.onItemSelected)
        bSizer7.Add(  self.list_ctrl2, 0, wx.ALL|wx.EXPAND, 5 )
        self.panel2.SetSizer(bSizer7)

        btn3 = wx.Button( self.panel2, wx.ID_ANY, u"Show information", wx.DefaultPosition, wx.DefaultSize, 0 )
        btn3.Bind(wx.EVT_BUTTON, self.add_message)
        bSizer7.Add(btn3, 0, wx.ALL|wx.ALIGN_RIGHT, 5 )

        bSizer1.Add(notebook1, 1, wx.EXPAND |wx.ALL, 5 )

        self.SetSizer( bSizer1 )
        self.Layout()  

        self.SetSize((1000, 650))
        self.SetTitle('Simple menu')
        self.Move((100,100))
        self.Show(True)

        items = musicdata.items()
        row_list =[]
        self.myRowDict = {}

        #print items
        for key,data in  items:
            row_list.append(Friend_info(data[0],data[1],data[2]))
        index1 = 0
        for row in row_list:
            self.list_ctrl.SetItemData(index1, row.id)
            self.myRowDict[index1] = row
            index1 += 1

        items2 = musicdata2.items()
        row_list2 =[]
        self.myRowDict2 = {}
        index2 = 0
        for key,data in items2:
            row_list2.append(Messages(data[0],data[1],data[2]))
        for row in row_list2:
            self.list_ctrl2.SetItemData(index2, row.id)
            self.myRowDict2[index2] = row
            index2 += 1

    # button event for adding elements   
    def add_line(self, event):
        index = 0
        items = musicdata.items()     
        for key, data in items:
            self.list_ctrl.InsertStringItem(index, data[0])
            self.list_ctrl.SetStringItem(index,1, data[1])
            self.list_ctrl.SetStringItem(index, 2,data[2])
            self.list_ctrl.SetItemData(index, key)
            index += 1 


        self.itemDataMap = musicdata
        listmix.ColumnSorterMixin.__init__(self, 3)
        self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self.list_ctrl)

    def add_message(self, event):
        index = 0
        items = musicdata2.items()     
        for key, data in items:
            self.list_ctrl2.InsertStringItem(index, data[0])
            self.list_ctrl2.SetStringItem(index,1, data[1])
            self.list_ctrl2.SetStringItem(index, 2,data[2])
            self.list_ctrl2.SetItemData(index, key)
            index += 1 

        self.itemDataMap = musicdata2
        listmix.ColumnSorterMixin.__init__(self, 3)
        self.Bind(wx.EVT_LIST_COL_CLICK, self.OnColClick, self.list_ctrl2)

    def GetListCtrl(self):
        return self.list_ctrl2 

    def OnColClick(self, event):
        print "column clicked"
        event.Skip()      


    def onItemSelected(self,event):
        global size
        self.currentItem = event.m_itemIndex
        friend_info = self.myRowDict[self.list_ctrl.GetItemData(self.currentItem)]

        self.currentItem2 = event.m_itemIndex
        message = self.myRowDict2[self.list_ctrl2.GetItemData(self.currentItem2)]

        self.imageFile = "images/profile/%d.jpg" %int(friend_info.name)
        self.bmp = wx.Image(self.imageFile,wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
        bitmap1 = wx.StaticBitmap(self.panel1, -1, self.bmp, (5,400), (100,100))
        self.imageFile2 = "images/cover/%d.jpg" %int(friend_info.name)
        #img = Image.open('images/cover/%d.jpg'%int(friend_info.name))
        if os.path.exists(self.imageFile2):
        #dimensions_cover = img.size
            self.bmp2 = wx.Image(self.imageFile2,wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
            bitmap2 = wx.StaticBitmap(self.panel1, -1, self.bmp2, (200,390))
        print friend_info.name

def main():

    ex = wx.App()
    Example(None)
    ex.MainLoop()    


if __name__ == '__main__':
    main()       

如果我改变

def GetListCtrl(self):
        return self.list_ctrl2 

def GetListCtrl(self):
        return self.list_ctrl

它适用于每一个但我想让它们同时工作我该怎么办?

0 个答案:

没有答案