Graphic in wxGrid?!?

Post here if you need help with creating a Graphical User Interface in Python.

Graphic in wxGrid?!?

Postby eslavko » Sun Dec 29, 2013 3:22 pm

I'm totaly new in wx (well in python too...)
I can't find help how to draw some graphic (lines/text) in Grid cell?
In this example I can enter text but how to do overlay draw? (for example line from bottom left to top right corner?)

Code: Select all
import wx
import wx.grid as grid

class Frame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, "Grid", size=(350,250))
        self.grid = grid.Grid(self)
        self.grid.CreateGrid(100, 10)

app = wx.PySimpleApp()
frame = Frame(None)
frame.Show()

app.MainLoop()

eslavko
 
Posts: 25
Joined: Thu Dec 26, 2013 10:23 am

Re: Graphic in wxGrid?!?

Postby octopez » Sun Dec 29, 2013 9:34 pm

Have you taken a look at the wxPython Docs & Demos?
I'm quite sure, that there is an example for this.
Unlike some other "demos" provided in other programming languages/toolkits, the wxPython Docs & Demos is a huge repository of helpful knowledge. Every (!) widget has it's own example with description, democode, ...
Dig into it, you won't be disapointed!
Greetings and all the best,
Octopez

Strange women lying in ponds distributing swords is no basis for a system of government.
User avatar
octopez
 
Posts: 25
Joined: Fri Feb 08, 2013 11:26 pm

Re: Graphic in wxGrid?!?

Postby eslavko » Mon Dec 30, 2013 9:01 am

octopez wrote:Have you taken a look at the wxPython Docs & Demos?
I'm quite sure, that there is an example for this.
Unlike some other "demos" provided in other programming languages/toolkits, the wxPython Docs & Demos is a huge repository of helpful knowledge. Every (!) widget has it's own example with description, democode, ...
Dig into it, you won't be disapointed!


I spend a half a day to find that but without sucess. The documentation for wx is a little confusing comparing for tk one. And a lot of examples is for non python languages...
eslavko
 
Posts: 25
Joined: Thu Dec 26, 2013 10:23 am

Re: Graphic in wxGrid?!?

Postby Yoriz » Mon Dec 30, 2013 12:13 pm

In the wxpython demo, take a look at the example Grid_megaExample.
It uses PyGridCellRenderer to make a custom cell renderer, you overwrite its draw method and using the dc(device contex) draw what you require.

Also in the wxPython3.0 Docs and Demos\wxPython\samples\wxPIA_book\Chapter-14\grid_renderer.py is the following example.
Code: Select all
import wx
import wx.grid
import random

class RandomBackgroundRenderer(wx.grid.PyGridCellRenderer):
    def __init__(self):
        wx.grid.PyGridCellRenderer.__init__(self)


    def Draw(self, grid, attr, dc, rect, row, col, isSelected):
        text = grid.GetCellValue(row, col)
        hAlign, vAlign = attr.GetAlignment()
        dc.SetFont( attr.GetFont() )
        if isSelected:
            bg = grid.GetSelectionBackground()
            fg = grid.GetSelectionForeground()
        else:
            bg = random.choice(["pink", "sky blue", "cyan", "yellow", "plum"])
            fg = attr.GetTextColour()

        dc.SetTextBackground(bg)
        dc.SetTextForeground(fg)
        dc.SetBrush(wx.Brush(bg, wx.SOLID))
        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.DrawRectangleRect(rect)
        grid.DrawTextRectangle(dc, text, rect, hAlign, vAlign)


    def GetBestSize(self, grid, attr, dc, row, col):
        text = grid.GetCellValue(row, col)
        dc.SetFont(attr.GetFont())
        w, h = dc.GetTextExtent(text)
        return wx.Size(w, h)

    def Clone(self):
        return RandomBackgroundRenderer()

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Grid Renderer",
                          size=(640,480))

        grid = wx.grid.Grid(self)
        grid.CreateGrid(50,50)

        # Set this custom renderer just for row 4
        attr = wx.grid.GridCellAttr()
        attr.SetRenderer(RandomBackgroundRenderer())
        grid.SetRowAttr(4, attr)

        for row in range(10):
            for col in range(10):
                grid.SetCellValue(row, col,
                                  "cell (%d,%d)" % (row, col))

app = wx.App(False)
frame = TestFrame()
frame.Show()
app.MainLoop()
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 837
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Graphic in wxGrid?!?

Postby eslavko » Mon Dec 30, 2013 3:38 pm

Thanks for info.
I just make thing in tkinter so no longer needed. But maybe next time.
eslavko
 
Posts: 25
Joined: Thu Dec 26, 2013 10:23 am


Return to GUI

Who is online

Users browsing this forum: No registered users and 2 guests