Open In App

Python – Stack and StackSwitcher in GTK+ 3

Last Updated : 31 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

A Gtk.Stack is a container which allows visibility to one of its child at a time. Gtk.Stack does not provide any direct access for users to change the visible child. Instead, the Gtk.StackSwitcher widget can be used with Gtk.Stack to obtain this functionality.
In Gtk.Stack transitions between pages can be done by means of slides or fades. This can be controlled with Gtk.Stack.set_transition_type().

The Gtk.StackSwitcher widget acts as a controller for a Gtk.Stack ; it shows a row of buttons to switch between the various pages of the associated stack widget. The content for the buttons comes from the child properties of the Gtk.Stack.

Follow below steps:

  1. import GTK+ 3 module.
  2. Create main window.
  3. Implement Stack.
  4. Implement Button.
  5. Implement Label.
  6. Implement StackSwitcher.
Example :




import gi
# Since a system can have multiple versions
# of GTK + installed, we want to make 
# sure that we are importing GTK + 3.
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
  
  
class StackWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title ="Geeks for Geeks")
        self.set_border_width(10)
  
        # Creating a box vertically oriented with a space of 100 pixel.
        vbox = Gtk.Box(orientation = Gtk.Orientation.VERTICAL, spacing = 100)
        self.add(vbox)
  
        # Creating stack, transition type and transition duration.
        stack = Gtk.Stack()
        stack.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT_RIGHT)
        stack.set_transition_duration(1000)
  
        # Creating the check button.
        checkbutton = Gtk.CheckButton("Yes")
        stack.add_titled(checkbutton, "check", "Check Button")
  
        # Creating label .
        label = Gtk.Label()
        label.set_markup("<big>Hello World</big>")
        stack.add_titled(label, "label", "Label")
  
        # Implementation of stack switcher.
        stack_switcher = Gtk.StackSwitcher()
        stack_switcher.set_stack(stack)
        vbox.pack_start(stack_switcher, True, True, 0)
        vbox.pack_start(stack, True, True, 0)
  
  
win = StackWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()


Output :



Previous Article
Next Article

Similar Reads

Python - Create window button in GTK+ 3
GTK+ 3 is a free and open-source cross-platform widget toolkit for creating graphical user interfaces (GUIs). It is licensed under the terms of the GNU Lesser General Public License. Along with Qt, it is one of the most popular toolkits for the Wayland and X11 windowing systems. Let’s see how to create a window and a button using GTK+ 3.Follow the
2 min read
Python - Create a box in GTK+ 3
Prerequisite - Python - Creating window, button in GTK+ 3 In GTK+ rather than specifying the position and size of each widget in the window, you can arrange your widgets in rows, columns, and/or tables. The size of your window is determined automatically, based on the sizes of the widgets it contains. And the sizes of the widgets are, in turn, dete
2 min read
Python - Notebook in GTK+ 3
The Gtk.Notebook widget is a Gtk.Container whose children are pages that can be switched between using tab labels along one edge. Configuration options for GtkNotebook. Gtk.Notebook.set_tab_pos() for choosing which edge the tabs should appear. Gtk.Notebook.set_scrollable() for making notebook bigger and for adding scrolling options. Gtk.Notebook.po
1 min read
Python - HeaderBar in GTK+ 3
A Gtk.HeaderBar is same as the horizontal Gtk.Box, it allows to place children at the start or the end and title to be displayed. GTK+ supports Client Side Decoration hence we can use a Gtk.HeaderBar in place of the title bar. The title will be centered with respect to the width of the box. A Gtk.HeaderBar is usually located across the top of a win
2 min read
Python - Grid container in GTK+ 3
Gtk.Grid is a container which arranges its child widgets in rows and columns, without the specification of the dimensions in the constructor. Children are added using Gtk.Grid.attach(). They can span multiple rows or columns. The Gtk.Grid.attach() method takes five parameters: child: the Gtk.Widget to add. left: the column number to attach the left
2 min read
Label in Python GTK+ 3
Labels are the main method of placing non-editable text in windows. For example, label can be used as a title placed next to entry widget. Some of the important features of the label are - The selection of labels can be done using Gtk.Label.set_selectable(). Selectable labels allow the user to copy the contents to the clipboard. Labels contain usef
2 min read
Python - ProgressBar in GTK+ 3
ProgressBar is used to display the progress of long-running operations. The Gtk.ProgressBar can be used in two different modes - Percentage modeActivity mode. When an application can determine how much work is remaining we can use Gtk.ProgressBar in percentage mode. In this mode, the application is required to call Gtk.ProgressBar.set_fraction() pe
2 min read
Python - Spinner in GTK+ 3
The Gtk.Spinner displays an icon-size spinning animation. It is often used as an alternative to a GtkProgressBar for displaying an indefinite activity. We can use Gtk.Spinner.start() to start and Gtk.Spinner.stop() to stop the animation. Example : C/C++ Code import gi gi.require_version(&quot;Gtk&quot;, &quot;3.0&quot;) from gi.repository import Gt
1 min read
Python - CellRendererText in GTK+ 3
Gtk.CellRenderer widgets are used to display information within widgets such as the Gtk.TreeView or Gtk.ComboBox. Below are the seven Gtk.CellRenderer widgets used for different purposes. Gtk.CellRendererTextGtk.CellRendererToggleGtk.CellRendererPixbufGtk.CellRendererComboGtk.CellRendererProgressGtk.CellRendererSpinnerGtk.CellRendererSpinGtk.CellRe
1 min read
Stack and Queue in Python using queue Module
A simple python List can act as queue and stack as well. Queue mechanism is used widely and for many purposes in daily life. A queue follows FIFO rule(First In First Out) and is used in programming for sorting and for many more things. Python provides Class queue as a module which has to be generally created in languages such as C/C++ and Java. 1.
3 min read
Practice Tags :