Hello I'm new to the forums and I want to ask some questions about grid like operations:

Here I make a grid and set all cellvalues to 0.

import wx
import wx.grid

class TestTable(wx.grid.PyGridTableBase):
    def __init__(self):
        wx.grid.PyGridTableBase.__init__(self)
        self.rowLabels = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
        self.colLabels = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]            
        
    def GetNumberRows(self):
        return 10

    def GetNumberCols(self):
        return 10

    def IsEmptyCell(self, row, col):
        return False
    
    def GetValue(self, row, col):
        return 0
    
    def SetValue(self, row, col, value):
        pass
        
    def GetColLabelValue(self, col):
        return self.colLabels[col]
       
    def GetRowLabelValue(self, row):
        return self.rowLabels[row]

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Grid Table",
                          size=(500,200))
        grid = wx.grid.Grid(self)
        table = TestTable()
        grid.SetTable(table, True)
        
app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()

Now I want to make some blocks of cells and these blocks should get another value say 1...
With a result as something like this:

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 1 1 0 1 1 1 1 0 0
0 1 1 0 1 1 1 1 0 0
0 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

My question is how I can make selections of groups of cells by blocks and change the values to say 1. After this I also need to be able to retrieve information of the position and value of these 'separate' blocks...

Some results I want to have are something like this:
Block 1 = [3C-4F] value = 1
Block 2 = [6B-8C] value = 1
Block 3 = [6E-7H] value = 1

I'm kinda new to programming and I'm allready stuck lol...

Anyones help on this would be great.

Since your TestTable objects are instances of this class http://wxpython.org/docs/api/wx.grid.GridTableBase-class.html,
I suppose you can use directly it's methods on your objects (like SetValue ). Also your method redefinitions hide the original methods, so may be you should drop, or rewrite your methods so that they call the methods of the parent class.

Ok,

so i started this in another way because I don't need a gui and I need to write my own grid class:

As far as I am now I have this:

# Create a Python spatial grid.

class Grid(object):
    """Grid class with a width, height"""
    def __init__(self, width, height):
        self.grid = []
        self.width = width
        self.height = height

    def new_grid(self):
        """Create an empty grid"""
        grid = []
        self.cell_value = 0
        # print self.width * self.height
        for i in range(self.width * self.height):
            grid.append(self.cell_value)
        return grid
        
    def print_grid(self):
        for i in range(self.width):
            for j in range(self.height):
                print self.cell_value,
            print

class Cell(object):
    def __init__(self, x, y, value):
        self.x = x
        self.y = y
        self.value = value

    def inspect(self):
        "#<Cell: value = #(value), x = #(x), y = #(y)>"

my_grid = Grid(10, 10)
my_grid.new_grid()
my_grid.print_grid()

print my_grid

This prints out my grid like this:

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
<__main__.Grid object at 0x00F4B230>

I now want to add a method that sets a block of cells in this grid to the value of 1 something like this:

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

Can anyone help me with this because I really have the feeling I lost it :(

How can I add a method that selects a group/block of cells and sets the value of the cells in this block to value 1.

Ok so I changed a bit more my code into this:

# Create a Python spatial grid.

from math import sqrt

class Grid(object):
    """Grid class with a width, height, length"""
    def __init__(self, width, height):
        self.grid = []
        self.width = width
        self.height = height
        #self.length = width * height
        for x in range(width):
            col = []
            for y in range(height):
                col.append(Cell(x, y, self.grid))
                self.grid.append(col)

    def setItem(self, x, y, val):
            self.grid[x][y] = val
            
    def printGrid(self):
        for x in range(self.width):
            for y in range(self.height):
                print self.grid[x][y].value,
            print
        
class Cell(object):
    def __init__(self, x, y, grid):
        self.x = x
        self.y = y
        self.grid = grid
        self.value = 0

    def inspect(self):
        "#<Cell: value = #(value), x = #(x), y = #(y)>"

my_grid = Grid(5, 5)
my_grid.printGrid()
my_grid.setItem(1, 1, 1)
my_grid.printGrid()

How do i need to rewrite my setItem method or my printGrid method to make this work?

I need to set items in my grid to a value but in this way my setItem method and printGrid method interfere with eachother... pls some help on this...

You really need to establish a list of row_lists ...

# forming a matrix/grid ...

class Grid(object):
    """Grid class with a width, height"""
    def __init__(self, width, height):
        #self.grid = []
        self.width = width
        self.height = height

    def new_grid(self):
        """Create an empty grid"""
        self.grid = []
        row_grid = []
        cell_value = 0
        for col in range(self.width):
            for row in range(self.height):
                row_grid.append(cell_value)
            self.grid.append(row_grid)
            row_grid = []  # reset row
        #print self.grid, col, row  # testing
        return self.grid

    def custom_grid(self, startx, endx, starty, endy, val=1):
        """put val into selected rows (y) and columns (x)"""
        self.grid = []
        row_grid = []
        cell_value = 0  # default val
        for col in range(self.width):
            for row in range(self.height):
                if (startx <= col <= endx) and (starty <= row <= endy):
                    row_grid.append(val)
                else:
                    row_grid.append(cell_value)
            self.grid.append(row_grid)
            row_grid = []  # reset row
        return self.grid

    def print_grid(self):
        for col in range(self.width):
            for row in range(self.height):
                print self.grid[col][row],
            print


my_grid = Grid(10, 10)
print "Empty grid ..."
my_grid.new_grid()
my_grid.print_grid()

print '-'*20

print "Custom grid ..."
# note that x forms the columns and y forms the rows
my_grid.custom_grid(startx=3, endx=6, starty=3, endy=6, val=1)
my_grid.print_grid()

"""
my output -->
Empty grid ...
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
--------------------
Custom grid ...
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
"""

Wow,

that's actually pretty good...and after watching your code it doesnt seem that hard...guess my inexperience with coding makes it hard for me to think off this stuff myself...

I also added some code so I will be able to load an external csv file that can be seen as another grid. This way the program should be able to make a new grid ánd to read a grid that is imported (or loaded) through a csv file.

For example my csv file looks like this:
0;0;0;0;0;0
0;1;1;1;0;0
0;1;1;1;0;0
0;0;0;0;0;0
0;0;0;0;0;0
0;0;0;0;0;0

Now I use a 'class method' to load this csv file...Can I still use the old methods of the Grid class like getting a value from this loaded csv file? And if yes how should I do this because now my load method doesn't make an object (because of the class method??)

def load(cls, filename):
        print "Loading code"
        loadGrid = []
        reader = csv.reader(open(filename), delimiter=';')
        for line in reader:
            loadGrid.append(line)
        print loadGrid
    load = classmethod(load)

-----------------------------------

grid = Grid.load("test.csv")
print grid
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.