nahuelq78
nahuelq78

Reputation: 11

How to set glade label without a button event?

I'm trying to set a label without a button event.

I want my application to show another message after 2 seconds.

When I run the program, the window just shows me the last message: "bye".

Is the sequence ok? Did I forget anything?

Thanks

enter image description here

import gi
gi.require_version('Gtk','3.0')
from gi.repository import Gtk
from time import sleep

class form():

    def __init__(self):
        self.b = Gtk.Builder()
        self.b.add_from_file("color.glade")
        self.ventana_main = self.b.get_object("ventana_main")
        self.label = ""
        self.box = self.b.get_object("box_test") #obj creado en color.glade
        self.box_area = Gtk.Box()
        self.box.add(self.box_area)
        self.b.connect_signals(self)
        self.ventana_main.show_all()

    def label_hi(self, widget):
        self.label = Gtk.Label("Hola")
        self.box_area.add(self.label)
        self.box_area.show_all()

    def label_bye(self, widget):
        self.label.set_markup("Bye")

    def on_ventana_main_destroy(self, widget, data=None):
        Gtk.main_quit()

if __name__ == "__main__":
    f = form()
    f.label_hi(f.ventana_main)
    sleep(2)
    f.label_bye(f.ventana_main)
    Gtk.main()

Upvotes: 0

Views: 285

Answers (1)

liberforce
liberforce

Reputation: 11454

You can't do changes before calling Gtk.main(), because at that point, your application is not even shown.

If you want a 2 seconds delay, use GLib.timeout_add_seconds, and pass the object you want to modify (your label) as the data parameter. The function parameter you need to pass is the callback that will be called after those 2 seconds. In that callback, you just call set_label (or your label_bye) on the label you have passed as the data parameter, and you're done.

Another thing is that you should create the label and add it to your widget tree in your constructor, not in your label_hi method. label_hi and label_bye just need to call set_label to change the message displayed.

Upvotes: 1

Related Questions