Gtk.Button¶
class — extends Widget, Accessible, Actionable, Buildable, ConstraintTarget
Calls a callback function when the button is clicked.
<picture> <source srcset="button-dark.png" media="(prefers-color-scheme: dark)"> <img alt="An example GtkButton" src="button.png"> </picture>
The GtkButton widget can hold any valid child widget. That is, it can hold
almost any other standard GtkWidget. The most commonly used child is the
GtkLabel.
Shortcuts and Gestures¶
The following signals have default keybindings:
CSS nodes¶
GtkButton has a single CSS node with name button. The node will get the
style classes .image-button or .text-button, if the content is just an
image or label, respectively. It may also receive the .flat style class.
When activating a button via the keyboard, the button will temporarily
gain the .keyboard-activating style class.
Other style classes that are commonly used with GtkButton include
.suggested-action and .destructive-action. In special cases, buttons
can be made round by adding the .circular style class.
Button-like widgets like ToggleButton, MenuButton,
VolumeButton, LockButton, ColorButton
or FontButton use style classes such as .toggle, .popup, .scale,
.lock, .color on the button node to differentiate themselves from a plain
GtkButton.
Accessibility¶
GtkButton uses the AccessibleRole.button role.
Constructors¶
new¶
Creates a new GtkButton widget.
To add a child widget to the button, use Button.set_child.
new_from_icon_name¶
Creates a new button containing an icon from the current icon theme.
If the icon name isn’t known, a “broken image” icon will be displayed instead. If the current icon theme is changed, the icon will be updated appropriately.
Parameters:
icon_name— an icon name
new_with_label¶
Creates a GtkButton widget with a GtkLabel child.
Parameters:
label— The text you want theGtkLabelto hold
new_with_mnemonic¶
Creates a new GtkButton containing a label.
If characters in label are preceded by an underscore, they are underlined.
If you need a literal underscore character in a label, use “__” (two
underscores). The first underlined character represents a keyboard
accelerator called a mnemonic. Pressing <kbd>Alt</kbd> and that key
activates the button.
Parameters:
label— The text of the button, with an underscore in front of the mnemonic character
Methods¶
get_can_shrink¶
Retrieves whether the button can be smaller than the natural size of its contents.
get_child¶
Gets the child widget of button.
get_has_frame¶
Returns whether the button has a frame.
get_icon_name¶
Returns the icon name of the button.
If the icon name has not been set with Button.set_icon_name
the return value will be None. This will be the case if you create
an empty button with Button.new to use as a container.
get_label¶
Fetches the text from the label of the button.
If the label text has not been set with Button.set_label
the return value will be None. This will be the case if you create
an empty button with Button.new to use as a container.
get_use_underline¶
gets whether underlines are interpreted as mnemonics.
set_can_shrink¶
Sets whether the button size can be smaller than the natural size of its contents.
For text buttons, setting can_shrink to true will ellipsize the label.
For icons and custom children, this function has no effect.
Parameters:
can_shrink— whether the button can shrink
set_child¶
Sets the child widget of button.
Note that by using this API, you take full responsibility for setting
up the proper accessibility label and description information for button.
Most likely, you'll either set the accessibility label or description
for button explicitly, or you'll set a labelled-by or described-by
relations from child to button.
Parameters:
child— the child widget
set_has_frame¶
Sets the style of the button.
Buttons can have a flat appearance or have a frame drawn around them.
Parameters:
has_frame— whether the button should have a visible frame
set_icon_name¶
Adds a GtkImage with the given icon name as a child.
If button already contains a child widget, that child widget will
be removed and replaced with the image.
Parameters:
icon_name— An icon name
set_label¶
Sets the text of the label of the button to label.
This will also clear any previously set labels.
Parameters:
label— a string
set_use_underline¶
Sets whether to use underlines as mnemonics.
If true, an underline in the text of the button label indicates the next character should be used for the mnemonic accelerator key.
Parameters:
use_underline—Trueif underlines in the text indicate mnemonics
Virtual methods¶
do_activate¶
Signal that causes the button to animate press then
release. Applications should never connect to this signal, but use
the clicked signal.
do_clicked¶
Signal emitted when the button has been activated (pressed and released).
Properties¶
can_shrink¶
Whether the size of the button can be made smaller than the natural size of its contents.
For text buttons, setting this property will allow ellipsizing the label.
If the contents of a button are an icon or a custom widget, setting this property has no effect.
child¶
The child widget.
has_frame¶
Whether the button has a frame.
icon_name¶
The name of the icon used to automatically populate the button.
label¶
Text of the label inside the button, if the button contains a label widget.
use_underline¶
If set, an underline in the text indicates that the following character is to be used as mnemonic.
Signals¶
activate¶
Emitted to animate press then release.
This is an action signal. Applications should never connect
to this signal, but use the Button.clicked signal.
The default bindings for this signal are all forms of the <kbd>␣</kbd> and <kbd>Enter</kbd> keys.
clicked¶
Emitted when the button has been activated (pressed and released).