Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need example of how to set color of an individual widget/button in Gtk4.jl #56

Closed
JeffFessler opened this issue Apr 15, 2024 · 3 comments

Comments

@JeffFessler
Copy link
Contributor

The Gkt4 docs are super helpful, but right now the do not seem to mention how to set the color of an individual widget (like a button) or other style attributes like font color and font size.

The only hint I can find is the CSS example:
https://github.com/JuliaGtk/Gtk4.jl/blob/main/examples/css.jl

That example sets the button color (and font style) for the entire window, leaving open the question of how to control those attributes separately for each individual widget.

(My students need black and white keys for a piano.)

In Gtk3 it was like this:

sharp = GtkCssProvider("#wb {color:white; background:black;}" 
b = GtkButton("A♯")
push!(GAccessor.style_context(b), GtkStyleProvider(sharp), 600)

If someone (maybe @jwahlstrand?) explains how to do this in Gtk4 then I will make a PR for another example akin to css.jl to help others. Thanks!

@jwahlstrand
Copy link
Member

To style individual widgets you can set classes in the CSS file and then apply them to the widgets. This functionality is surprisingly poorly documented, I learned about it from here: https://stackoverflow.com/questions/72911291/vala-how-to-set-color-of-gtk4-label-programatically

Here's an example:

using Gtk4

# in CSS, define classes for the styles you want:
css="""
.red {
    background: red;
    color: white;
}

.blue {
    background: blue;
    color: white;
}
"""

win=GtkWindow("styling individual widgets")
cssprov=GtkCssProvider(css)
push!(Gtk4.display(win), cssprov)  # applies the CSS to all widgets

b=GtkBox(:v)
red_button = GtkButton("I'm red")
blue_button = GtkButton("I'm blue")

push!(b,red_button)
push!(b,blue_button)

win[]=b

# add the CSS classes to the widgets (you can add more than one)
add_css_class(red_button,"red")
add_css_class(blue_button,"blue")

# CSS classes can be removed with `remove_css_class`

This should be what you need. PR's of new examples and documentation are welcome!

@JeffFessler
Copy link
Contributor Author

Thank you so much! I will leave this open until I make a PR.

@JeffFessler
Copy link
Contributor Author

Closed by #57

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants