Question

I'm trying to use GtkEntry.insert-at-cursor signal in my code, but it doesnt seem to work.

    def on_servername_activate(self, widget):
       output = StringIO.StringIO()         
       servername = widget.get_text()
       self.output.write("USHARE_NAME="+servername+'\n')

This is a part of my code where I would like to use insert-at-cursor. I am almost sure that this is a mistake I am making. I replaced on_servername_activate with on_servername_insertatcursor (because on_servername_insert-at-cursor gives me a syntax error at the first hyphen) and nothing happens when text is inserted into the box. Nothing gets inserted into output, however everything works perfectly with the activate signal.

Was it helpful?

Solution

It seems there is a conceptual error between signals and methods.

On one hand, the signals are triggered when an event occurs, those signals can be names as strings like 'insert-at-cursor' or 'insert_at_cursor'. On the other hand, you need to connect those signals (in a widget) with your functions/methods. The functions/methods can have any arbitrary name, for making them easier to read we try to name them as close as the signals, but it is not mandatory.

In your case, you might want to have something like:

class Foo:
  ...
  def create_widgets(self):
      entry.gtkEntry()
      entry.connect('insert-at-cursor', self.entry_insert_at_cursor)

  def entry_insert_at_cursor(self, *args):
      # some code

As you can see, in entry.connect(...) happens the match between signal and method. That explains the syntax error in your code.

The another misconception seems to be the use of the signal insert-at-cursor. For that signal, you have to bind the entry to a key, which does not seem the behaviour you are looking for. Depending of the version of GTK you are targeting, you might want to use:

      entry.connect('changed', self.entry_changed)

or

      entry.connect('insert-text', self.entry_insert_text)

In the latter, your method will receive the text changed as well as the position. Similarly, you can deal directly with the buffer associated with the entry:

      entry.connect('inserted-text', self.buffer_entry_inserted_text)

Where the method will receive additionally the numbers of bytes inserted.

OTHER TIPS

I'd rather use inserted-text signal callback of gtk.EntryBuffer object, like follows:

def on_insert_cb (buf, pos, chars, nch):
    print buf.get_text()

# --- skip ---

entry = gtk.Entry()
entry.get_buffer().connect("inserted-text", on_insert_cb)

# --- skip ---
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top