problem in painting over wx.grid control.

hi to all…
problem:
I’m having one grid control and need to paint over that…
when a button is clicked, current cell must be crossed by a line… It’s my application requirement…
by the way… I tried pygridcellrenderer class (draw method)… managed to get a cross line…
but when my application started particular cell is not crossed…
only it is crossed when that cell has received focus by mouse or keyboard…
and that cross line disappeared when i moved the grid cursor beyond some distance…
I need that painted cross line must be retained..
anyone knows how to fix this problem give me right direction —>
I’ve attached the code… and run the code staright…

advanced thanks…

ex.py (1.74 KB)

Hi,
Your problem is that you forgot to set the pen before drawing the line. So, it’s set to wx.TRANSPARENT when the cell isn’t selected and wx.BLACK_PEN when it is (or something like that). You just need to add dc.Pen = wx.BLACK_PEN before calling dc.DrawLine, like this:

def Draw(self, grid, attr, dc, rect, row, col, isSelected):
    text = grid.GetCellValue(row, col)
    hAlign, vAlign = attr.GetAlignment()
    dc.SetFont( attr.GetFont())

    x1 , y1 = rect.GetBottomLeft()
    x2 , y2 = rect.GetTopRight()
    dc.SetTextBackground(wx.WHITE)
    dc.SetTextForeground(wx.RED)
    dc.SetBrush(wx.Brush(wx.WHITE, wx.SOLID))
    #dc.SetPen(wx.TRANSPARENT_PEN)              # if enabled not working
    dc.DrawRectangleRect(rect)
    grid.DrawTextRectangle(dc, text, rect, hAlign, vAlign)
    dc.Pen = wx.BLACK_PEN               ##ADD THIS LINE
    dc.DrawLine(x1+2,y1-2,x2-2,y2+2)

hi to all… (Ronan Lamy)

your reply rocks… Thank U a lot…

I do read the docs and wxpython in action…
but nowhere i get this hint…
can u tell me how u got these hint… I think u r well experienced…

by the way… some things remains…

  1. After moving the cursor focus of particular cell, the rectangle border remains…
    only when it disappears, when i moved the cursor beyond some distance…
  2. particularly move upward or downward the cursor in D column… watch rendered cell…
    there is some border appears…
  3. That crossed line in rendered cell, is remains fine when the cursor within D column…
    when the cursor moves D or beyond that columns, for every move that crossed line/cell is repainted…

plz tell me how to fix these problems… becoz, finer control is required for my application…

advanced thanks…

···

On Wed, Feb 25, 2009 at 10:22 PM, Ronan Lamy Ronan.Lamy@normalesup.org wrote:

Hi,
Your problem is that you forgot to set the pen before drawing the line. So, it’s set to wx.TRANSPARENT when the cell isn’t selected and wx.BLACK_PEN when it is (or something like that). You just need to add dc.Pen = wx.BLACK_PEN before calling dc.DrawLine, like this:

def Draw(self, grid, attr, dc, rect, row, col, isSelected):
    text = grid.GetCellValue(row, col)
    hAlign, vAlign = attr.GetAlignment()
    dc.SetFont( attr.GetFont())

    x1 , y1 = rect.GetBottomLeft()


    x2 , y2 = rect.GetTopRight()
    dc.SetTextBackground(wx.WHITE)
    dc.SetTextForeground(wx.RED)
    dc.SetBrush(wx.Brush(wx.WHITE, wx.SOLID))
    #dc.SetPen(wx.TRANSPARENT_PEN)              # if enabled not working


    dc.DrawRectangleRect(rect)
    grid.DrawTextRectangle(dc, text, rect, hAlign, vAlign)
    dc.Pen = wx.BLACK_PEN               ##ADD THIS LINE
    dc.DrawLine(x1+2,y1-2,x2-2,y2+2)

wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

I do read the docs and wxpython in action…
but nowhere i get this hint…

