1

I use GTK and Python for developing an application. I want to load TreeView elements (1 column) from SQLite3 database. But something go wrong (without any error)! Here is a whole code:

#!/usr/bin/python
import sys
import sqlite3 as sqlite
from gi.repository import Gtk
from gi.repository import Notify

def notify(notifer, text, notificationtype=""):
    Notify.init("Application")
    notification = Notify.Notification.new (notifer, text, notificationtype)
    notification.show ()

def get_object(gtkname):
    builder = Gtk.Builder()
    builder.add_from_file("main.ui")
    return builder.get_object(gtkname)

def base_connect(basefile):
    return sqlite.connect(basefile)

class Handler:

    def main_destroy(self, *args):
        Gtk.main_quit(*args)

    def hardwaretool_clicked(self, widget):
        baselist = get_object("subjectlist")
        baselist.clear()
        base = base_connect("subjectbase")
        with base:
            cur = base.cursor()
            cur.execute("SELECT * FROM sub")
            while True:
                row = cur.fetchone()
                if row == None:
                    break
                iter = baselist.append()
                print "row ", row[0]
                baselist.set(iter, 0, row[0])
            cur.close()

    def gamestool_clicked(self, widget):
        print("gamestool clicked!!!!! =)")

    def appstool_clicked(self, widget):
        print("appstool clicked!!!!! =)")

    def fixtool_clicked(self, widget):
        notify("Charmix","Fix Applied", "dialog-ok")

    def brokenfixtool_clicked(self, widget):
        notify("Charmix","Broken Fix Report Sended", "dialog-error")

    def sendfixtool_clicked(self, widget):
        notify("Charmix","Fix Sended", "dialog-information")

class CharmixMain:

    def __init__(self):

        builder = Gtk.Builder()
        builder.add_from_file("main.ui")

        self.window = builder.get_object("main")

        self.subject = builder.get_object("subjectlist")
        self.problem = builder.get_object("problemlist")

        self.toolbar = builder.get_object("toolbar")
        self.hardwaretool = builder.get_object("hardwaretool")
        self.gamestool = builder.get_object("gamestool")
        self.appstool = builder.get_object("appstool")
        self.fixtool = builder.get_object("fixtool")
        self.brokenfixtool = builder.get_object("brokenfixtool")
        self.sendfixtool = builder.get_object("sendfixtool")

        builder.connect_signals(Handler())

        context = self.toolbar.get_style_context()
        context.add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)

if __name__ == "__main__":
    Charmix = CharmixMain()
    Charmix.window.show()
    Gtk.main()

I'm interested in this part (not working normally):

def hardwaretool_clicked(self, widget):
        baselist = get_object("subjectlist")
        baselist.clear()
        base = base_connect("subjectbase")
        with base:
            cur = base.cursor()
            cur.execute("SELECT * FROM sub")
            while True:
                row = cur.fetchone()
                if row == None:
                    break
                iter = baselist.append()
                print "row ", row[0]
                baselist.set(iter, 0, row[0])
            cur.close()

TreeView(subjecttree) don't display anything, but print "row ", row[0] works fine and display all the strings.

Please, help me. Maybe i need to repaint TreeView or thomething like that? Do you know, how can I get it?

gpoo
  • 8,408
  • 3
  • 38
  • 53
Bogdan
  • 11
  • 2
  • 1
    Before going any further, you should put a smaller (minimal) test case. In your current code, the notification part does not make sense, neither all those unused widgets (e.g. those with event _clicked). Also, you could put the minimal widgets in code instead of making a reference to a file whose content you did not submit ("main.ui"). – gpoo Jan 23 '13 at 00:57

1 Answers1

1

The problem is in your get_object method.

When you do:

builder = Gtk.Builder()
builder.add_from_file("main.ui")

you're actually creating a new window; even if you are using the same ui file, you are creating a completely different widget.

One way to get around the problem of accesing the widgets you need to process with your handler is to pass them as parameter of the constructor:

class Handler(object):
    def __init__(self, widget1, widget2):
        self.widget1 = widget1
        self.widget2 = widget2
...

You can use those widgets on the handler's method afterwards.

Another way of accesing the widgets in a more 'decoupled' way is to add the object you want to use as the last parameter of the connect method when you're connecting signals; the drawback is that you would have to do this manually (since Glade doesn't provide this posibility)

self.widget.connect('some-signal', handler.handler_method, object_to_use)
asermax
  • 3,053
  • 2
  • 23
  • 28