[GH-ISSUE #362] SetBackgroundColor draws over grid #268

Closed
opened 2026-03-04 01:03:29 +03:00 by kerem · 1 comment
Owner

Originally created by @ghost on GitHub (Oct 31, 2019).
Original GitHub issue: https://github.com/rivo/tview/issues/362

When i set the background color after creating a grid, the entire terminal gets the color and i can't see the grid anymore.

package main

import (
        "github.com/rivo/tview"
        "github.com/gdamore/tcell"
)

func main() {
        newPrimitive := func(text string) tview.Primitive {
                return tview.NewTextView().
                        SetTextAlign(tview.AlignCenter).
                        SetText(text)
        }
        songList := newPrimitive("songs")
        songInfo := newPrimitive("info")
        grid := tview.NewGrid().
                SetRows(-1, 5).
                SetColumns(0).
                SetBorders(true).
                AddItem(songList, 0, 0, 1, 1, 0, 0, false).
                AddItem(songInfo, 1, 0, 1, 1, 0, 0, false).
                SetBackgroundColor(tcell.ColorDefault)


        if err := tview.NewApplication().SetRoot(grid, true).SetFocus(grid).Run(); err != nil {
                panic(err)
        }
}

Seizing the opportunity. I'm learning programming right now and i started with Go and tview. I made a function that lists all the songs in a folder, prints and play the songs in sequence. How do i execute this function inside the songList primitive in this grid? I want the song list printed there.

Thanks!

Originally created by @ghost on GitHub (Oct 31, 2019). Original GitHub issue: https://github.com/rivo/tview/issues/362 When i set the background color after creating a grid, the entire terminal gets the color and i can't see the grid anymore. ``` package main import ( "github.com/rivo/tview" "github.com/gdamore/tcell" ) func main() { newPrimitive := func(text string) tview.Primitive { return tview.NewTextView(). SetTextAlign(tview.AlignCenter). SetText(text) } songList := newPrimitive("songs") songInfo := newPrimitive("info") grid := tview.NewGrid(). SetRows(-1, 5). SetColumns(0). SetBorders(true). AddItem(songList, 0, 0, 1, 1, 0, 0, false). AddItem(songInfo, 1, 0, 1, 1, 0, 0, false). SetBackgroundColor(tcell.ColorDefault) if err := tview.NewApplication().SetRoot(grid, true).SetFocus(grid).Run(); err != nil { panic(err) } } ``` Seizing the opportunity. I'm learning programming right now and i started with Go and tview. I made a function that lists all the songs in a folder, prints and play the songs in sequence. How do i execute this function inside the songList primitive in this grid? I want the song list printed there. Thanks!
kerem closed this issue 2026-03-04 01:03:30 +03:00
Author
Owner

@rivo commented on GitHub (Nov 27, 2019):

SetBackgroundColor() is a function of Box and it will return an instance of Box so your grid variable is not a Grid. This means that only the grid's super class (Box) will be drawn. It's empty so you don't see anything. You should call SetBackgroundColor() separately:

package main

import (
	"github.com/gdamore/tcell"
	"github.com/rivo/tview"
)

func main() {
	newPrimitive := func(text string) tview.Primitive {
		return tview.NewTextView().
			SetTextAlign(tview.AlignCenter).
			SetText(text)
	}
	songList := newPrimitive("songs")
	songInfo := newPrimitive("info")
	grid := tview.NewGrid().
		SetRows(-1, 5).
		SetColumns(0).
		SetBorders(true).
		AddItem(songList, 0, 0, 1, 1, 0, 0, false).
		AddItem(songInfo, 1, 0, 1, 1, 0, 0, false)
	grid.SetBackgroundColor(tcell.ColorDefault)

	if err := tview.NewApplication().SetRoot(grid, true).SetFocus(grid).Run(); err != nil {
		panic(err)
	}
}

Regarding your other question, you can write to a TextView at any time:

fmt.Fprintln(textView,"Song A")

See also SetChangedFunc() for some information on how to deal with that.

Let me know in case there's anything else that needs clarification.

<!-- gh-comment-id:559180124 --> @rivo commented on GitHub (Nov 27, 2019): `SetBackgroundColor()` is a function of `Box` and it will return an instance of `Box` so your `grid` variable is not a `Grid`. This means that only the grid's super class (`Box`) will be drawn. It's empty so you don't see anything. You should call `SetBackgroundColor()` separately: ```go package main import ( "github.com/gdamore/tcell" "github.com/rivo/tview" ) func main() { newPrimitive := func(text string) tview.Primitive { return tview.NewTextView(). SetTextAlign(tview.AlignCenter). SetText(text) } songList := newPrimitive("songs") songInfo := newPrimitive("info") grid := tview.NewGrid(). SetRows(-1, 5). SetColumns(0). SetBorders(true). AddItem(songList, 0, 0, 1, 1, 0, 0, false). AddItem(songInfo, 1, 0, 1, 1, 0, 0, false) grid.SetBackgroundColor(tcell.ColorDefault) if err := tview.NewApplication().SetRoot(grid, true).SetFocus(grid).Run(); err != nil { panic(err) } } ``` Regarding your other question, you can write to a `TextView` at any time: ```go fmt.Fprintln(textView,"Song A") ``` See also [`SetChangedFunc()`](https://godoc.org/github.com/rivo/tview#TextView.SetChangedFunc) for some information on how to deal with that. Let me know in case there's anything else that needs clarification.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#268
No description provided.