简体   繁体   English

将文本视图添加到pygtk中的scrolledwindow中

[英]adding a textview to scrolledwindow in pygtk

I am trying to add some text into a scrolledwindow in pygtk. 我试图在pygtk中的滚动窗口中添加一些文本。 The textview is packed inside a simple box container. textview包装在一个简单的盒子容器中。 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. 我已遵循此方法,因为我已经在“ glade”文件的滚动窗口中添加了视口。 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. 它与HBox / VBox一起使用,这是唯一的问题。 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? 但是,既然我已经使用了add_with_viewport()方法,那么我应该在哪个对象上调用show()方法,以便显示我添加的小部件? 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. 因为没有没有变量引用刚添加的视口,所以我应该在哪个对象上调用show()方法以使小部件显示在滚动窗口中。 I tried adding show to the container like container.show() but it didn't show up. 我尝试将show添加到类似container.show()的容器中,但是没有显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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