Skip to content

Instantly share code, notes, and snippets.

@serdarsen
Last active June 23, 2018 18:31
Show Gist options
  • Select an option

  • Save serdarsen/d1ee90cd1b48a53d4bf2bef5e72916a6 to your computer and use it in GitHub Desktop.

Select an option

Save serdarsen/d1ee90cd1b48a53d4bf2bef5e72916a6 to your computer and use it in GitHub Desktop.

Revisions

  1. Serdar ŞEN revised this gist May 25, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions my-pygtk-listview-sample.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ ListView Sample

    #### ListView.py
    ```py
    #!/usr/bin/env python3
    #!/usr/bin/python3

    import gi.repository
    gi.require_version('Gtk', '3.0')
    @@ -62,7 +62,7 @@ class ListView(Gtk.ScrolledWindow):

    #### app.py
    ```py
    #!/usr/bin/env python3
    #!/usr/bin/python3

    import gi
    gi.require_version('Gtk', '3.0')
  2. Serdar ŞEN revised this gist May 24, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion my-pygtk-listview-sample.md
    Original file line number Diff line number Diff line change
    @@ -96,7 +96,7 @@ class MyWindow(Gtk.Window):

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


    win = MyWindow()
  3. Serdar ŞEN revised this gist May 24, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion my-pygtk-listview-sample.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    ListView Sample
    ===

    <img src="screenshot1.gif" width="286" height="352"/>
    <img src="https://gist.github.com/serdarsen/d1ee90cd1b48a53d4bf2bef5e72916a6/raw/f42ad974e6aad862a7d635cbb28406d34a533633/screenshot1.gif" width="286" height="352"/>

    #### ListView.py
    ```py
  4. srdr revised this gist May 24, 2018. 1 changed file with 0 additions and 0 deletions.
    Binary file added screenshot1.gif
    Loading
    Sorry, something went wrong. Reload?
    Sorry, we cannot display this file.
    Sorry, this file is invalid so it cannot be displayed.
  5. Serdar ŞEN created this gist May 24, 2018.
    112 changes: 112 additions & 0 deletions my-pygtk-listview-sample.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,112 @@

    ListView Sample
    ===

    <img src="screenshot1.gif" width="286" height="352"/>

    #### ListView.py
    ```py
    #!/usr/bin/env 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
    ```py
    #!/usr/bin/env 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 onRigintClick" % btn.get_label())


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

    Usage
    ===
    ```sh
    srdr@aspr:~/Desktop/my-pygtk-listview-sample$ python3 app.py
    ```