# Changing the background color of slice views

**URL:** https://discourse.slicer.org/t/changing-the-background-color-of-slice-views/16151
**Category:** Development
**Created:** [February 23, 2021, 2:18am UTC](https://discourse.slicer.org/t/changing-the-background-color-of-slice-views/16151 "2021-02-23T02:18:46Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![giovform](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/giovform/32/4687_2.png) [@giovform](https://discourse.slicer.org/u/giovform)
#### Post date: [February 23, 2021, 2:18am UTC](https://discourse.slicer.org/t/changing-the-background-color-of-slice-views/16151/1 "2021-02-23T02:18:46Z")

</div>

I am not being able to change de slice views background colors. I am doing this:

```
viewNode = slicer.app.layoutManager().sliceWidget('Red').mrmlSliceNode()
viewNode.SetBackgroundColor(1,1,1)
viewNode.SetBackgroundColor2(1,1,1)

```

But it has no effect. What should I do? I want to change to white. Thank you!

---

<div class="post-metadata">

### Author: ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)
#### Post date: [February 19, 2022, 8:02am UTC](https://discourse.slicer.org/t/changing-the-background-color-of-slice-views/16151/2 "2022-02-19T08:02:04Z")

</div>

Try:  
`view = slicer.app.layoutManager().sliceWidget('Red').sliceView() view.setBackgroundColor(qt.QColor.fromRgbF(1,1,1))`

- From [Slicer/screencapture.md at ed0fdbe71c5e98ef384a93d17850403aca8e685f · Slicer/Slicer (github.com)](https://github.com/Slicer/Slicer/blob/ed0fdbe71c5e98ef384a93d17850403aca8e685f/Docs/developer_guide/script_repository/screencapture.md#capture-3d-view-into-png-file-with-transparent-background)

---

<div class="post-metadata">

### Author: ![Dwij\_Mistry](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dwij_mistry/32/15265_2.png) [@Dwij\_Mistry](https://discourse.slicer.org/u/Dwij_Mistry)
#### Post date: [February 19, 2022, 10:05am UTC](https://discourse.slicer.org/t/changing-the-background-color-of-slice-views/16151/3 "2022-02-19T10:05:47Z")

</div>

@jumbojing you forgot to add view.forceRender() to apply the modifications

For all slice view we can write like

```auto
layoutManager = slicer.app.layoutManager()
for sliceViewName in layoutManager.sliceViewNames():
  view = layoutManager.sliceWidget(sliceViewName).sliceView()
  view.setBackgroundColor(qt.QColor.fromRgbF(1,1,1))
  view.forceRender()

```

---

<div class="post-metadata">

### Author: ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)
#### Post date: [February 19, 2022, 10:11am UTC](https://discourse.slicer.org/t/changing-the-background-color-of-slice-views/16151/4 "2022-02-19T10:11:42Z")

</div>

@Dwij_Mistry Perfect! Thanks 👍

但是, 如果每个slice有个边框就更好了

> However, it would be better if each slice had a balck border.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/f/5fec429061630487d60912864a4237207bf21466.png)

---

<div class="post-metadata">

### Author: ![Dwij\_Mistry](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dwij_mistry/32/15265_2.png) [@Dwij\_Mistry](https://discourse.slicer.org/u/Dwij_Mistry)
#### Post date: [February 19, 2022, 12:53pm UTC](https://discourse.slicer.org/t/changing-the-background-color-of-slice-views/16151/5 "2022-02-19T12:53:06Z")

</div>

> [@jumbojing](#):
>
> However, it would be better if each slice had a balck border.

If you are ok to use dark mode of slicer then we can get border which is nothing but a background color of central widget of MainWindow

 ![Screenshot (110)](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/d/7/d719d534d14f8c7b0ae3fae13560ed92d233669c.png)

**Dark mode can be activated from Menu → Edit → application settings → appearance → style → dark slicer**

---

<div class="post-metadata">

### Author: ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)
#### Post date: [February 19, 2022, 12:57pm UTC](https://discourse.slicer.org/t/changing-the-background-color-of-slice-views/16151/6 "2022-02-19T12:57:10Z")

</div>

Great! Thanks 👍

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/e/5e9da3762dc3bbcb25615b60c861915d3145112c.png)

---

<div class="post-metadata">

### Author: ![Dwij\_Mistry](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dwij_mistry/32/15265_2.png) [@Dwij\_Mistry](https://discourse.slicer.org/u/Dwij_Mistry)
#### Post date: [February 25, 2022, 4:45pm UTC](https://discourse.slicer.org/t/changing-the-background-color-of-slice-views/16151/7 "2022-02-25T16:45:54Z")

</div>

@jumbojing

Finally I found a way by which we can have border without changing the slicer color mode.

```auto

def paint_border(view):
  color = [0.0, 0.0, 0.0] #change color for border 
  lineThickness = 10.0
  viewRenderer = view.renderWindow().GetRenderers().GetItemAsObject(0)
  borderPoints = vtk.vtkPoints()
  borderPoints.InsertNextPoint( 1e-4, 1e-4, 0)
  borderPoints.InsertNextPoint(0.9999, 1e-4, 0)
  borderPoints.InsertNextPoint(0.9999, 0.9999, 0)
  borderPoints.InsertNextPoint( 1e-4, 0.9999, 0)
  borderCells = vtk.vtkCellArray()
  borderCells.InsertNextCell(5)
  for i in range(5):
    borderCells.InsertCellPoint(i%5)
  borderPolyData = vtk.vtkPolyData()
  borderPolyData.SetPoints(borderPoints)
  borderPolyData.SetLines(borderCells)
  borderCoordinate = vtk.vtkCoordinate()
  borderCoordinate.SetCoordinateSystemToNormalizedViewport()
  borderCoordinate.SetViewport(viewRenderer)
  borderPolyDataMapper = vtk.vtkPolyDataMapper2D()
  borderPolyDataMapper.SetInputData(borderPolyData)
  borderPolyDataMapper.SetTransformCoordinate(borderCoordinate)
  borderPolyDataMapper.SetTransformCoordinateUseDouble(True)
  highlightedBorderActor = vtk.vtkActor2D()
  highlightedBorderActor.SetMapper(borderPolyDataMapper)
  highlightedBorderActor.GetProperty().SetColor(color[0], color[1], color[2])
  highlightedBorderActor.GetProperty().SetDisplayLocationToForeground()
  highlightedBorderActor.GetProperty().SetLineWidth(lineThickness)
  viewRenderer.AddActor2D(highlightedBorderActor)
  view.renderWindow().Render()

#calling 
paint_border(slicer.app.layoutManager().threeDWidget(0).threeDView())
for sliceViewName in slicer.app.layoutManager().sliceViewNames():
    paint_border(slicer.app.layoutManager().sliceWidget(sliceViewName).sliceView())

```

OutPut

 ![Capture](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/6/76fa84c76af6f5279f03bcdf2bf93320d4cc0d65.png)

---

<div class="post-metadata">

### Author: ![jumbojing](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jumbojing/32/10811_2.png) [@jumbojing](https://discourse.slicer.org/u/jumbojing)
#### Post date: [March 1, 2022, 1:13pm UTC](https://discourse.slicer.org/t/changing-the-background-color-of-slice-views/16151/8 "2022-03-01T13:13:44Z")

</div>

> [@Dwij\_Mistry](#):
>
> ```auto
> def paint_border(view):
> color = [0.0, 0.0, 0.0] #change color for border 
> lineThickness = 10.0
> viewRenderer = view.renderWindow().GetRenderers().GetItemAsObject(0)
> borderPoints = vtk.vtkPoints()
> borderPoints.InsertNextPoint( 1e-4, 1e-4, 0)
> borderPoints.InsertNextPoint(0.9999, 1e-4, 0)
> borderPoints.InsertNextPoint(0.9999, 0.9999, 0)
> borderPoints.InsertNextPoint( 1e-4, 0.9999, 0)
> borderCells = vtk.vtkCellArray()
> borderCells.InsertNextCell(5)
> for i in range(5):
> borderCells.InsertCellPoint(i%5)
> borderPolyData = vtk.vtkPolyData()
> borderPolyData.SetPoints(borderPoints)
> borderPolyData.SetLines(borderCells)
> borderCoordinate = vtk.vtkCoordinate()
> borderCoordinate.SetCoordinateSystemToNormalizedViewport()
> borderCoordinate.SetViewport(viewRenderer)
> borderPolyDataMapper = vtk.vtkPolyDataMapper2D()
> borderPolyDataMapper.SetInputData(borderPolyData)
> borderPolyDataMapper.SetTransformCoordinate(borderCoordinate)
> borderPolyDataMapper.SetTransformCoordinateUseDouble(True)
> highlightedBorderActor = vtk.vtkActor2D()
> highlightedBorderActor.SetMapper(borderPolyDataMapper)
> highlightedBorderActor.GetProperty().SetColor(color[0], color[1], color[2])
> highlightedBorderActor.GetProperty().SetDisplayLocationToForeground()
> highlightedBorderActor.GetProperty().SetLineWidth(lineThickness)
> viewRenderer.AddActor2D(highlightedBorderActor)
> view.renderWindow().Render()
> 
> #calling 
> paint_border(slicer.app.layoutManager().threeDWidget(0).threeDView())
> for sliceViewName in slicer.app.layoutManager().sliceViewNames():
> paint_border(slicer.app.layoutManager().sliceWidget(sliceViewName).sliceView())
> 
> ```
> 
> OutPut

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/3/636a1933151b40f654a11c4db87a9b0db079fe60.png)

@Dwij_Mistry what’s wrong? a black line

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/d/7d29aeab760acb98de11a2209a0fd18661c62c16.png)

---

<div class="post-metadata">

### Author: ![Dwij\_Mistry](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dwij_mistry/32/15265_2.png) [@Dwij\_Mistry](https://discourse.slicer.org/u/Dwij_Mistry)
#### Post date: [May 12, 2022, 11:05am UTC](https://discourse.slicer.org/t/changing-the-background-color-of-slice-views/16151/9 "2022-05-12T11:05:25Z")

</div>

I am also facing the same issue, Can any one please share why it is happening? or any alternative solution?

@lassoan @pieper @jamesobutler @jcfr @cpinter

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [May 12, 2022, 12:54pm UTC](https://discourse.slicer.org/t/changing-the-background-color-of-slice-views/16151/10 "2022-05-12T12:54:20Z")

</div>

I cannot reproduce this, so I cannot tell what’s wrong, but maybe you need to tweak the `1e-4` and `0.9999` constants.
