Skip to content

Instantly share code, notes, and snippets.

@serdarsen
Last active June 23, 2018 18:31
Show Gist options
  • Save serdarsen/d1ee90cd1b48a53d4bf2bef5e72916a6 to your computer and use it in GitHub Desktop.
Save serdarsen/d1ee90cd1b48a53d4bf2bef5e72916a6 to your computer and use it in GitHub Desktop.
My pygtk ListView sample app

ListView Sample

ListView.py

#!/usr/bin/python3

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


class ListView(Gtk.ScrolledWindow):

    innerBox = None
    margins = [0, 0, 0, 0]  # top, bottom, left, right
    spacing = 3

    def __init__(self):
        Gtk.ScrolledWindow.__init__(self)
        self.set_overlay_scrolling(True)
        self.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
        self.initInnerbox()

    def initInnerbox(self):
        self.innerBox = Gtk.VBox()
        self.add(self.innerBox)
        self.innerBox.set_spacing(self.spacing)
        self.innerBox.props.valign = Gtk.Align.START

    def setMargins(self, margins):
        self.margins = margins
        self.set_margin_top(margins[0])
        self.set_margin_bottom(margins[1])
        self.set_margin_left(margins[2])
        self.set_margin_right(margins[3])

    def getMargins(self):
        return self.margins

    def setSpacing(self, spacing):
        self.spacing = spacing
        self.innerBox.set_spacing(self.spacing)

    def getSpacing(self):
        return self.spacing

    def addItem(self, row):
        print("scrollView add")
        self.innerBox.add(row)

    def clean(self):
        # cleans the innerBox
        if self.innerBox.get_children() is not None:
            for child in self.innerBox.get_children():
                self.innerBox.remove(child)

app.py

#!/usr/bin/python3

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

class MyWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="ListView Sample")
        self.set_default_size(256, 300)

        self.listView = ListView()
        self.listView.setMargins([1, 1, 1, 1])
        self.listView.setSpacing(3)
        self.add(self.listView)

        itemsList = ["list item 1", "list item 2", "list item 3", "list item 4"]

        for item in itemsList:
            listButton = Gtk.Button(item, xalign=0)
            listButton.get_style_context().add_class("flat")
            listButton.connect("button-press-event", self.listButtonOnPress)
            self.listView.addItem(listButton)
        
    def listButtonOnPress(self, btn, event):
        # Left Mouse Button OnClick
        if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 1:
            print("%s onLeftClick" % btn.get_label())

        # Right Mouse Button OnClick
        if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 3:
            print("%s onRightClick" % btn.get_label())


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

Usage

srdr@aspr:~/Desktop/my-pygtk-listview-sample$ python3 app.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment