在 gtk.TextView 上画线

发布于 2024-08-17 04:42:06 字数 153 浏览 5 评论 0原文

我试图显示某个子字符串的“选择” gtk.TextView 通过在单词周围绘制边框。唯一的标记方法 到目前为止我发现的 TextView 中的文本是通过将 TextTags 放置 修改的属性。这似乎没有提供一种绘制边界的方法, 不过,GTK 支持这个还是 ONLT PYGTK 有问题

I'm trying to show the "selection" of a certain sub-string in a
gtk.TextView by drawing a border around the word. The only way to mark
text in a TextView that I've found so far is by placing TextTags with
modified properties. This does not seem to offer a way to draw a border,
though, DOES GTK SUPPORT THIS OR IS THIS A PROBLEM WITH ONLT PYGTK

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遮云壑 2024-08-24 04:42:06

我想出了如何在文本视图上绘图!

首先,让我们假设对 gtk.TextView 的引用位于一个名为“viewer”的变量中,位于您的一个类中
另外,绘图函数必须使用名为“expose-event”的事件来调用,否则绘图将被刷新并且不会保留在屏幕上。

下一部分是 gtk.TextView,它由 7 种类型的 gtk.gdk.windows 组成,您可以在其中使用 gtk.gdk.windows。 示例

gtk.TEXT_WINDOW_WIDGET
gtk.TEXT_WINDOW_TEXT
gtk.TEXT_WINDOW_LEFT - not displayed by default
gtk.TEXT_WINDOW_RIGHT  - not displayed by default
gtk.TEXT_WINDOW_TOP - not displayed by default
gtk.TEXT_WINDOW_BOTTOM
gtk.TEXT_WINDOW_PRIVATE

为了让绘图出现在 gtk.TextView 上,我们必须在 gtk.TEXT_WINDOW_TEXT 上绘图,

代码如下所示

if(viewer!=None):           
    viewer.connect("expose-event", expose_view)
        self.drawable=viewer.get_window(gtk.TEXT_WINDOW_TEXT)

def expose_view(self,window,event): 
    if(self.drawable!=None):            
    self.drawable.draw_line(self.drawable.new_gc(),1,1,30,30)
    # (1,1) and (30,30) are the coordinates and u can give the values accordingly

I figured out how to draw on a text view !!!

To begin with lets assume the reference to your gtk.TextView is in a variable called viewer, Inside one of ur classes
Also the draw function has to be called with an event called expose-event else the drawings will be refreshed and will not stay on the screen

The next part is the gtk.TextView consists of 7 types of gtk.gdk.windows on which u can draw

gtk.TEXT_WINDOW_WIDGET
gtk.TEXT_WINDOW_TEXT
gtk.TEXT_WINDOW_LEFT - not displayed by default
gtk.TEXT_WINDOW_RIGHT  - not displayed by default
gtk.TEXT_WINDOW_TOP - not displayed by default
gtk.TEXT_WINDOW_BOTTOM
gtk.TEXT_WINDOW_PRIVATE

For the drawing to appear on gtk.TextView We have to draw on gtk.TEXT_WINDOW_TEXT

An Example Code is as shown Below

if(viewer!=None):           
    viewer.connect("expose-event", expose_view)
        self.drawable=viewer.get_window(gtk.TEXT_WINDOW_TEXT)

def expose_view(self,window,event): 
    if(self.drawable!=None):            
    self.drawable.draw_line(self.drawable.new_gc(),1,1,30,30)
    # (1,1) and (30,30) are the coordinates and u can give the values accordingly
终弃我 2024-08-24 04:42:06

gtk.TextBuffer 标签中,用于设置一个或多个预定义的文本属性。如果没有子类化,这仅限于 gtk.TextTag 的属性,并且不包括类似于边框或轮廓属性的任何内容。在这方面,PyGTK 和普通 GTK+ 没有区别。

虽然有点 hacky,但完成您想做的事情的最简单方法是连接到 gtk.TextViewexpose-event,获取字符串的坐标并绘制在 event.window 上,这是公开回调中提供的事件的 gdk.Window

(请注意,您不必获取并存储 gtk.TEXT_WINDOW_TEXT 窗口,您只需在回调中检查暴露事件适用于哪个窗口,如果不是针对文本窗口,则可能会忽略该暴露。)

相反,您大概可以子类化 TextBuffer/TextView/TextTag 中的一个或多个来添加 border 标记,但这样做是否合理是另一个问题。

In a gtk.TextBuffer tags are used to set one or more pre-defined text attributes. Without subclassing, this is limited to the properties of a gtk.TextTag, and doesn't include anything akin to a border or outline property. There is no difference between PyGTK and plain GTK+ in this regard.

While somewhat hacky, the easiest way to do what you want to do is to connect to the expose-event of your gtk.TextView, get the coordinates of your string and draw on event.window, which is the gdk.Window of the event provided in the expose callback.

(Note that you don't have to get and store the gtk.TEXT_WINDOW_TEXT window, you just need to check what window the expose event is for in the callback, probably ignoring the expose if it's not for the text window.)

Instead, you could presumably subclass one or more of TextBuffer/TextView/TextTag to add a border tag, but whether it's reasonable to do so is another question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文