gpt4 book ai didi

python - Gtk+ 3 - 在 TextView 中编写和自动滚动的问题

转载 作者:行者123 更新时间:2023-11-28 16:49:47 24 4
gpt4 key购买 nike

我有一个 Gtk.ScrolledWindow(),里面有一个 Gtk.TextView(),(我正在使用 PyGObject 自省(introspection))。

在我插入一些文本并从另一个线程自动滚动后,连续多次调用 append_text 方法,我收到了一些警告,然后 GUI 崩溃了。

这是我代码中比较麻烦的部分:

class TextViewWindow(Gtk.Window):

[...]

def append_text(self, text):
self.textbuffer.insert(self.textbuffer.get_end_iter(), text)
self.scroll_to_end()

def scroll_to_end(self):
self.textbuffer.place_cursor(self.textbuffer.get_end_iter())
insert_mark = self.textbuffer.get_insert()
self.textview.scroll_to_mark(insert_mark , 0.0, True, 0.0, 1.0)

有人知道我做错了什么吗?

--
以下是我遇到的一些错误:

Gtk-WARNING **: Invalid text buffer iterator: either the iterator is uninitialized, or the characters/pixbufs/widgets in the buffer have been modified since the iterator was created. You must use marks, character numbers, or line numbers to preserve a position across buffer modifications. You can apply tags and insert marks without invalidating your iterators, but any mutation that affects 'indexable' buffer contents (contents that can be referred to by character offset) will invalidate all outstanding iterators

Pango-CRITICAL **: pango_layout_index_to_pos: assertion `layout != NULL' failed

Gtk-WARNING **: gtktextview.c:4813: somehow some text lines were modified or scrolling occurred since the last validation of lines on the screen - may be a text widget bug.

Gtk:ERROR:gtktextview.c:4814:gtk_text_view_paint: code should not be reached

最佳答案

更新Gtk widgets from other threads ,您需要:

一个。在调用 Gtk.main()

之前调用以下方法
GObject.threads_init()
Gdk.threads_init()

在您的主题中,将更新 Gtk 小部件的代码括起来:

Gdk.threads_enter()
# your code here
Gdk.threads_leave()

在你的情况下,你可能需要这样的东西:

Gdk.threads_enter()
your_window.append_text('something')
your_window.scroll_to_end()
Gdk.threads_leave()

关于python - Gtk+ 3 - 在 TextView 中编写和自动滚动的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8803041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com