I have a simple Python/GTK app. I have a function that does stuff and until said function is done doing stuff, the GtkSpinner should be spinning.
Here is my code:
import time
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class Handler:
def on_main_window_destroy(self, *args):
Gtk.main_quit()
def on_button_clicked(self, *args):
print("button clicked")
self.dostuff()
def dostuff(self):
app.spinner.start()
print("I'm doing stuff")
time.sleep(3)
print("I'm done")
app.spinner.stop()
class MainApp():
def __init__(self):
self.builder = Gtk.Builder()
self.builder.add_from_file("test.glade")
self.builder.connect_signals(Handler())
self.main_window = self.builder.get_object("main_window")
self.main_window.show()
self.spinner = self.builder.get_object("spinner")
def main(self):
Gtk.main()
if __name__ == "__main__":
app = MainApp()
app.main()
The time.sleep() command is not the problem. If I replace it with actual "work" the same thing happens. The Spinner is not started/activated.
Why is that? Every command is handled line-by-line. So how can anything be blocking? If I replace the Spinner-code by simple print() statements, they are being outputted just as you'd expect it. I don't have a CS degree and I'm just a hobbyist. Is there some concept I'm not getting?
What do I need to change so that my Spinner starts before the task is started and stops after the task has been finished?
Thanks so much in advance!!
EDIT: This is my Glade file: https://pastebin.com/UDUinH1d
I figured it out myself. For some reason (which I do not quite understand) the GTK GUI is unresponsive even though the Python script should be executed line by line. For some reason this doesn't seem to be the case with PyGObject. So the only solution was to just thread it, which now looks like this:
t = threading.Thread(target=dostuff, args=(optional, arguments, go, here))
app.spinner.start()
t.start()