I'm kinda' new in Python, I'm learning it by myself and all I have to say is that it's a wonderful programming language. Even if I'm learning it in the straight way, for now I know (as a beginner) how to work with GTK, threads, urllib and other modules.
But I need an answer from experienced PyGTK users. Take a look to this little example in the image:
http://i.stack.imgur.com/y10nv.png
In this concept application (it doesn't have a working code) when the user will select a row from Gtk.TreeView I will show some row-specific data taken from the web (URL differ for each row). So when the user click on a row, a thread is started (to not froze the GUI) getting the data from an URL. When data was succesfully fetched, a callback is called displaying the content in the Gtk.Label placed under the list.
The problem is that if the user quickly selects some different rows, well... there will be a little mess because some requests may take longer than others and there will be a "desynchronization" between the selected row and the data displayer in Gtk.Label. I know that a running thread can't be stopped from outside (even that I found a thread module that make this possible), so what's the best way to prevent the Gtk.Label being desync开发者_JS百科hronized with the current selection?
I'm sorry for my bad english and for my silly question. Currently I managed to fix this by comparing the text in the selected row with what I expect to get from the requested URL, but I think there is a better way to manage this.
Thank you very much, Ovidiu Nitan
It sounds like you're already doing the obvious thing. In your callback function, simply check that the URL it came from matches the row that's selected at present, and if it doesn't, ignore it.
Only the thread running the mainloop should update the GUI, so your download worker has to communicate back anyway, for example using idle_add. This allows you to pass a token, like a simple counter or the selected TreeIter, when starting the thread and compare any results that come back to the last one you sent off. Only on match do you set the label.
精彩评论