简体   繁体   English

更改gtk.ListStore后,gtk.TreeView不会更改:

[英]gtk.TreeView does not change after changing its gtk.ListStore:

Used: python 2.79, gtk 2.0 二手:python 2.79,gtk 2.0

Dear python and PyGtk-users 尊敬的python和PyGtk用户

In my program I want to give the opportunity to show searchresults of a great datafile in a gtk.TreeView using the method in the code. 在我的程序中,我想提供使用代码中的方法在gtk.TreeView中显示出色数据文件的搜索结果的机会。 In the function that creates the ListStore I also create the csv-file the store is using. 在创建ListStore的函数中,我还创建了商店正在使用的csv文件。 Now I can see, that the csv-file has changed after a new search with different keywords, but the TreeView does not, it still keeps the result of the first search, though I clear the store everytime self.search is running. 现在我可以看到,使用不同的关键字进行新搜索后,csv文件已更改,但是TreeView却没有,尽管我每次self.search运行时都会清除存储,但它仍然保留第一次搜索的结果。 It's really enerving if you need to restart the program for every new search... 如果您需要为每个新搜索重新启动程序,那确实很令人兴奋。

I already tried some things, like creating a proper function for the datacreatin for the store, but nothing serves and the truth is that I don't have a lot of ideas... 我已经尝试过一些事情,例如为商店的datacreatin创建适当的功能,但是没有任何作用,事实是我没有太多的主意...

And everything I found in the internet about this was about autorefreshing of the store with a timer or other topics more profound, seems like everyone instead of me knows how to do this... 我在互联网上发现的与此有关的所有内容都是关于使用计时器或其他更深刻的主题自动刷新商店的,似乎每个人(而不是我)都知道该怎么做...

Can anyone tell me, what mistake I am making? 谁能告诉我,我犯了什么错误? How do I refresh a TreeView?(as you can see self.search is called by a button...) here the relevant part of the code: 我如何刷新TreeView?(如您所见,self.search由按钮调用...),这里是代码的相关部分:

import gtk, csv

class BASE:

    #...
    #some other funtions, irrelevant for the problem
    #...

    def search(self, widget, event, data=None):

        #...
        #all the searchingstuff and the creation of 'inwrite.csv'
        #...


        with open('/home/emil/Documents/inwrite.csv', 'r') as storefile:
            lines = storefile.readlines()


        store = gtk.ListStore(str, str, str, str, str, str)

        store.clear()       

        i=0

        while i < len(lines):
            line = [lines[i]]
            csvfile = csv.reader(line, delimiter=',')
            for row in csvfile:
                temp = (row[0], row[1], row[2], row[3], row[4], row[5])
            store.append(temp)
            i = i + 1



        self.tabview = gtk.TreeView(store)
        self.tabview.set_rules_hint(True)
        self.tabview.set_reorderable(True)
        self.sw.add(self.tabview)
        self.tabview.show()

        self.create_columns(self.tabview)

    def __init__(self):

        #...
        #creating the Window, entries,...
        #...

        self.button1 = gtk.Button('Buscar')
        self.button1.connect('clicked', self.search, 'button 1')

        #...
        #packing and showing the whole GUI
        #...

    def create_columns(self, tabview):

        rendererText = gtk.CellRendererText()
        rendererText.props.wrap_width = 80
        rendererText.props.wrap_mode = gtk.WRAP_WORD
        column = gtk.TreeViewColumn('Codigo', rendererText, text=0)
        column.set_sort_column_id(0)    
        self.tabview.append_column(column)

        #...
        #creating 5 more columns by the same principle
        #...

    def main(self):
        gtk.main()


if __name__=='__main__':
    base = BASE()
    run = base
    run.main()

thanks for your help, tell me if you need more information! 感谢您的帮助,告诉我是否需要更多信息!

I do not know what is exactly wrong with your program (if you want to know that, you should provide a working minimal example). 我不知道您的程序到底有什么问题(如果您想知道,您应该提供一个可行的最小示例)。

Nevertheless, you do not need to manually update the TreeView because the TreeView always shows the content of the ListStore or TreeStore. 但是,您不需要手动更新TreeView,因为TreeView总是显示ListStore或TreeStore的内容。 You also do not have to create the ListStore everytime. 您也不必每次都创建ListStore。 You can create it one time and clear and insert the new entries when the event is emitted. 您可以一次创建它,并在发出事件时清除并插入新条目。

Here is a small example that shows how it works: 这是一个显示其工作原理的小示例:

#from gi.repository import Gtk  # uncomment to use PyGObject
import gtk as Gtk

class TestCase:
    def __init__(self):
        win = Gtk.Window()
        button = Gtk.Button('Show')
        button.connect('clicked', self.on_button_clicked)

        self.clicked = 0
        self.liststore = Gtk.ListStore(int)
        self.liststore.append((self.clicked,))
        view = Gtk.TreeView(self.liststore)
        renderer = Gtk.CellRendererText()
        column = Gtk.TreeViewColumn('demo', renderer, text=0)
        view.append_column(column)

        box = Gtk.HBox()
        box.pack_start(button, True, True, 0)
        box.pack_start(view, True, True, 0)
        win.add(box)
        win.connect('delete-event', Gtk.main_quit)
        win.show_all()

    def on_button_clicked(self, button):
        self.clicked += 1
        self.liststore.clear()
        self.liststore.append((self.clicked,))


if __name__ == "__main__":
    TestCase()
    Gtk.main()

It also seems that you first create your csv file and read it afterwards. 看来您首先创建了csv文件,然后再阅读。 I don't know about the rest of your program but it is probably better to use the data directly and insert it into the ListStore and not read it from the csv file. 我对程序的其余部分一无所知,但是直接使用数据并将其插入ListStore而不是从csv文件中读取数据可能更好。 It might be even possible then to just remove and insert the new entries and not clear the whole ListStore and insert everything again. 甚至有可能只是删除并插入新条目,而不清除整个ListStore并再次插入所有内容。 That would be more efficient with bigger amounts of data. 数据量更大时,效率会更高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM