Widgets which were not automatically realized

I wrote an application with (among many other things) a notebook with several pages. From elsewhere in the application, I need to update (eg) page 3 of the notebook, but that page hasn’t been realized as it hasn’t been shown yet, as thus gives an error.

Is there some established way to solve this problem? (I could probably call the .realize() method for the unrealized notebook tab, but that doesn’t seem very elegant)

You can try map signal.

Thanks for the reply, q962.

I tried to call gtk_widget_show_all() when I create the notebook. That still doesn’t realize on all notebook tabs.

I tried to connect a handler to the notebook’s realize signal. Maybe I’m doing it wrong but that doesn’t work either.

As commented in the original post, I try to add an item to the ListStore on tab 3 from another part of the program. If I debug it, I find that the pointer to the ListStore is still NULL.

Can you describe the structure of the code.

I can not understand.

Didn’t you add your three GtkNoteBooKPags via gtk_notebook_append_page/install?

This all requires adding an instance of GtkWidget and I can’t figure out how this all happens.

Need more information.

If I am not mistaken, Gtk only maps the pages of the GtkNotebook only when the page is shown. Not when the page is initialially not visible. So, if you make a notebook, by default only the first page is shown, the others are only rendered when needed.

So, I have notebook with 3 functions: Page 0 for inputting data, Page 1 for editing, and Page 2 shows a log for all steps. If I enter data on Page 0 without first showing page 2, the program crashes. If I first look at page 2, then input the data, all is ok.

I see, but it’s confusing me. When you add a page through append, you need to create an instance of gtkwidget. After inheriting gtkwidget, you can initialize the data in init. I don’t know where your gliststore is stored. Is it passed as a parameter? I don’t know what went wrong. I think only knowing the structure of your code can further analyze your problem.

------------------ Original ------------------

The following program (this version works - it’s in C that I have problems) shows minimally what I am doing:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#  test_realize_order.py
#  Copyright 2023 John Coppens <john@jcoppens.com>
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  GNU General Public License for more details.
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GooCanvas', '2.0')
from gi.repository import Gtk, GooCanvas

class MainWindow(Gtk.Window):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.connect("destroy", lambda x: Gtk.main_quit())
        self.set_default_size(400, 300)

        nb = Gtk.Notebook()

        btn = Gtk.Button(label = 'Click to add line')
        btn.connect('clicked', self.add_line_clicked)

        self.store = Gtk.ListStore(str)
        view = Gtk.TreeView(
                    model = self.store)
        renderer = Gtk.CellRendererText()
        col = Gtk.TreeViewColumn('Items', renderer, text = 0)

        nb.append_page(btn, Gtk.Label(label = 'Add line'))
        nb.append_page(view, Gtk.Label(label = 'List store'))



    def add_line_clicked(self, btn):
        print('Button clicked, adding line')
        self.store.append( ('A new line', ) )

    def run(self):

mainwdw = MainWindow()

If I first look at Page 1, then, on Page 0 click on the button, there’s no problem.
If after restarting the program, I click on the button first, the program crashes.
(again, the problem is in C, not in this Python version).

There is nothing wrong with this logic.

self.store exists independently of any gtkwidget .
But you say you need to activate the second page for it to work, which doesn’t make sense.

I believe, your C code logic is not the same as Python’s.

And you say that the value of self.store is null, which rules out the possibility that self.store is freed.

Are you really sure that the logic of c and python is consistent?

But you say you need to activate the second page for it to work, which doesn’t make sense.

I’ve repeated this test several times… If I try to add a record without first opening Page 2 (just click the tab), and then going back to Page 0, the program crashes.

And you say that the value of self.store is null, which rules out the possibility that self.store is freed.

As the GUI doesn’t change during the program’s operation, the program doesn’t ‘free’ any of its widgets.

I can only be sure about the observations I make. I remember having had this same problem years ago with a similar program.

I’ll try to distill a manageable-sized version of the C program, but it was much easier to do it in Python. As I tried to explain in my previous mail, the idea was only to show how the program should work.


Have you tried gdb’s awatch ?
On the next line of self.store = new use

(gdb) p app
$1 = 0x5555555d82a0 [GtkApplication]
(gdb) awatch *0x5555555d82a0
Hardware access (read/write) watchpoint 2: *0x5555555d82a0

Then click on the second tab to see which line of code operates on the variable

Remember to use the value of the pointer, not the variable name.

I’ll try that later, thanks!

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