`ForceRefresh` after `AppendRows` does not display new items .

1,154 views
Skip to first unread message

Olemis Lang

unread,
Nov 17, 2009, 1:33:33 PM11/17/09
to wxpytho...@googlegroups.com
I am appending a row in a grid this way :

{{{
#!python

dialog = wx.TextEntryDialog(self, *args)
try :
if dialog.ShowModal() == wx.ID_OK:
value = dialog.GetValue()
if value :
try :
self.grid.GetTable().add_value(value)
self.notify() <=====
********** ForceRefresh called in here **********
except ValueError, exc :
wx.MessageBox(exc.message, caption="Invalid value", style=wx.OK)
else :
wx.MessageBox("Cannot be empty", caption="Invalid value", style=wx.OK)
finally :
dialog.Destroy()

}}}

and the Grid doesn't display the new row . I have confirmed that the
model includes the recently created row and also tried grid.AppendRows
(the model's method always returns True ) but everything remains just
the same (IOW model.GetNumberRows returns e.g. 15 whereas
grid.GetNumberRows returns 14 ).

Why is this happening ? What can be done ?

Thnx in advance !

--
Regards,

Olemis.

Blog ES: http://simelo-es.blogspot.com/
Blog EN: http://simelo-en.blogspot.com/

Featured article:

Frank Millman

unread,
Nov 18, 2009, 12:58:39 AM11/18/09
to wxpytho...@googlegroups.com

There is an extra step you have to take, but I have never seen it in any
formal documentation. The only place I have seen it is in some of the Grid
examples in the demo.

After appending, inserting, or deleting rows (or columns) in the table, you
must notify the grid of the change, like this -


msg = wx.grid.GridTableMessage(self, # The table
wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it
1 # how many
)

or

msg = wx.grid.GridTableMessage(self, # The table
wx.grid.GRIDTABLE_NOTIFY_ROWS_INSERTED, # what we did to it
5, # from which row
1 # how many
)

or

msg = wx.grid.GridTableMessage(self, # The table
wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, # what we did to it
5, # from which row
1 # how many
)

followed by

self.GetView().ProcessTableMessage(msg)

This assumes that it is called from the Table class.

If it is called from the Grid class, change self to self.GetTable(), and
self.GetView() to self.

HTH

Frank Millman

Olemis Lang

unread,
Nov 18, 2009, 8:08:23 AM11/18/09
to wxpytho...@googlegroups.com
On Wed, Nov 18, 2009 at 12:58 AM, Frank Millman <fr...@chagford.com> wrote:
>
> Olemis Lang wrote:
>>
>> I am appending a row in a grid this way :
>>
>> {{{
>> #!python
>>
>>     dialog = wx.TextEntryDialog(self, *args)
>>     try :
>>       if dialog.ShowModal() == wx.ID_OK:
>>         value = dialog.GetValue()
>>         if value :
>>           try :
>>             self.grid.GetTable().add_value(value)
>>             self.notify()                                       <=====
>>  ********** ForceRefresh called in here **********
>>           except ValueError, exc :
>>             wx.MessageBox(exc.message, caption="Invalid
>> value", style=wx.OK)
>>         else :
>>           wx.MessageBox("Cannot be empty", caption="Invalid
>> value", style=wx.OK)
>>     finally :
>>       dialog.Destroy()
>>
>> }}}
>>
>> and the Grid doesn't display the new row . I have confirmed
>> that the model includes the recently created row and also
>> tried grid.AppendRows
>>
[...]

>>
>> (IOW
>> model.GetNumberRows returns e.g. 15 whereas
>> grid.GetNumberRows returns 14 ).
>>
>
> There is an extra step you have to take,

I'll try that ... I suppose that the same holds for ClearGrid (/me
experiencing similar problems :-/ )

> but I have never seen it in any
> formal documentation.

Undocumented thingies ... Hmmmm . IMO it should be documented somewhere .

> The only place I have seen it is in some of the Grid
> examples in the demo.
>

I c . And they are in cyberspace somewhere, isn't it ?

> After appending, inserting, or deleting rows (or columns) in the table, you
> must notify the grid of the change, like this -
>
>

Just to satisfy my curiosity : Shouldn't all these happen
transparently once ForceRefresh or (AppendRows | ... ) is called ? Is
there a (good) reason for sending an extra (non-intuitive IMO )
message to the grid ?

BTW : Thnx !

Mike Driscoll

unread,
Nov 18, 2009, 10:09:23 AM11/18/09
to wxPython-users
They are documented. See the wiki:

http://wiki.wxpython.org/wxGrid

Or the wxPython demo: http://wxpython.org/download.php


>
> > The only place I have seen it is in some of the Grid
> > examples in the demo.
>
> I c . And they are in cyberspace somewhere, isn't it ?


The demo can be found here: http://wxpython.org/download.php

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Olemis Lang

unread,
Nov 18, 2009, 10:27:12 AM11/18/09
to wxpytho...@googlegroups.com

I see . Thnx !

BTW, if `BasePropertyTable` is a well known «patch» to workaround this
situation :

Q:
- Why not to include it in wxPython as a helper ?

Mike Driscoll

unread,
Nov 18, 2009, 11:55:18 AM11/18/09
to wxPython-users


On Nov 18, 9:27 am, Olemis Lang <ole...@gmail.com> wrote:
I don't see any mention of that on that page, so I'm not sure why you
think it's a well known "patch". It's really up to Robin Dunn or one
of the other wx devs to decide if something is worth including with
the distro.

Olemis Lang

unread,
Nov 18, 2009, 12:33:29 PM11/18/09
to wxpytho...@googlegroups.com
On Wed, Nov 18, 2009 at 11:55 AM, Mike Driscoll <kyos...@gmail.com> wrote:
>
> On Nov 18, 9:27 am, Olemis Lang <ole...@gmail.com> wrote:
>> On Wed, Nov 18, 2009 at 10:09 AM, Mike Driscoll <kyoso...@gmail.com> wrote:
>>
[...]

>>
>> > They are documented. See the wiki:
>>
>> >http://wiki.wxpython.org/wxGrid
>>
>> > Or the wxPython demo:http://wxpython.org/download.php
>>
>> I see . Thnx !
>>
>> BTW, if `BasePropertyTable` is a well known «patch» to workaround this
>> situation :
>>
>> Q:
>>     - Why not to include it in wxPython as a helper ?
>>
>
> I don't see any mention of that on that page, so I'm not sure why you
> think it's a well known "patch".

Well , probably I was wrong (and | or) got the wrong (and premature )
impression about whether that was «well known "patch"» . You're right.
But I am really sure that, once those operations (append, delete,
clear ...) are performed , the human intuition makes programmers (like
me) think that those changes will be reflected in the grid. The use of
such events directly is just a «hack» (didn't find the right word ...
) that exposes the details of the underlying mechanisms available in
(wxPython | wxWiidgets ) to communicate with widgets . Considering
that I agree with those people ( e.g. Trygve Reenskaug and James O.
Coplien authors of MVC [1]_ and DCI [2]_ architectures ) that state
that «Object oriented programming grew out of Doug Englebart's vision
of the computer as an extension of the human mind» and therefore «the
goal of object-oriented programming pioneers was to capture end user
mental models in the code» and that's why the goal should be to
«provide the illusion of a direct connection from the end user brain
to the computer "brain"» ... then IMO they should be hidden somehow.
That's what encapsulation is for ;o)

If this is not possible directly in the base classes (e.g. due to the
fact that wxPython depends on wxWidgets and SWIG and ...) at least
IMHO it would be nice to have a ready-to-use shortcut somewhere to get
this done OOTB . It's just a suggestion ; but you know, probably I'm
wrong (anyway, I was born by mistake :P ...)

> It's really up to Robin Dunn or one
> of the other wx devs to decide if something is worth including with
> the distro.
>

Of course , that's why I am asking it here, it's just a suggestion .
I'm just pushing them a little to see if they fall . I keep waiting at
the bottom of the precipice ... ;o) ... but probably programmers will
be the real winners .

PS: ... just a funny way to say a serious thing

;o)

.. [1] Model–view–controller
(http://en.wikipedia.org/wiki/Model–view–controller)

.. [2] The DCI Architecture: A New Vision of Object-Oriented Programming
(http://www.artima.com/articles/dci_vision.html)

Robin Dunn

unread,
Nov 19, 2009, 9:28:49 PM11/19/09
to wxpytho...@googlegroups.com
On 11/18/09 5:08 AM, Olemis Lang wrote:

> Just to satisfy my curiosity : Shouldn't all these happen
> transparently once ForceRefresh or (AppendRows | ... ) is called ? Is
> there a (good) reason for sending an extra (non-intuitive IMO )
> message to the grid ?

It's just the way that the API was designed, and was probably meant to
help facilitate a looser coupling between the table and the grid. IOW,
to allow the grid and the table to be somewhat out of sync if there is a
need for it.


--
Robin Dunn
Software Craftsman
http://wxPython.org

Reply all
Reply to author
Forward
0 new messages