1

I hope someone can help me.

My Aim

  • creating a simple multi paged GTK application with python
  • pages should be switched by using a sidebar or a top bar
  • each page should be able to contain multiple elements (e.g. a few buttons, labels, ...) arranged in a grid or something

My code so far (some copy&paste from free sources and some modifications)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

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

class StackSidebar(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="application title")
        self.set_default_size(900, 600)
        self.connect("destroy", Gtk.main_quit)

        grid = Gtk.Grid()
        self.add(grid)

        stack = Gtk.Stack()
        stack.set_hexpand(True)
        stack.set_vexpand(True)
        grid.attach(stack, 1, 0, 1, 1)

        stacksidebar = Gtk.StackSidebar()
        stacksidebar.set_stack(stack)
        grid.attach(stacksidebar, 0, 0, 1, 1)

        label = Gtk.Label("label 1 text inside")
        name = "label1"
        title = "label 1 name"
        stack.add_titled(label, name, title)

        label = Gtk.Label("label 2 text inside")
        name = "label2"
        title = "label 2 name"
        stack.add_titled(label, name, title)

        label = Gtk.Label("label 3 text inside")
        name = "label3"
        title = "label 3 name"
        stack.add_titled(label, name, title)

window = StackSidebar()
window.set_wmclass ("application title", "application title")
window.show_all()

Gtk.main()

Result

Click link to see the running application

Problem

I am only able to see/create just one label within each page. See label = Gtk.Label("label 1 text inside"). As stated before, I would like to arrange a few buttons and so on but do not have any idea how to begin that.

Could you help? Is that even possible? Is my approach OK or should I use something like GtkNotebook? Thanks in advance.

  • You need to put your Labels, Buttons, etc into a layout container, eg a [Box](http://python-gtk-3-tutorial.readthedocs.io/en/latest/layout.html#boxes) or a Table. – PM 2Ring Jun 12 '17 at 23:22
  • as @PM2Ring said, you need to use container Widgets to layout your UI. Try Glade and play with the widgets to understand how to make user interfaces. – José Fonte Jun 13 '17 at 00:06
  • 1
    @PM2Ring Table is no longer applicable in GTK+ 3; use Grid instead. – andlabs Jun 13 '17 at 03:01
  • Thanks @andlabs. I haven't used GTK much lately, and I've never used GTK3... – PM 2Ring Jun 13 '17 at 03:23

1 Answers1

0

Thanks for your help. I looked at GtkGrid and it works really well.

This is my solution (snippet)

# See initial post for previous code

grid2 = Gtk.Grid()

button1 = Gtk.Button(label="Button 1")
label1 = Gtk.Label("This is a test label")
button3 = Gtk.Button(label="Button 3")
button4 = Gtk.Button(label="Button 4")
button5 = Gtk.Button(label="Button 5")
button6 = Gtk.Button(label="Button 6")

grid2.add(button1)
grid2.attach(label1, 1, 0, 2, 1)
grid2.attach_next_to(button3, button1, Gtk.PositionType.BOTTOM, 1, 2)
grid2.attach_next_to(button4, button3, Gtk.PositionType.RIGHT, 2, 1)
grid2.attach(button5, 1, 2, 1, 1)
grid2.attach_next_to(button6, button5, Gtk.PositionType.RIGHT, 1, 1)

name = "page3"
title = "page3-title"
stack.add_titled(grid2, name, title)

# See initial post for next code

Result

Click link to see running application