python gtk3 controlling button size

  • Last Update :
  • Techknowledgy :

The button size should be controlled by packing HBox in VBox but my two buttons still have size depending on text:

    first = Gtk.VBox()
    second = Gtk.HBox()
    third = Gtk.VBox()
    fourth = Gtk.HBox()
    but1 = Gtk.Button(label = "any title")
    first.pack_start(second, False, False, 0)
    third.pack_start(fourth, False, False, 0)
    first.pack_start(but1, False, False, 0)
    self.data_wp = "title of label"
    self.label_data = Gtk.Label(label = self.data_wp)
    Gtk.Widget.set_size_request(but1, 85, 15)
    but2 = Gtk.Button(label = self.data_wp)
    Gtk.Container.add(but2, self.label_data)
    Gtk.Widget.set_size_request(but2, 85, 15)

Suggestion : 2

First, we create a horizontally orientated box container where 6 pixels are placed between children. This box becomes the child of the top-level window.,A Gtk.HeaderBar is similar to a horizontal Gtk.Box, it allows to place children at the start or the end. In addition, it allows a title to be displayed. The title will be centered with respect to the width of the box, even if the children at either side take up different amounts of space.,The Gtk.Notebook widget is a Gtk.Container whose children are pages that can be switched between using tab labels along one edge.,Finally, Gtk.Grid can be used like a Gtk.Box by just using Gtk.Grid.add(), which will place children next to each other in the direction determined by the “orientation” property (defaults to Gtk.Orientation.HORIZONTAL).

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
import gi

gi.require_version("Gtk", "3.0")
from gi.repository
import Gtk

class MyWindow(Gtk.Window):
   def __init__(self):
   super().__init__(title = "Hello World")

self.box = Gtk.Box(spacing = 6)
self.add(self.box)

self.button1 = Gtk.Button(label = "Hello")
self.button1.connect("clicked", self.on_button1_clicked)
self.box.pack_start(self.button1, True, True, 0)

self.button2 = Gtk.Button(label = "Goodbye")
self.button2.connect("clicked", self.on_button2_clicked)
self.box.pack_start(self.button2, True, True, 0)

def on_button1_clicked(self, widget):
   print("Hello")

def on_button2_clicked(self, widget):
   print("Goodbye")

win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
        self.box = Gtk.Box(spacing = 6)
        self.add(self.box)
        self.button1 = Gtk.Button(label = "Hello")
        self.button1.connect("clicked", self.on_button1_clicked)
        self.box.pack_start(self.button1, True, True, 0)

        self.button2 = Gtk.Button(label = "Goodbye")
        self.button2.connect("clicked", self.on_button2_clicked)
        self.box.pack_start(self.button2, True, True, 0)
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
import gi

gi.require_version("Gtk", "3.0")
from gi.repository
import Gtk

class GridWindow(Gtk.Window):
   def __init__(self):

   super().__init__(title = "Grid Example")

button1 = Gtk.Button(label = "Button 1")
button2 = Gtk.Button(label = "Button 2")
button3 = Gtk.Button(label = "Button 3")
button4 = Gtk.Button(label = "Button 4")
button5 = Gtk.Button(label = "Button 5")
button6 = Gtk.Button(label = "Button 6")

grid = Gtk.Grid()
grid.add(button1)
grid.attach(button2, 1, 0, 2, 1)
grid.attach_next_to(button3, button1, Gtk.PositionType.BOTTOM, 1, 2)
grid.attach_next_to(button4, button3, Gtk.PositionType.RIGHT, 2, 1)
grid.attach(button5, 1, 2, 1, 1)
grid.attach_next_to(button6, button5, Gtk.PositionType.RIGHT, 1, 1)

self.add(grid)

win = GridWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

Suggestion : 3

last modified January 6, 2022

1._
#include <gtk/gtk.h>

int main(int argc, char *argv[]) {
    
  GtkWidget *window;
  GtkWidget *fixed;

  GtkWidget *btn1;
  GtkWidget *btn2;
  GtkWidget *btn3;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "GtkFixed");
  gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

  fixed = gtk_fixed_new();
  gtk_container_add(GTK_CONTAINER(window), fixed);

  btn1 = gtk_button_new_with_label("Button");
  gtk_fixed_put(GTK_FIXED(fixed), btn1, 150, 50);
  gtk_widget_set_size_request(btn1, 80, 30);

  btn2 = gtk_button_new_with_label("Button");
  gtk_fixed_put(GTK_FIXED(fixed), btn2, 15, 15);
  gtk_widget_set_size_request(btn2, 80, 30);

  btn3 = gtk_button_new_with_label("Button");
  gtk_fixed_put(GTK_FIXED(fixed), btn3, 100, 100);
  gtk_widget_set_size_request(btn3, 80, 30);

  g_signal_connect(G_OBJECT(window), "destroy", 
      G_CALLBACK(gtk_main_quit), NULL);

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}

In our example, we create three buttons and place them at fixed coordinates. When we resize the window of the application, the buttons keep their size and positions.

fixed = gtk_fixed_new();

The get_fixed_new function creates a GtkFixed container.

gtk_fixed_put(GTK_FIXED(fixed), btn1, 150, 50);
5._
#include <gtk/gtk.h>

int main(int argc, char *argv[]) {
    
  GtkWidget *window;
  GtkWidget *align;

  GtkWidget *lbl;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "GtkAlignment");
  gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_container_set_border_width(GTK_CONTAINER(window), 5);

  align = gtk_alignment_new(0, 1, 0, 0);
  lbl = gtk_label_new("bottom-left");
  
  gtk_container_add(GTK_CONTAINER(align), lbl);
  gtk_container_add(GTK_CONTAINER(window), align);

  g_signal_connect(G_OBJECT(window), "destroy", 
      G_CALLBACK(gtk_main_quit), NULL);

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}

In the example, a label is positioned in the bottom-left corner of the window.

align = gtk_alignment_new(0, 1, 0, 0);

Suggestion : 4

Here's an example of using gtk_button_new to create a button with a picture and a label in it. I've broken the code to create a box up from the rest so you can use it in your programs. ,We've almost seen all there is to see of the button widget. It's pretty simple. There is however two ways to create a button. You can use the gtk_button_new_with_label() to create a button with a label, or use gtk_button_new() to create a blank button. It's then up to you to pack a label or pixmap into this new button. To do this, create a new box, and then pack your objects into this box using the usual gtk_box_pack_start, and then use gtk_container_add to pack the box into the button. ,Checking the state of the check button is identical to that of the toggle button. ,As you can imagine, these work identically to the normal button widget calls. The first creates a blank toggle button, and the second, a button with a label widget already packed into it.

GtkWidget * gtk_toggle_button_new(void);

GtkWidget * gtk_toggle_button_new_with_label(gchar * label);
void toggle_button_callback(GtkWidget * widget, gpointer data) {
   if (GTK_TOGGLE_BUTTON(widget) - > active) {
      /* If control reaches here, the toggle button is depressed. */
   }
}
void gtk_toggle_button_set_state(GtkToggleButton * toggle_button,
   gint state);
void gtk_toggle_button_toggled(GtkToggleButton * toggle_button);
GtkWidget * gtk_check_button_new(void);

GtkWidget * gtk_check_button_new_with_label(gchar * label);