English 中文(简体)
Draw Lines on gtk.TextView
原标题:
  • 时间:2010-01-08 12:01:09
  •  标签:
  • 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

问题回答

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

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.





相关问题
Draw Lines on gtk.TextView

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 ...

pygtk: cannot set parent on toplevel widget

I m working on a project that has a glade GUI. I need the main window to have 2 section, divided by a gtk.Hpaned widget (horizontal panes). The left pane would have a tool-bar like layout of buttons,...

pygtk: howto change background of a gtk.TextView widget

I want to make the background of a textview widget black and the foreground white. Been trying the .modify_bg and .modify_fg methods, but none affect the way this thing looks. Can anyone suggest ...

how to properly destroy gtk.Dialog objects/widgets

Noob @ programming with python and pygtk. I m creating an application which includes a couple of dialogs for user interaction. #!usr/bin/env python import gtk info = gtk.MessageDialog(type=gtk....

python GTK container for mjpeg stream

I have an mjpeg stream from a web-cam and would like to display it in an application written in python using pygtk. The stream is a string of bytes from the driver. What widget would be best for ...

Python, thread and gobject

I am writing a program by a framework using pygtk. The main program doing the following things: Create a watchdog thread to monitor some resource Create a client to receive data from socket call ...

热门标签