Search code examples
pythonlinuxgtkpygtkglade

adding a textview to scrolledwindow in pygtk


I am trying to add some text into a scrolledwindow in pygtk. The textview is packed inside a simple box container. I searched online and came to know that I need to first add a viewport in order for this to work but I keep getting the following error:

Gtk-CRITICAL **: gtk_viewport_add: assertion 'gtk_bin_get_child (bin) == NULL' failed

Here is my code:

container = Gtk.Box()
container.set_name('text_container')

tv = Gtk.TextView()
text_input = self.builder.get_object('entry1')
text = text_input.get_text()
text_input.set_text('')
tv.get_buffer().set_text(text)

container.pack_start(tv,True,True,0)
self.viewport.add(container)

I've followed this method because i already added a viewport to the scrolled window in my "glade" file. Any help or online resources are welcomed.


Solution

  • It worked with an HBox/VBox, that was the only problem there. However now that I have used the add_with_viewport() method, on which object should I be calling the show() method in order for my added widget to be shown? here is the final code:-

    container = Gtk.VBox()
    container.set_name('text_container')
    tv = Gtk.TextView()
    text_input = self.builder.get_object('entry1')
    text = text_input.get_text()
    if text:
            text_input.set_text('')
            tv.get_buffer().set_text(text)
    
            container.pack_start(tv,True,True,0)
            self.sw.add_with_viewport(container)
    

    since no there is no variable referencing to the just added viewport, on which object shall I call the show() method for the widget to be displayed in the scrolledwindow. I tried adding show to the container like container.show()but it didn't show up.