1

アプリケーションの開発には GTK と Python を使用しています。SQLite3 データベースから TreeView 要素 (1 列) を読み込みたい。しかし、何かがうまくいかない (エラーなし)! ここにコード全体があります:

#!/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()

この部分に興味があります(正常に動作していません):

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)は何も表示しませんが、正常にprint "row ", row[0] 動作し、すべての文字列を表示します。

私を助けてください。多分私はTreeViewまたはそのようなものを再描画する必要がありますか? どうすれば入手できますか?

4

1 に答える 1

1

問題はあなたのget_object方法にあります。

あなたがするとき:

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

実際に新しいウィンドウを作成しています。同じ ui ファイルを使用していても、まったく異なるウィジェットを作成しています。

ハンドラーで処理する必要があるウィジェットにアクセスする際の問題を回避する 1 つの方法は、ウィジェットをコンストラクターのパラメーターとして渡すことです。

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

これらのウィジェットは、後でハンドラーのメソッドで使用できます。

より「分離された」方法でウィジェットにアクセスする別の方法は、connectシグナルを接続するときにメソッドの最後のパラメーターとして使用するオブジェクトを追加することです。欠点は、これを手動で行う必要があることです (Glade ではこの機能が提供されていないため)。

self.widget.connect('some-signal', handler.handler_method, object_to_use)
于 2013-01-23T01:07:08.453 に答える