0

in my application i'm trying to change the label text when i get new data from an api. My problem is after some time my gui hangs up. And its not always at the same time that its hanging up somtimes its right after start running the aplication sometimes its after it has run for half an hour.

And its only the gui thats hangs up, the program itself keeps running and can still give output on the console.

The updating of the label text runs in an extra thread.

class GUI():
def get_assets(self,trader):
    assets={}
    if trader=="kraken":
        pass
    elif trader=="cex":
        query=info_CEX()
        res=query.get_balance()
        for key in res:
            try:
                assets[key]={}
                assets[key]["available"]=res[key]["available"]
                assets[key]["orders"]=res[key]["orders"]  
            except(KeyError,TypeError):
                continue
        return assets

def update_asset_labels(self,trader):
    assets=self.get_assets(trader)
    for key in assets:
        try:
            label=self.builder.get_object("BX"+key)
            label.set_text(str(float(assets[key]["available"]))+"/"+str(float(assets[key]["orders"])))
        except(AttributeError):
            if key=="EUR":
                label=self.builder.get_object("kapital")
                label.set_text(str(float(assets[key]["available"])))
            continue





def app(self):
    """Initialisiert die graphischen Oberfläche.
    """ 
    UI_FILE = PFAD+"/PYtraderGUI.ui"
    self.builder = Gtk.Builder()
    self.builder.add_from_file(UI_FILE)
    self.builder.connect_signals(self)
    self.window = self.builder.get_object("window1")
    """self.window.maximize()"""
    self.window.show_all()


    def update_info():
        price=["None"] * 11
        oldprice=["None"] * 11
        chan=0
        start_price=0
        query=infokraken.infokraken()
        trader="cex"
        while True:
            try:
                self.update_asset_labels(trader)
                pass
            except (ValueError):
                continue

    thread1 = threading.Thread(target=update_info)
    thread1.daemon = True
    thread1.start()

if __name__ == "__main__":
    GObject.threads_init()
    GO=GUI()
    GO.connect("delete-event", Gtk.main_quit)
    GO.show_all()
    GO.app()
    Gtk.main()
user2468067
  • 11
  • 1
  • 3

1 Answers1

0

I'd recommend reading on GTK+ and it's working with threads. IIRC (it's been a while) you shouldn't make changes directly to the GUI from a thread. Instead you should do it through GLib's idle_add(callable) function. This function should schedule making the changes to a time when the GUI is idle.

Take a look at this example from the PyGObject User Guide concerning Threads and Concurrency. Also, read the Threads: FAQ section at the bottom of the section.

Another way of doing this would be using Queues or Pipes to have communication between the main GUI thread and the worker thread. Letting the main thread make the changes to the GUI.

Daniel F.
  • 1,760
  • 13
  • 13