can u tell me how u got these hint… I think u r well experienced…

Well experienced? Not really: I use trial and error and try to glean useful information from the wiki, the (C++ and Python) docs and the demo. I don’t know where there’s a good and comprehensive explanation of device contexts or of wxGrids though the wxGrid manual on the wiki is a nice start for the latter.

by the way… some things remains…

  1. After moving the cursor focus of particular cell, the rectangle border remains…

    only when it disappears, when i moved the cursor beyond some distance…

  2. particularly move upward or downward the cursor in D column… watch rendered cell…

    there is some border appears…

  3. That crossed line in rendered cell, is remains fine when the cursor within D column…
    when the cursor moves D or beyond that columns, for every move that crossed line/cell is repainted…

plz tell me how to fix these problems… becoz, finer control is required for my application…

Uncommenting the line
#dc.SetPen(wx.TRANSPARENT_PEN)

seems to fix all your problems, for reasons similar as in my first answer.

Cheers!

hi… again u rocks…

ok… any way my third problem remains… (watch my print statement ‘cross’ in console o/p.)

one more…

 grid.SetDefaultCellAlignment(wx.CENTRE,wx.CENTRE)

when i add this line in test frame class, this doesn’t seem to be working…

thanx…

···

On Fri, Feb 27, 2009 at 1:05 AM, Ronan Lamy Ronan.Lamy@normalesup.org wrote:

I do read the docs and wxpython in action…

but nowhere i get this hint…

can u tell me how u got these hint… I think u r well experienced…

Well experienced? Not really: I use trial and error and try to glean useful information from the wiki, the (C++ and Python) docs and the demo. I don’t know where there’s a good and comprehensive explanation of device contexts or of wxGrids though the wxGrid manual on the wiki is a nice start for the latter.

by the way… some things remains…

  1. After moving the cursor focus of particular cell, the rectangle border remains…

    only when it disappears, when i moved the cursor beyond some distance…

  2. particularly move upward or downward the cursor in D column… watch rendered cell…

    there is some border appears…

  3. That crossed line in rendered cell, is remains fine when the cursor within D column…
    when the cursor moves D or beyond that columns, for every move that crossed line/cell is repainted…

plz tell me how to fix these problems… becoz, finer control is required for my application…

Uncommenting the line
#dc.SetPen(wx.TRANSPARENT_PEN)

seems to fix all your problems, for reasons similar as in my first answer.

Cheers!


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

also…

Is there any way to draw a line as thick…?
( Now i’m using drawrectangle with desired height filling with brush…)

···

On Fri, Feb 27, 2009 at 2:15 PM, Murali kumar murali.au@gmail.com wrote:

hi… again u rocks…

ok… any way my third problem remains… (watch my print statement ‘cross’ in console o/p.)

one more…

 grid.SetDefaultCellAlignment(wx.CENTRE,wx.CENTRE)

when i add this line in test frame class, this doesn’t seem to be working…

thanx…

On Fri, Feb 27, 2009 at 1:05 AM, Ronan Lamy Ronan.Lamy@normalesup.org wrote:

I do read the docs and wxpython in action…

but nowhere i get this hint…

can u tell me how u got these hint… I think u r well experienced…

Well experienced? Not really: I use trial and error and try to glean useful information from the wiki, the (C++ and Python) docs and the demo. I don’t know where there’s a good and comprehensive explanation of device contexts or of wxGrids though the wxGrid manual on the wiki is a nice start for the latter.

by the way… some things remains…

  1. After moving the cursor focus of particular cell, the rectangle border remains…

    only when it disappears, when i moved the cursor beyond some distance…

  2. particularly move upward or downward the cursor in D column… watch rendered cell…

    there is some border appears…

  3. That crossed line in rendered cell, is remains fine when the cursor within D column…
    when the cursor moves D or beyond that columns, for every move that crossed line/cell is repainted…

plz tell me how to fix these problems… becoz, finer control is required for my application…

