简体   繁体   中英

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.

Missed the real reason first time round.

The problem is that you are adding more than one widget to the viewport (which can only have one widget).

gtk_viewport_add: assertion 'gtk_bin_get_child (bin) == NULL' failed

Translates to: "The assertion that the viewport has no children is false", because you've already added something.

You need to pack all your containers into another container, and then add that to the viewport.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM