How to Change the Font Size of a Gtk.Label in Vala

How can I change the font size of a Gtk.Label in vala?

You could try with css, I think lately this is the preferred way. Give your label a class, then load a css. If you are going to change the font size of a label, I bet you are also going to customize other things so the css may be useful for you.

How can I change the font size in GTK?

If you want to change font overall in your app(s), I'd leave this job to gtkrc (then becomes a google question, and "gtkrc font" query brings us to this ubuntu forums link which has the following snippet of the the gtkrc file):

style "font"
{
font_name = "Corbel 8"
}
widget_class "*" style "font"
gtk-font-name = "Corbel 8"

(replace the font with the one you/user need)

Then the user will get consistent experience and will be able to change the settings easily without need for them to poke in the code and without you needing to handle the overhead of maintaining your personal configuration-related code. I understand you can make this setting more specific if you have a more precise definition for the widget_class.

YMMV for different platforms, but AFAIK this file is always present at some location if GTK is being used, and allows to the user to be in charge of presentation details.

Scale font size based on widget size in Gtk 3.0

If you set a font size in ems or %, it will base itself on the default font size. The trick is to change the default font size according to the window size. Connect to the window's configure_event, and set a new default font size when the window is resized.

Here's a simplified example:

const string WINDOW_CSS_TEMPLATE = "#mywindow { font-size: %fpx; }";
const string APPLICATION_CSS = "#mywindow .label { font-size: 5em; }";

void main(string [] args) {
Gtk.init(ref args);

var win = new Gtk.Window();
win.name = "mywindow";
win.default_width = 800;
win.default_height = 600;
var label = new Gtk.Label("Some text");
label.halign = Gtk.Align.END;
label.valign = Gtk.Align.END;
win.add(label);
win.destroy.connect(Gtk.main_quit);

var win_provider = new Gtk.CssProvider();
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
win_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);

var app_provider = new Gtk.CssProvider();
try {
app_provider.load_from_data(APPLICATION_CSS, -1);
} catch (Error e) {
Process.exit(1);
}
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
app_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);

win.configure_event.connect((event) => {
try {
// Replace this with your desired calculation of the base font size
var target_size = (double) event.height / 600.0 * 12.0;
var css = WINDOW_CSS_TEMPLATE.printf(target_size);
win_provider.load_from_data(css, -1);
} catch (Error e) {
Process.exit(1);
}
return Gdk.EVENT_PROPAGATE;
});

win.show_all();
Gtk.main();
}

This only cares about the height of the window, so you'll get an unreadably tiny label if you resize the window to be much wider than it is high, but you can plug in whatever calculation works for you.

Change font face/size of the top GTKWindow?

Yes this is possible in both GTK and Glade, although some code will be required to change all the forms font.

In GTK you will be using the GtkFontChooserWidget. This should let you choose between fonts.

In Glade, when you are using the attributes tab you are able to change font size with the Scale attribute. I haven't yet figured out how to change the actual font through those attributes yet. Thankfully there is a workaround. Instead of using Attributes, use Markup. Markup works just fine for both font size and font style.

Markup Example

<span font='36' face='Georgia'>Markup</span>

Assuming you are using a GtkComboBoxText to list possible fonts and button for selecting, you would just need a handler that would go through each of the labels and change the font to the designated one.

def when_visible(window):
"""
This is where per-window initialization takes place.
Values to be displayed are populated into their widgets.
"""
global FONT_FACE
name = Gtk.Buildable.get_name(window) # Window currently on

if 'font_select_screen' = name:
font = BUILDER.get_object('fontEntryCombo')
FONT_FACE = font.get_active_text()
elif 'random_screen' = name:
label1_text = "<span font='36' face='{}'>Label</span>".format(FONT_FACE)
label1 = BUILDER.get_object('label1')
label1.set_markup(label1_text)

Vala - How to set color of Gtk4.Label programatically?

Like NoDakker already replied, GTK re-uses CSS as a styling language instead of inventing its own.

What applications in your situation usually do is the following: they create a CSS file, e.g. "myapp.css", and load it using GtkCssProvider's API, like gtk_css_provider_load_from_resource(). Then they add it for the whole application

GdkDisplay *display = gdk_display_get_default ();
GtkCssProvider *provider = gtk_css_provider_new ();
gtk_css_provider_load_from_resource (provider, "/org/myapp/my-app.css");
gtk_style_context_add_provider_for_display (display,
GTK_STYLE_PROVIDER (provider),
GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
g_object_unref (provider);

or in Vala

var provider = new Gtk.CssProvider ();
provider.load_from_resource ("/org/myapp/my-app.css");
Gtk.StyleContext.add_provider_for_display (Gdk.Display.get_default (),
provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);

Next, in the CSS file, they usually create CSS classes. For example, a common use case similar to yours is to mark something as "bad" or "error", so you would get something like

.error {
color: red;
}

Finally, in your code, you can then easily add or remove the class from a widget when needed, with gtk_widget_add_css_class

static void
validate_input (GtkWidget *input)
{
if (input_is_valid (input))
gtk_widget_remove_css_class (input, "error");
else
gtk_widget_add_css_class (input, "error");
}

or in Vala

private void validate_input (Gtk.Widget input) {
if (input_is_valid (input))
input.remove_css_class ("error");
else
input.add_css_class ("error");
}

python GTK3 limit label width

So here is a solution (still not the exact pixel width) You can use Gtk.Widget.set_halign to force no horizontal expansion.

Here is the part of the code:

    lbl=gtk.Label(label)
lbl.set_max_width_chars(5)
lbl.set_ellipsize(pango.EllipsizeMode.END)
lbl.set_halign(gtk.Align.CENTER)
self.add(lbl)

This is what it looks like:
screeshot of the window

I hope I did not miss anything this time.



Related Topics



Leave a reply



Submit