How to set the gtk+ window to the center of screen?

c gtk
11,018

Solution 1

To tell your application to position in the center, the following should work. I am however not familiar with the function you mention, and perhaps it is as good as this way of doing it. You can use the gtk_window_move() to move your window, the coordinates is by default calculated by default from the upper left corner, it is however possible to change this with:

void gtk_window_set_gravity (GtkWindow *window,
                             GdkGravity gravity);

The available enums is as follows:

GDK_GRAVITY_NORTH_WEST, GDK_GRAVITY_NORTH, GDK_GRAVITY_NORTH_EAST,
GDK_GRAVITY_WEST,GDK_GRAVITY_CENTER, GDK_GRAVITY_EAST, 
GDK_GRAVITY_SOUTH_WEST, GDK_GRAVITY_SOUTH, GDK_GRAVITY_SOUTH_EAST, 
and GDK_GRAVITY_STATIC.

And then use

void gtk_window_move (GtkWindow *window,
                      gint x,
                      gint y);

It is not certain it will change the initial window position, it is up to the Windowmanager to comply or not, as the manual for gtk tells the reader, most window managers ignore initial window positions.

This link will give you a lot of information regarding the function above https://developer.gnome.org/gtk3/3.5/GtkWindow.html#gtk-window-move

Solution 2

you must use gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS) :)

Solution 3

I know this is a "c" tagged question but if someone look for Python code, here it is:

if win is your main window

win.set_position(Gtk.WindowPosition.CENTER_ALWAYS)

Share:
11,018
richard
Author by

richard

Updated on July 27, 2022

Comments

  • richard
    richard over 1 year

    I've created a new window and want to move it to the center of the screen, and how am I supposed to do it? I've tried below

    gtk_widget_hide (GTK_WIDGET (window));
    gtk_window_set_position (window, GTK_WIN_POS_CENTER);
    gtk_widget_show_all (GTK_WIDGET (window));
    

    but it seems that the gtk_window_set_position (window, GTK_WIN_POS_CENTER) is only triggered once. Is there something I did wrong?