How to change window elements when a button is pressed?

Hi, I am new to coding, so please be patient. I know a little bit of C, Matlab and Python (used only in terminal to create some terminal programs) and I would like to contribute by creating an application with a GUI. I read a bit but I don’t understand how does everything works

My hard question is: how can I change elements in the window, when a button is pressed? Everything returned by the functions don’t trigger changes. See this example:

Example
#!/usr/bin/python
# -*- coding: utf-8 -*-

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Handy', '0.0')
from gi.repository import Gtk, GdkPixbuf, Handy


class Program(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Test")
        self.set_default_size(800, 600)
        self.set_position(Gtk.WindowPosition.CENTER)

        hb = Handy.HeaderBar()
        hb.set_show_close_button(True)
        hb.props.title = "Test"
        self.set_titlebar(hb)

        test1 = ""
        test2 = ""

        button1 = Gtk.Button()
        button1.add(Gtk.Image.new_from_icon_name(
            "document-new", Gtk.IconSize.BUTTON))
        button1.connect("clicked", self.on_new_clicked)
        hb.pack_start(button1)

        button2 = Gtk.Button()
        button2.set_label("Press")
        button2.connect("clicked", self.on_button_clicked)

        self.add(button2)        
        if (test1 != ""):
                label1 = Gtk.Label()
                label1.set_text(test1)
                self.add(label1)
        if (test2 != ""):
                label2 = Gtk.Label()
                label2.set_text(test2)
                self.add(label2)

    def on_new_clicked(self, args):
        print("You pressed New button: it should prompt something in the main window, but it doesn't")
        test1 = "You pressed New button and everything works"
        return test1

    def on_button_clicked(self, args):
        print("You pressed a button in the window: it should prompt something in the main window, but it doesn't")
        test2 = "You pressed a button in the window and everything works"
        return test2

def main():
    app = Program()
    app.connect("delete-event", Gtk.main_quit)
    app.show_all()
    Gtk.main()


if __name__ == '__main__':
    main()

This program creates a window with two buttons (“New” in the headerbar and one centered in the main window) and I’d like that if you press one of those a label appears. How can I do that?
This stupid example would help me in solving other complex situations.
Thank you very much!

@airon90,
I think you should put label.set_text(test) instructions into the two callback functions if you want the text to change when you click.

Hi.

There are a lot of ways to do it. All the ways share the common method where you call a function to do a “show the label”. You can do it with a Gtk.MessageDialog like this :

def on_new_clicked(self, args):
        print("You pressed New button: it should prompt something in the main window, but it doesn't")
        test1 = "You pressed New button and everything works"
        dialog = Gtk.MessageDialog(text=test1, buttons=Gtk.ButtonsType.CLOSE) # Show a dialog with a close button
        dialog.run()
        dialog.hide() # Hide it after closing
        del dialog # Destroy it
...

There are also methods like sending a notification, using Gtk.InfoBar etc. The question is which suits here :smile:

@j_arun_mani Thank you for your answer but it is not what I want. Maybe I express the concept in a bad way. I don’t want to show something in some way but I want that the main window changes when a button is pressed. One of my goal is to create a (physical) book catalogue app: when you press New it shows a popup (and that’s okay) where to add data and then the main window changes, so that it splits itself into two parts: a lateral panel listing books and a window where to show data of books if one is selected.

I cannot trigger the main window to change itself. That is what I mainly want.

In this example I’d like that a new label is shown in the main window :slight_smile:

@vmagnin If I add .set_text(text) in the functions I don’t know how to add them in the main window, then

Your init(self) will be executed only once, at start, and you must define your gui there. I think you should put into it (without any if):

label1 = Gtk.Label()
label1.set_text("something)
self.add(label1)

Your callback functions will be called each time you click. And they should contain something like:

label1.set_text("You pressed New button and everything works)

In Gtk everything is a widget, so why not simplify our process. I see from your code that there is no active element inside your window (only a title-bar). The logic to accomplish what you want is quite simple. Add a widget to the window and replace it with another when required. Like initally have a Gtk.Grid with all buttons and elementary stuff. When you require a change, modify the grid as you like.

In your case the “require a change” is when the button is clicked. So make a function that changes the children of your grid. Easy as that. :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.