Gtk.Button
Gtk.Button — A widget that emits a signal when clicked on

Object Hierarchy:
GObject
╰── GInitiallyUnowned
╰── Gtk.Widget
╰── Gtk.Container
╰── Gtk.Bin
╰── Gtk.Button
├── Gtk.ToggleButton
├── Gtk.ColorButton
├── Gtk.FontButton
├── Gtk.LinkButton
├── Gtk.LockButton
├── Gtk.ModelButton
╰── Gtk.ScaleButton
Functions:
- new
() -> Gtk.Widget - new_with_label
(label:str) -> Gtk.Widget - new_with_mnemonic
(label:str) -> Gtk.Widget - new_from_icon_name
(icon_name:str, size:int) -> Gtk.Widget - new_from_stock
(stock_id:str) -> Gtk.Widget - pressed
(self) - released
(self) - clicked
(self) - enter
(self) - leave
(self) - set_relief
(self, relief:Gtk.ReliefStyle) - get_relief
(self) -> Gtk.ReliefStyle - get_label
(self) -> str - set_label
(self, label:str) - get_use_stock
(self) -> bool - set_use_stock
(self, use_stock:bool) - get_use_underline
(self) -> bool - set_use_underline
(self, use_underline:bool) - set_focus_on_click
(self, focus_on_click:bool) - get_focus_on_click
(self) -> bool - set_alignment
(self, xalign:float, yalign:float) - get_alignment
(self) -> xalign:float, yalign:float - set_image
(self, image:Gtk.Widget) - get_image
(self) -> Gtk.Widget - set_image_position
(self, position:Gtk.PositionType) - get_image_position
(self) -> Gtk.PositionType - set_always_show_image
(self, always_show:bool) - get_always_show_image
(self) -> bool - get_event_window
(self) -> Gdk.Window
Signals:
- “activate”
(widget, user_data) - “clicked”
(button, user_data) - “enter-notify-event”
(button, user_data) - “leave-notify-event”
(button, user_data) - “button-press-event”
(button, user_data) - “button-release-event”
(button, user_data)
Description:
The Gtk.Button widget is generally used to trigger a callback function that is called when the button is pressed. The various signals and how to use them are outlined below.
The Gtk.Button widget can hold any valid child widget. That is, it can hold almost any other standard Gtk.Widget. The most commonly used child is the Gtk.Label.
Function Details:
new()
new () -> Gtk.Widget
Creates a new Gtk.Button widget. To add a child widget to the button,use `Gtk.Container:add()`.
- Returns:
The newly created
Gtk.Buttonwidget.
new_with_label()
new_with_label (label:str) -> Gtk.Widget
Creates a Gtk.Button widget with a Gtk.Label child containing the giventext.
- Returns:
The newly created
Gtk.Buttonwidget.
new_with_mnemonic()
new_with_mnemonic (label:str) -> Gtk.Widget
Creates a new Gtk.Button 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 “__” (twounderscores). The first underlined character represents a keyboardaccelerator called a mnemonic.Pressing Alt and that key activates the button.
- Returns: a new Gtk.Button
new_from_icon_name()
new_from_icon_name (icon_name:str, size:int) -> Gtk.Widget
Creates a new button containing an icon from the current icon theme.
If the icon name isn’t known, a “broken image” icon will bedisplayed instead. If the current icon theme is changed, the iconwill be updated appropriately.
This function is a convenience wrapper around Gtk.Button:new() andGtk.Button:set_image().
Returns: a new
Gtk.Buttondisplaying the themed iconSince: 3.10
new_from_stock()
new_from_stock (stock_id:str) -> Gtk.Widget
Gtk.Button:new_from_stock has been deprecated since version 3.10 and should not be used in newly-written code.
Use Gtk.Button:new_with_label() instead.
Creates a new Gtk.Button containing the image and text from a stock item.Some stock ids have preprocessor macros like GTK_STOCK_OK andGTK_STOCK_APPLY.
If stock_id is unknown, then it will be treated as a mnemoniclabel (as for Gtk.Button:new_with_mnemonic()).
- Returns: a new Gtk.Button
pressed()
pressed (self)
Gtk.Button:pressed has been deprecated since version 2.20 and should not be used in newly-written code.
Use the “button-press-event” signal.
Emits a “pressed” signal to the given Gtk.Button.
released()
released (self)
Gtk.Button:released has been deprecated since version 2.20 and should not be used in newly-written code.
Use the “button-release-event” signal.
Emits a “released” signal to the given Gtk.Button.
clicked()
clicked (self)
Emits a “clicked” signal to the given Gtk.Button.
enter()
enter (self)
Gtk.Button:enter has been deprecated since version 2.20 and should not be used in newly-written code.
Use the “enter-notify-event” signal.
Emits a “enter” signal to the given Gtk.Button.
leave()
leave (self)
Gtk.Button:leave has been deprecated since version 2.20 and should not be used in newly-written code.
Use the “leave-notify-event” signal.
Emits a “leave” signal to the given Gtk.Button.
set_relief()
set_relief (self, relief:Gtk.ReliefStyle)
Sets the relief style of the edges of the given Gtk.Button widget.Two styles exist, GTK_RELIEF_NORMAL and GTK_RELIEF_NONE.The default style is, as one can guess, GTK_RELIEF_NORMAL.The deprecated value GTK_RELIEF_HALF behaves the same asGTK_RELIEF_NORMAL.
get_relief()
get_relief (self) -> Gtk.ReliefStyle
Returns the current relief style of the given Gtk.Button.
- Returns: The current Gtk.ReliefStyle
get_label()
get_label (self) -> str
Fetches the text from the label of the button, as set byGtk.Button:set_label(). If the label text has not been set the return value will be None. This will be the case if you create an empty button with Gtk.Button:new() to use as a container.
- Returns: The text of the label widget. This string is ownedby the widget and must not be modified or freed.
set_label()
set_label (self, label:str)
Sets the text of the label of the button to str. This text isalso used to select the stock item if Gtk.Button:set_use_stock()is used.
This will also clear any previously set labels.
get_use_stock()
get_use_stock (self) -> bool
Gtk.Button:get_use_stock has been deprecated since version 3.10 and should not be used in newly-written code.
Returns whether the button label is a stock item.
- Returns:
Trueif the button label is used toselect a stock item instead of beingused directly as the label text.
set_use_stock()
set_use_stock (self, use_stock:bool)
Gtk.Button:set_use_stock has been deprecated since version 3.10 and should not be used in newly-written code.
If True, the label set on the button is used as astock id to select the stock item for the button.
get_use_underline()
get_use_underline (self) -> bool
Returns whether an embedded underline in the button label indicates amnemonic. See Gtk.Button:set_use_underline().
- Returns:
Trueif an embedded underline in the button labelindicates the mnemonic accelerator keys.
set_use_underline()
set_use_underline (self, use_underline:bool)
If true, an underline in the text of the button label indicatesthe next character should be used for the mnemonic accelerator key.
set_focus_on_click()
set_focus_on_click (self, focus_on_click:bool)
Sets whether the button will grab focus when it is clicked with the mouse.Making mouse clicks not grab focus is useful in places like toolbars whereyou don’t want the keyboard focus removed from the main area of theapplication.
- Since: 2.4
get_focus_on_click()
get_focus_on_click (self) -> bool
Returns whether the button grabs focus when it is clicked with the mouse.See Gtk.Button:set_focus_on_click().
Returns:
Trueif the button grabs focus when it is clicked withthe mouse.Since: 2.4
set_alignment()
set_alignment (self, xalign:float, yalign:float)
Gtk.Button:set_alignment has been deprecated since version 3.14 and should not be used in newly-written code.
Access the child widget directly if you need to controlits alignment.
Sets the alignment of the child. This property has no effect unless the child is a Gtk.Misc or a Gtk.Alignment.
- Since: 2.4
get_alignment()
get_alignment (self) -> xalign:float, yalign:float
Gtk.Button:get_alignment has been deprecated since version 3.14 and should not be used in newly-written code.
Access the child widget directly if you need to controlits alignment.
Gets the alignment of the child in the button.
- Since: 2.4
set_image()
set_image (self, image:Gtk.Widget)
Set the image of button to the given widget. The image will bedisplayed if the label text is None or if“always-show-image” is True. You don’t have to call`Gtk.Widget:show()onimage` yourself.
- Since: 2.6
get_image()
get_image (self) -> Gtk.Widget
Gets the widget that is currenty set as the image of button.This may have been explicitly set by Gtk.Button:set_image()or constructed by Gtk.Button:new_from_stock().
Returns: a
Gtk.WidgetorNonein case there is no image.Since: 2.6
set_image_position()
set_image_position (self, position:Gtk.PositionType)
Sets the position of the image relative to the text inside the button.
- Since: 2.10
get_image_position()
get_image_position (self) -> Gtk.PositionType
Gets the position of the image relative to the text inside the button.
Returns: the position
Since: 2.10
set_always_show_image()
set_always_show_image (self, always_show:bool)
If True, the button will ignore the “gtk-button-images”setting and always show the image, if available.
Use this property if the button would be useless or hard to usewithout the image.
- Since: 3.6
get_always_show_image()
get_always_show_image (self) -> bool
Returns whether the button will ignore the “gtk-button-images”setting and always show the image, if available.
Returns:
Trueif the button will always show the imageSince: 3.6
get_event_window()
get_event_window (self) -> Gdk.Window
Returns the button’s event window if it is realized, None otherwise.This function should be rarely needed.
Returns:
button’s event window.Since: 2.22
Example:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class Button(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.set_title("Button")
self.connect("destroy", Gtk.main_quit)
button = Gtk.Button(label="Button")
button.connect("clicked", self.on_button_clicked)
self.add(button)
def on_button_clicked(self, button):
print("Button has been clicked!")
window = Button()
window.show_all()
Gtk.main()