I have a bunch of GTK buttons in a GTK Flowbox, and am using the CSS border-radius property to make them round and background-image for the background. Unfortunately, they then appear as ovals, as in picrel below. What can I do so that the button would always have height equal to width, thus maintaining the shape of a perfect circle?
A MRE as requested: C file:
#include <gtk/gtk.h>
#define WINDOW_HEIGHT 600
GtkWidget * stop;
int num_stops;
static void stop_toggle (GtkToggleButton *source, gpointer user_data) {
//have to use set child here
if (gtk_toggle_button_get_active(source)) {
gtk_widget_set_name(GTK_WIDGET(source), "stop_button_pressed");
}
else {
gtk_widget_set_name(GTK_WIDGET(source), "stop_button");
}
}
static void make_toggles (GtkWidget * window) {
GtkWidget * stop_flow;
short i;
stop_flow = gtk_flow_box_new();
//initialise stops on this panel
char label[7]; label[0] = '\0';
for (i = 0; i < num_stops; i++) {
sprintf(label, "%d", i);
stop = gtk_toggle_button_new_with_label(label);
gtk_widget_set_name(stop, "stop_button");
gtk_flow_box_append(GTK_FLOW_BOX(stop_flow), stop);
g_signal_connect(stop, "toggled", G_CALLBACK(stop_toggle), NULL);
}
gtk_flow_box_set_column_spacing(GTK_FLOW_BOX(stop_flow), 15);
gtk_flow_box_set_row_spacing(GTK_FLOW_BOX(stop_flow), 10);
gtk_flow_box_set_selection_mode(GTK_FLOW_BOX(stop_flow), GTK_SELECTION_NONE);
gtk_widget_set_name(stop_flow, "window_main");
gtk_window_set_child (GTK_WINDOW (window), stop_flow);
}
static void app_startup(GtkApplication* app) {
//use this to figure out number of stops and pistons from the LCS app
num_stops = 16;
}
static void activate (GtkApplication* app, gpointer user_data) {
GtkWidget *window;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Left Panel");
gtk_window_set_default_size (GTK_WINDOW (window), 800, WINDOW_HEIGHT);
make_toggles(window);
GtkCssProvider * cssProvider = gtk_css_provider_new();
gtk_css_provider_load_from_path(cssProvider, "/your_location/GTK/styling2.css");
gtk_style_context_add_provider_for_display(gtk_widget_get_display(window), GTK_STYLE_PROVIDER(cssProvider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
gtk_widget_show (window);
}
int main(int argc, char **argv) {
GtkApplication *app;
int status;
app = gtk_application_new("lcs.console", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect(app, "startup", G_CALLBACK(app_startup), NULL);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
With Css file:
w_main {
background-color: grey;
}
#stop_button {
background-color: white;
background-size: 100% 100%;
background-position: center center;
border-radius: 50%;
color: black;
font-weight: bold;
font-size: xx-large;
}
#stop_button_pressed {
background-color: yellow;
background-size: 100% 100%;
background-position: center center;
border-radius: 50%;
color: black;
font-weight: bold;
font-size: xx-large;
}
And you may compile the CSS file on linux via
gcc -Wall pkg-config --cflags gtk4 file.c pkg-config --libs gtk4 .


.cfile that compiles cleanly and demonstrates the problem [rather than background images, just set solid colors]. We might like to download, compile, and run your code. Also, what is the window geometry? And, what is the screen geometry? (i.e.) We'd like to know the screen aspect ration and, possibly, the pixel aspect ratio. As a rule, whenever a circle comes out as an oval, it's related to aspect ratio.