beli3ver
beli3ver

Reputation: 405

Python add label to parent widget

I have currently this code:

import gi
import gettext
_ = gettext.gettext

gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')

from gi.repository import Gtk, Gio, GLib

class Invoicy(Gtk.Window):
    def __init__(self):

        Gtk.Window.__init__(self, title="Invoicy")
        self.set_border_width = 10
        self.set_default_size(800, 600)

        header_bar = Gtk.HeaderBar()
        header_bar.set_show_close_button(True)
        header_bar.props.title = "Invoicy"
        self.set_titlebar(header_bar)

        newbutton = Gtk.Button(None,image=Gtk.Image.new_from_gicon(Gio.ThemedIcon(name="document-new"), Gtk.IconSize.LARGE_TOOLBAR))
        header_bar.pack_start(newbutton)

        menubutton = Gtk.Button(None,image=Gtk.Image.new_from_gicon(Gio.ThemedIcon(name="open-menu"), Gtk.IconSize.LARGE_TOOLBAR))
        menubutton.connect("clicked", self.settingsdialog)
        header_bar.pack_end(menubutton)

        label = Gtk.Label("test")

        self.add(label)


win = Invoicy()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

Now I want, when I click on newbutton to call a function that add new label in the main window. Is this possible? When yes, how could I do this? I tried it for several hours now. Thanks.

Upvotes: 0

Views: 55

Answers (1)

Fred
Fred

Reputation: 12776

You have to call the connect method on the "newbutton" to add a callback handler function that adds the label.

newbutton.connect("clicked", self.on_newbutton_clicked)

And a a callback handler method in your Window class:

def on_newbutton_clicked(self, button):
    label = Gtk.Label("test")
    self.add(label)

Upvotes: 1

Related Questions