简体   繁体   中英

gtk.TreeView does not change after changing its gtk.ListStore:

Used: python 2.79, gtk 2.0

Dear python and PyGtk-users

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. In the function that creates the ListStore I also create the csv-file the store is using. 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. 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...

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:

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. You also do not have to create the ListStore everytime. 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. 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. It might be even possible then to just remove and insert the new entries and not clear the whole ListStore and insert everything again. That would be more efficient with bigger amounts of data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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