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 文件,您也在创建一个完全不同的小部件。

解决访问需要使用处理程序处理的小部件问题的一种方法是将它们作为构造函数的参数传递:

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 回答