2013-01-22 66 views
1

我使用GTK和Python开发应用程序。 我想从SQLite3数据库加载TreeView元素(1列)。 但是出问题了(没有任何错误)! 这里是整个代码:GTK TreeView与ListStore不显示任何东西。使用Python和SQLite3

#!/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或类似的东西? 你知道吗,我怎么能得到它?

+1

在继续之前,您应该放一个较小的(最小的)测试用例。在你当前的代码中,通知部分没有意义,也没有所有这些未使用的小部件(例如那些带有_clicked事件的小部件)。此外,您可以将最小的小部件放在代码中,而不是引用未提交内容的文件(“main.ui”)。 – gpoo

回答

1

问题出在您的get_object方法中。

当你这样做:

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

你实际上创建新窗口;即使您使用的是相同的ui文件,您也正在创建一个完全不同的小部件。

一种方法来解决accesing你需要用处理器来处理小工具的问题是他们通过为构造函数的参数:

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)