Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

i am trying to animate a spinner while the programm is running but i can't. I tried using threads but i can't get what i want...you can look here for the progress i made http://www.reddit.com/r/ubuntuappshowdown/comments/vvyav/problem_with_spinner/

i get the spinner to animate but the program stops running...

share|improve this question
add comment

2 Answers 2

up vote 0 down vote accepted

I saw your question yesterday on reddit as well.

When I tried to use threads in python and gtk, it took me like one night to figure it out.

In the end it was as simple as

GObject.threads_init()
Gdk.threads_init()

in the beginning and then spawn threads like you normally would in python with something like

watch_thread = Thread(target=self.function)
watch_thread.daemon = True
watch_thread.start()

I have no idea if that helps you. But I do hope so. I will also take a look at your code and might edit my answer afterwards :)

share|improve this answer
    
i merge your pull request but i didn't work... i have to run both function with thread? (one to start the spinner and the other to execute the commands) edit: it works!!! thanks very much! –  Clepto Jul 4 '12 at 15:04
add comment

Keep following 2 tips in mind when using threads and PyGTK:

  • Make sure to call GObject.threads_init() as soon as possible in your program to enable threading in your PyGTK app.

  • If you are doing any GUI tasks in your thread, wrap it inside GObject.idle_add(callable), always.

Here is a full working example, adapt to your code as needed:

import time
import threading

from gi.repository import Gtk, GObject
GObject.threads_init() # Don' forget!


class WorkerThread(threading.Thread):
    def __init__(self, callback):
        threading.Thread.__init__(self)
        self.callback = callback

    def run(self):
        # Simulate a long process, do your actual work here
        time.sleep(4)

        # The callback runs a GUI task, so wrap it!
        GObject.idle_add(self.callback)


class MyWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.connect('delete-event', Gtk.main_quit)
        self.resize(400, 400)

        vbox = Gtk.VBox()

        button = Gtk.Button("Let's spin!")
        button.connect('clicked', self.on_button_clicked)
        vbox.pack_start(button, False, False, 0)

        self.spinner = Gtk.Spinner()
        vbox.pack_start(self.spinner, True, True, 0)
        self.add(vbox)
        self.show_all()

    def on_button_clicked(self, widget):
        self.spinner.start()
        thread = WorkerThread(self.work_finished_cb)
        thread.start()

    def work_finished_cb(self):
        self.spinner.stop()

if __name__ == "__main__":
    app = MyWindow()
    Gtk.main()
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.