[GH-ISSUE #209] Grid and Flex RemoveItem not removing item #164

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

Originally created by @tc-turner on GitHub (Dec 21, 2018).
Original GitHub issue: https://github.com/rivo/tview/issues/209

When trying to remove an item from either a Grid or Flex using RemoveItem, nothing happens.

Am I missing something obvious?

Terminal: iTerm2 Build 3.2.6, VSC 1.30.0 built in, Terminal 2.8.2
Go: 1.11.4
tview: f93fbad8f8

package main

import (
	"fmt"

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

func main() {
	app := tview.NewApplication()
	var toplevel = tview.NewGrid()
	var nf = tview.NewButton("Button")
	nf.SetBorder(true).SetRect(0, 0, 22, 3)
	toplevel.AddItem(nf, 1, 0, 2, 2, 1, 1, true)
	app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		if event.Name() == "Ctrl+V" {
			fmt.Printf(event.Name()) // This happens
			toplevel.RemoveItem(nf)  // This doesn't
                         nf.SetLabel("Nope") // This doesn't
		}
		return event
	})
	if err := app.SetRoot(toplevel, true).SetFocus(nf).Run(); err != nil {
		panic(err)
	}
}

image

Originally created by @tc-turner on GitHub (Dec 21, 2018). Original GitHub issue: https://github.com/rivo/tview/issues/209 When trying to remove an item from either a Grid or Flex using RemoveItem, nothing happens. Am I missing something obvious? Terminal: iTerm2 Build 3.2.6, VSC 1.30.0 built in, Terminal 2.8.2 Go: 1.11.4 tview: f93fbad8f89750bcac42201907aec21d737b394f ``` package main import ( "fmt" "github.com/gdamore/tcell" "github.com/rivo/tview" ) func main() { app := tview.NewApplication() var toplevel = tview.NewGrid() var nf = tview.NewButton("Button") nf.SetBorder(true).SetRect(0, 0, 22, 3) toplevel.AddItem(nf, 1, 0, 2, 2, 1, 1, true) app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { if event.Name() == "Ctrl+V" { fmt.Printf(event.Name()) // This happens toplevel.RemoveItem(nf) // This doesn't nf.SetLabel("Nope") // This doesn't } return event }) if err := app.SetRoot(toplevel, true).SetFocus(nf).Run(); err != nil { panic(err) } } ``` ![image](https://user-images.githubusercontent.com/16192966/50325242-ad9d4b00-04b1-11e9-8c7d-0c725da23869.png)
kerem closed this issue 2026-03-04 01:02:32 +03:00
Author
Owner

@rivo commented on GitHub (Dec 26, 2018):

Your button is removed but the page is not cleared as all that remains is an empty grid with a transparent background colour. See NewGrid() for details:

Note that Box, the superclass of Grid, will have its background color set to transparent so that any grid areas not covered by any primitives will leave their background unchanged. To clear a Grid's background before any items are drawn, set it to the desired color:

grid.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor)

Here's your fixed code:

package main

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

func main() {
	app := tview.NewApplication()
	var toplevel = tview.NewGrid()
	toplevel.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor)
	var nf = tview.NewButton("Button")
	nf.SetBorder(true)
	toplevel.AddItem(nf, 1, 0, 2, 2, 1, 1, true)
	app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		if event.Name() == "Ctrl+V" {
			toplevel.RemoveItem(nf)
		}
		return event
	})
	if err := app.SetRoot(toplevel, true).Run(); err != nil {
		panic(err)
	}
}

(I also removed some calls that didn't do anything like SetRect() and SetFocus().)

<!-- gh-comment-id:450020345 --> @rivo commented on GitHub (Dec 26, 2018): Your button is removed but the page is not cleared as all that remains is an empty grid with a transparent background colour. See [`NewGrid()`](https://godoc.org/github.com/rivo/tview#NewGrid) for details: > Note that Box, the superclass of Grid, will have its background color set to transparent so that any grid areas not covered by any primitives will leave their background unchanged. To clear a Grid's background before any items are drawn, set it to the desired color: > > `grid.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor)` Here's your fixed code: ```go package main import ( "github.com/gdamore/tcell" "github.com/rivo/tview" ) func main() { app := tview.NewApplication() var toplevel = tview.NewGrid() toplevel.SetBackgroundColor(tview.Styles.PrimitiveBackgroundColor) var nf = tview.NewButton("Button") nf.SetBorder(true) toplevel.AddItem(nf, 1, 0, 2, 2, 1, 1, true) app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { if event.Name() == "Ctrl+V" { toplevel.RemoveItem(nf) } return event }) if err := app.SetRoot(toplevel, true).Run(); err != nil { panic(err) } } ``` (I also removed some calls that didn't do anything like `SetRect()` and `SetFocus()`.)
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#164
No description provided.