How can i unlock Gtk button event

When i click on some button, the entire application stay locked waiting a result from the method, to return something..

So, i have one Gtk.Button, and i connected him to a function, for example on_button_clicked:

button = Gtk.Button()
button.connect('clicked', on_button_clicked)

the function on_button_clicked look like this:

def on_button_clicked(widget):
    func1()
    func2()
    func3()

When the functions (func1, func2, func3) is runing, the entire application stops, waiting a result from the main function (on_button_clicked). The os say 'The Application is not responding'.

Basicaly, the func1 encode one url, request that url using urllib, that request return a response that is a json file, then the func2 load that json, then make a dict with informations from json, and make an iteration in this dict printing the informations.

func1(term):
        url = 'https://api.flickr.com/services/rest/?'

        values = OrderedDict([
            ('url',url),
            ('method','flickr.photos.search'),
            ('api_key', '47a28953049fe88b32522c8997e712bb'),
            ('text', term.replace(' ', '+')),
            ('format', 'json'),
            ('nojsoncallback',1)
            ])

        url_encoded = urllib.urlencode(values)
        url_encoded = urllib.unquote(url_encoded)


        request = Request(url_encoded[4:])
        try:
            response = urlopen(request, timeout =1)
        except urllib2.URLError, e:
            print 'There was an error: %r' %e

In this time i can't click or edit no other widget.

Answers


func1(), func2() and func3() are blocking the gtk main loop. In this case, it is probably the network request. Therefore, you have to use threads.

Probably something like this:

from threading import Thread
...
def on_button_clicked(widget):
    Thread(target=func1).start()

However, you should note that you have to use glib.idle_add(), if you want to modify gtk widgets from a thread. To hide a widget from a Thread for example, you would do glib.idle_add(widget.set_visible, False).


Need Your Help

Andengine Configuration Error For Eclipse

eclipse configuration andengine

I'm trying to configure andengine on eclipse. there are some warnings in andengine default folder in src folder. I didn't change anything. I build it with api 15.

Can't get mouse callback to work with java swing

java swing matlab

I would like to get eventData when the mouse is clicked or dragged along a slider. I have tried suggestions across blog posts (http://undocumentedmatlab.com/blog/uicontrol-callbacks) and the