Overwritten TreeCtrl.OnCompareItems() not called in Python3/wxPython4
Thomas Krull <[email protected]> Wed, 7 Mar 2018 00:33:02 -0800 (PST)
| Newsgroups | gmane.comp.python.wxpython.devel |
|---|---|
| Message-ID | <[email protected]> |
I have a wx.TreeCtrl structure where the user can sort the items by different criteria (date, name, id, descending, ascending, ...). This worked fine in Python 2, but Python 3 (with wxPython 4) refuses sorting. The method CTreeCtrl.OnCompareItems() is called in Python 2, but never in Python 3. In the functools.cmp_to_key() documentation ( https://docs.python.org/3/library/functools.html) I found a hint: Python 3 does not support comparison functions. Confusing: in the description of wx.TreeCtrl (wxPython 4) there *is* a comparison method OnCompareItems() ( https://docs.wxpython.org/wx.TreeCtrl.html#wx.TreeCtrl.OnCompareItems). The description says, that together with this method I must use the RTTI macros DECLARE_DYNAMIC_CLASS and IMPLEMENT_DYNAMIC_CLASS since the baseclass does not know that I overwrote OnCompareItems(). I found descriptions of how to use this macros in C++, but nothing for python. Now I have no idea how I could make my program calling my OnCompareItems() method in Python3/wxPython 4. The file compare_or_not.py contains a small sample programm which reflects the problem. It runs with Python 2 and Python 3 as well. The print( 'in CTreeCtrl.OnCompareItems()' ) shows, that this method is called (in py2) or not called (in py3). Can anybody help? Regards, Humbalan -- You received this message because you are subscribed to the Google Groups "wxPython-dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
compare_or_not.py
(text/x-python, 1.7 KB)
import sys
import wx
class CTreeCtrl( wx.TreeCtrl ):
def __init__( self, parent ):
super( CTreeCtrl, self ).__init__( parent=parent, style=wx.TR_HIDE_ROOT )
def OnCompareItems( self, item1, item2 ):
print( 'in CTreeCtrl.OnCompareItems()' )
if sys.version_info.major < 3:
d1 = self.GetItemData( item1 ).Data
d2 = self.GetItemData( item2 ).Data
else:
d1 = self.GetItemData( item1 )
d2 = self.GetItemData( item2 )
if d1 < d2: return -1
elif d1 > d2: return 1
else : return 0
class CSettingsTree( wx.Dialog ):
def __init__( self, parent, settings ) :
size = wx.Size(200,150)
wx.Dialog.__init__( self, parent, title='all settings', size=size )
bSizer_main = wx.BoxSizer( wx.VERTICAL )
self.m_treeCtrl = CTreeCtrl( self )
bSizer_main.Add( self.m_treeCtrl, 0, wx.ALL|wx.EXPAND, 5 )
self.SetSizer( bSizer_main )
bSizer_main.Fit( self )
root = self.m_treeCtrl.AddRoot( 'Settings' )
for key, name in settings :
if sys.version_info.major < 3 : sort_key = wx.TreeItemData( name )
else : sort_key = name
self.m_treeCtrl.AppendItem( root, '{}: {}'.format(key, name), data=sort_key )
self.m_treeCtrl.ExpandAll()
self.m_treeCtrl.SortChildren( root )
#---------------------------------------------------------------------------
if __name__=="__main__":
app = wx.App( redirect=False )
settings = [(50,'Taylor'),(200,'Mueller'),(101,'Baker'),(102,'Smith')]
dlg = CSettingsTree( wx.Frame( None ), settings )
dlg.ShowModal()