[GH-ISSUE #947] SetLabelColor on Button appears to be not working? #694

Open
opened 2026-03-04 01:07:05 +03:00 by kerem · 2 comments
Owner

Originally created by @tlafebre on GitHub (Feb 8, 2024).
Original GitHub issue: https://github.com/rivo/tview/issues/947

Hello,

Please consider the simplified example below.

I want to change the color of "otherButton" if "button" is pressed. Pressing the "button" doesn't change the Label color of the "otherButton".

otherButton.SetLabel will change the text of the button however... What am I missing?

package main

import (
	"log"

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

var (
	button      *tview.Button
	otherButton *tview.Button
)

func main() {
	app := tview.NewApplication()

	grid := tview.NewGrid()

	button = tview.NewButton("Ok").SetSelectedFunc(func() {
		//otherButton.SetLabel("42")
		otherButton.SetLabelColor(tcell.ColorRed)
	})

	otherButton = tview.NewButton("Other")

	grid.
		AddItem(button, 0, 0, 1, 1, 0, 0, true).
		AddItem(otherButton, 2, 0, 1, 1, 0, 0, false)

	if err := app.SetRoot(grid, true).Run(); err != nil {
		log.Fatalf("Error")
	}

	app.SetFocus(button)
}

Thanks in advance!

Originally created by @tlafebre on GitHub (Feb 8, 2024). Original GitHub issue: https://github.com/rivo/tview/issues/947 Hello, Please consider the simplified example below. I want to change the color of "otherButton" if "button" is pressed. Pressing the "button" doesn't change the Label color of the "otherButton". otherButton.SetLabel will change the text of the button however... What am I missing? ``` package main import ( "log" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) var ( button *tview.Button otherButton *tview.Button ) func main() { app := tview.NewApplication() grid := tview.NewGrid() button = tview.NewButton("Ok").SetSelectedFunc(func() { //otherButton.SetLabel("42") otherButton.SetLabelColor(tcell.ColorRed) }) otherButton = tview.NewButton("Other") grid. AddItem(button, 0, 0, 1, 1, 0, 0, true). AddItem(otherButton, 2, 0, 1, 1, 0, 0, false) if err := app.SetRoot(grid, true).Run(); err != nil { log.Fatalf("Error") } app.SetFocus(button) } ``` Thanks in advance!
Author
Owner

@rivo commented on GitHub (Mar 7, 2024):

When I'm trying your code, it does what it's supposed to do:

image

After pressing Enter otherButton is the same text ("Other") but with a red label.

So I'm not sure what you're seeing on your end. Can you run this exact code and send a screenshot?

<!-- gh-comment-id:1984090447 --> @rivo commented on GitHub (Mar 7, 2024): When I'm trying your code, it does what it's supposed to do: <img width="367" alt="image" src="https://github.com/rivo/tview/assets/480930/9fef08fb-f421-4fce-a39a-4b7e5e8388b2"> After pressing <kbd>Enter</kbd> `otherButton` is the same text ("Other") but with a red label. So I'm not sure what you're seeing on your end. Can you run this exact code and send a screenshot?
Author
Owner

@dustinmichels commented on GitHub (Jan 30, 2025):

Hello Rivo!! I love this library. I am also having trouble with buttons. Working with a tview.Button, it seems that these methods do work:

  • SetLabelColorActivated
  • SetBackgroundColorActivated

But these do not:

  • SetLabelColor
  • SetBackgroundColor
Image

Here is complete sample code:

package main

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

func main() {
	app := tview.NewApplication()

	// Create a menu page
	menuPage := makeMenuPage(app, func() {
		app.Stop()
	})

	// Set the root page
	app.SetRoot(menuPage, true)

	// Start the app
	if err := app.Run(); err != nil {
		panic(err)
	}
}

// makeMenuPage creates a menu page with arrow key navigation
func makeMenuPage(app *tview.Application, buttonAction func()) tview.Primitive {
	menu := tview.NewFlex().SetDirection(tview.FlexRow)

	// Create buttons
	buttons := []*tview.Button{
		tview.NewButton("Btn 1").SetSelectedFunc(buttonAction),
		tview.NewButton("Btn 2").SetSelectedFunc(buttonAction),
		tview.NewButton("Btn 3").SetSelectedFunc(buttonAction),
		tview.NewButton("Btn 4").SetSelectedFunc(buttonAction),
	}

	// Apply colors to buttons
	for _, btn := range buttons {
		btn.SetLabelColor(tcell.ColorWhite.TrueColor())                // Text color
		btn.SetBackgroundColor(tcell.ColorBlack.TrueColor())           // Default background color
		btn.SetLabelColorActivated(tcell.ColorRed.TrueColor())         // Text color when selected
		btn.SetBackgroundColorActivated(tcell.ColorYellow.TrueColor()) // Background when selected
	}

	// Add buttons to menu
	for _, btn := range buttons {
		menu.AddItem(btn, 1, 1, true)
	}

	// Enable arrow key navigation
	currentIndex := 0
	menu.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		switch event.Key() {
		case tcell.KeyUp:
			currentIndex = (currentIndex - 1 + len(buttons)) % len(buttons)
			app.SetFocus(buttons[currentIndex])
			return nil
		case tcell.KeyDown:
			currentIndex = (currentIndex + 1) % len(buttons)
			app.SetFocus(buttons[currentIndex])
			return nil
		}
		return event
	})

	return menu
}
<!-- gh-comment-id:2625190504 --> @dustinmichels commented on GitHub (Jan 30, 2025): Hello Rivo!! I love this library. I am also having trouble with buttons. Working with a `tview.Button`, it seems that these methods _do_ work: - `SetLabelColorActivated` - `SetBackgroundColorActivated` But these do not: - `SetLabelColor` - `SetBackgroundColor` <img width="565" alt="Image" src="https://github.com/user-attachments/assets/8940d630-12e7-4066-8f0c-15805a787e97" /> --- Here is complete sample code: ```go package main import ( "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) func main() { app := tview.NewApplication() // Create a menu page menuPage := makeMenuPage(app, func() { app.Stop() }) // Set the root page app.SetRoot(menuPage, true) // Start the app if err := app.Run(); err != nil { panic(err) } } // makeMenuPage creates a menu page with arrow key navigation func makeMenuPage(app *tview.Application, buttonAction func()) tview.Primitive { menu := tview.NewFlex().SetDirection(tview.FlexRow) // Create buttons buttons := []*tview.Button{ tview.NewButton("Btn 1").SetSelectedFunc(buttonAction), tview.NewButton("Btn 2").SetSelectedFunc(buttonAction), tview.NewButton("Btn 3").SetSelectedFunc(buttonAction), tview.NewButton("Btn 4").SetSelectedFunc(buttonAction), } // Apply colors to buttons for _, btn := range buttons { btn.SetLabelColor(tcell.ColorWhite.TrueColor()) // Text color btn.SetBackgroundColor(tcell.ColorBlack.TrueColor()) // Default background color btn.SetLabelColorActivated(tcell.ColorRed.TrueColor()) // Text color when selected btn.SetBackgroundColorActivated(tcell.ColorYellow.TrueColor()) // Background when selected } // Add buttons to menu for _, btn := range buttons { menu.AddItem(btn, 1, 1, true) } // Enable arrow key navigation currentIndex := 0 menu.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { switch event.Key() { case tcell.KeyUp: currentIndex = (currentIndex - 1 + len(buttons)) % len(buttons) app.SetFocus(buttons[currentIndex]) return nil case tcell.KeyDown: currentIndex = (currentIndex + 1) % len(buttons) app.SetFocus(buttons[currentIndex]) return nil } return event }) return menu } ```
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#694
No description provided.