Uncommenting the line
#dc.SetPen(wx.TRANSPARENT_PEN)

seems to fix all your problems, for reasons similar as in my first answer.

Cheers!


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Murali kumar wrote:

also..

Is there any way to draw a line as thick..?
( Now i'm using drawrectangle with desired height filling with brush..)

Give the pen a width when you create it.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

thanx a lot…

one more thing…

When running my attached code, that crossed line in rendered cell, is remains fine when the   cursor within A-C column.. when the cursor moves D or beyond that columns, for every move that crossed line/cell is repainted..

anyone explain me… and fix that prob…

ex.py (1.92 KB)

···

On Sat, Feb 28, 2009 at 2:26 AM, Robin Dunn robin@alldunn.com wrote:

Murali kumar wrote:

also…

Is there any way to draw a line as thick…?

( Now i’m using drawrectangle with desired height filling with brush…)

Give the pen a width when you create it.

Robin Dunn

Software Craftsman

http://wxPython.org Java give you jitters? Relax with wxPython!


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

thanx a lot…

one more thing…

When running my attached code, that crossed line in rendered cell, is remains fine when the   cursor within A-C column.. when the cursor moves D or beyond that columns, for every move that crossed line/cell is repainted.. (watch my print statement 'cross' in console o/p..)

anyone explain me… and fix that prob…

ex.py (1.92 KB)

···

On Sat, Feb 28, 2009 at 2:26 AM, Robin Dunn robin@alldunn.com wrote:

Murali kumar wrote:

also…

Is there any way to draw a line as thick…?

( Now i’m using drawrectangle with desired height filling with brush…)

Give the pen a width when you create it.

Robin Dunn

Software Craftsman

http://wxPython.org Java give you jitters? Relax with wxPython!


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

thanx a lot…

  • When running my attached code, that crossed line in rendered cell, is remains fine when the cursor within A-C column… when the cursor moves D or beyond that columns, for every move that crossed line/cell is repainted… (watch my print statement ‘cross’ in console o/p…)

  • grid.SetDefaultCellAlignment(wx.CENTRE,wx.CENTRE)
    when i add this line in test frame class, this doesn’t (centre alignment) seem to be working…

anyone explain me… and fix these probs…

ex.py (1.92 KB)

···

On Sat, Feb 28, 2009 at 2:26 AM, Robin Dunn robin@alldunn.com wrote:

Murali kumar wrote:

also…

Is there any way to draw a line as thick…?

( Now i’m using drawrectangle with desired height filling with brush…)

Give the pen a width when you create it.

Robin Dunn

Software Craftsman

http://wxPython.org Java give you jitters? Relax with wxPython!


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Murali kumar wrote:

thanx a lot...

* When running my attached code, that crossed line in rendered cell, is remains fine when the cursor within A-C column.. when the cursor moves D or beyond that columns, for every move that crossed line/cell is repainted.. (watch my print statement 'cross' in console o/p..)

The renderer's Draw method is called whenever there is a paint event in which the update region intersects with the cell. This happens when another window obscures part of the grid, the grid is scrolled, when the borders need to be redrawn as the cell highlight moves to a new cell, and whenever else that the grid calls RefreshRect on itself. In other words, it is happening for a reason and so you just need to be prepared for it to be called often.

* grid.SetDefaultCellAlignment(wx.CENTRE,wx.CENTRE)
when i add this line in test frame class, this doesn't (centre alignment) seem to be working...

Because the alignment constant you want to use is wx.ALIGN_CENTER.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

hi…

In previous mail i’ve attached a simple code with one renderer…
In my application,

  • I’m using many custom renderer’s simultaneously…(ex: cross the cell with a line…)
  • one paint event called by a timer function in particular time interval… (ex: 1 or 2 secs)

For this scenario, when i run, my application is hangs sometimes… (paint event not performed…grid refresh not performed…) I think this is caused by custom renderer’s…
so, please give me right direction to the way go…
also give me some performance advice for the last mail…

···

On Sun, Mar 1, 2009 at 2:56 AM, Robin Dunn robin@alldunn.com wrote:

Murali kumar wrote:

thanx a lot…

  • When running my attached code, that crossed line in rendered cell, is remains fine when the cursor within A-C column… when the cursor moves D or beyond that columns, for every move that crossed line/cell is repainted… (watch my print statement ‘cross’ in console o/p…)

The renderer’s Draw method is called whenever there is a paint event in which the update region intersects with the cell. This happens when another window obscures part of the grid, the grid is scrolled, when the borders need to be redrawn as the cell highlight moves to a new cell, and whenever else that the grid calls RefreshRect on itself. In other words, it is happening for a reason and so you just need to be prepared for it to be called often.

  • grid.SetDefaultCellAlignment(wx.CENTRE,wx.CENTRE)

when i add this line in test frame class, this doesn’t (centre alignment) seem to be working…

Because the alignment constant you want to use is wx.ALIGN_CENTER.

Robin Dunn

Software Craftsman

http://wxPython.org Java give you jitters? Relax with wxPython!


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Murali kumar wrote:

hi..

In previous mail i've attached a simple code with one renderer..
In my application,
  * I'm using many custom renderer's simultaneously...(ex: cross the cell with a line..)
  * one paint event called by a timer function in particular time interval.. (ex: 1 or 2 secs)

Do you actually send a paint event or just call one of the Refresh methods?

For this scenario, when i run, my application is hangs sometimes.. (paint event not performed..grid refresh not performed..) I think this is caused by custom renderer's..

What makes you think that?

so, please give me right direction to the way go..
also give me some performance advice for the last mail..

It's not that hard. Like I said before, just be prepared to have the Draw method be called very many times. So anything that it does that could take some non-trivial time should be optimized (like cache some calculated values, etc.) and don't do anything that could take a long time (access a database) if you don't need to do it.

I have a very complex renderer that I am working on right now for the day-job and I've been very pleased with the performance. All I did was some common sense optimizations like you would/should do for any python code that could be executed many times.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

hi…

In previous mail i’ve attached a simple code with one renderer…

In my application,

  • I’m using many custom renderer’s simultaneously…(ex: cross the cell with a line…)

  • one paint event called by a timer function in particular time interval… (ex: 1 or 2 secs)

Do you actually send a paint event or just call one of the Refresh methods?

calling Refresh in my timer handler …

For this scenario, when i run, my application is hangs sometimes… (paint event not performed…grid refresh not performed…) I think this is caused by custom renderer’s…

What makes you think that?

U R CORRECT… actually i’m removed all print statement’s and did some tweaks… now it seems to be performing…

so, please give me right direction to the way go…

also give me some performance advice for the last mail…

It’s not that hard. Like I said before, just be prepared to have the Draw method be called very many times. So anything that it does that could take some non-trivial time should be optimized (like cache some calculated values, etc.) and don’t do anything that could take a long time (access a database) if you don’t need to do it.

I have a very complex renderer that I am working on right now for the day-job and I’ve been very pleased with the performance. All I did was some common sense optimizations like you would/should do for any python code that could be executed many times.

Thanks for ur reply…

and what to do clear all my custom renderer’s at once…?
clear grid only clears cell values… (now i did setting default renderer’s for all cells)

···

Robin Dunn

Software Craftsman

http://wxPython.org Java give you jitters? Relax with wxPython!


wxpython-users mailing list

wxpython-users@lists.wxwidgets.org

http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Murali kumar wrote:

and what to do clear all my custom renderer's at once..?
clear grid only clears cell values.. (now i did setting default renderer's for all cells)

I guess it depends on what you mean by "clear my custom renderers". Most likely you want to add code to the renderer's Draw method that draws the cell in a "cleared" state when there is no data in the table for that cell.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!