Python Forum
[PyGTK] Creating Book Table - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [PyGTK] Creating Book Table (/thread-30595.html)



[PyGTK] Creating Book Table - ramazanemreosmanoglu - Oct-27-2020

Hello;

I writed this codes for creating book table:

import os
import gi

def get_all_books():
    """
    Structure:

        (
            [isbn, name, author, page_number],
            [isbn, name, author, page_number],
            [isbn, name, author, page_number],
            ...
        )
    """
    
    return (
        ["039103948", "Name", "John Doe", "923"],
        ["039103948", "Name", "John Doe", "923"],
        ["039103948", "Name", "John Doe", "923"],
        ["039103948", "Name", "John Doe", "923"],

    )

UI_FILE = os.path.join(os.getcwd(), "window.ui")

gi.require_version("Gtk", "3.0")

from gi.repository import Gtk

class SignalHandler:
    def selection_changed(self, selection):
        ...

def create_treeview():
    store = Gtk.ListStore(int, str, str, int)
    books = get_all_books()

    map(lambda b: store.append(b), books) # Add books to store

    tree = Gtk.TreeView(store)

    isbn_col   = Gtk.TreeViewColumn("ISBN")
    name_col   = Gtk.TreeViewColumn("Name")
    author_col = Gtk.TreeViewColumn("Author")
    page_n_col = Gtk.TreeViewColumn("P. Number")

    isbn_r, name_r, author_r, page_n_r = Gtk.CellRendererText(), Gtk.CellRendererText(), Gtk.CellRendererText(), Gtk.CellRendererText()   

    l = [
        (isbn_col, isbn_r),
        (name_col, name_r),
        (author_col, author_r),
        (page_n_col, page_n_r),
    ]

    for i in l:
        i[0].pack_start(i[1], True)
        i[0].add_attribute(i[1], "text", l.index(i))
    
    map(
        lambda i: tree.append_column(i[0]),
        l,
    )

    select = tree.get_selection()
    select.connect("changed", SignalHandler().selection_changed)

    return tree

builder = Gtk.Builder()
builder.add_from_file(UI_FILE)
builder.connect_signals(SignalHandler())

if __name__ == "__main__":
    window = builder.get_object("main_window")
    box = builder.get_object("all_books_box")
    box.pack_end(create_treeview(), True, True, 0)

    window.show_all()

    Gtk.main()
When i run this codes, opening a blank window.

Console outputs:
~/LibraryManagement ยป python3 src/main.py                                                                        emre@emre-mint
src/main.py:71: PyGTKDeprecationWarning: Using positional arguments with the GObject constructor has been deprecated. Please specify keyword(s) for "model" or use a class specific constructor. See: https://wiki.gnome.org/PyGObject/InitializerDeprecations
  tree = Gtk.TreeView(store)
Why it doesn't working?


RE: [PyGTK] Creating Book Table - Larz60+ - Oct-27-2020

Quote:If you want, all of my codes is ...
Please post all of your code needed to solve here, we don't like to follow links.


RE: [PyGTK] Creating Book Table - ramazanemreosmanoglu - Oct-27-2020

(Oct-27-2020, 04:34 PM)Larz60+ Wrote: Please post all of your code needed to solve here

I edited my post.


RE: [PyGTK] Creating Book Table - Axel_Erfurt - Oct-27-2020

window.ui is mising