Results 1 to 4 of 4

Thread: [SOLVED] [PyGTK] gtk.Entry set cursor position

  1. #1
    Join Date
    Jun 2006
    Location
    Gwangju, Korea
    Beans
    3,479

    [SOLVED] [PyGTK] gtk.Entry set cursor position

    A utility I'm writing includes a gtk.Entry whose default value is the clipboard contents. Since the clipboard contents won't always fit in the Entry without scrolling, and since the most important information is at the beginning of the string, I want the first character to be visible (plus whatever other characters fit).

    My approach to this has been to try to set the cursor to position 0 (the Entry has input focus by default). However, this test code produces weird results:
    Code:
    # self.text_entry is an instance of gtk.Entry
            self.text_entry.paste_clipboard()
            print self.text_entry.get_position()
            self.text_entry.set_position(0)
            self.text_entry.insert_text('test',0)
            print self.text_entry.get_position()
    The printed results are:
    Code:
    0
    0
    Furthermore, the word "test" appears after the pasted text, but the cursor is immediately before the word "test". So not only is the cursor not getting moved, but get_position() is reporting incorrect values. What's wrong?


    EDIT: I've attached a screenshot showing the state after the code snippets above have run.
    Attached Images Attached Images
    Last edited by mssever; December 18th, 2008 at 10:51 PM.

  2. #2
    Join Date
    Jun 2006
    Location
    Gwangju, Korea
    Beans
    3,479

    Re: [PyGTK] gtk.Entry set cursor position

    I've done some more testing, and the mystery deepens. Consider this code snippet:
    Code:
    self.text_entry.paste_clipboard()
    txt = self.text_entry.get_text()
    print 'txt =', repr(txt)
    self.text_entry.set_text('this is a test')
    I expect this code to store the current value of self.text_entry (which is the same as the clipboard contents) in txt, then change the value of self.text_entry to "this is a test".

    What actually happens, though, is that txt is equal to "" and self.text_entry still contains the clipboard contents. No methods that operate on the text entry that I've tried do anything.

    What confuses me is that later in the program (in a signal handler) I'm able to successfully change the text and set it all to selected. What gives?

    EDIT: Correction: The string "this is a test" shows up at the very end of the current contents, being appended, rather than replacing the clipboard contents. The string "test" (see post 1) also shows up, immediately before "this is a test", even though it appears later in the source.
    Last edited by mssever; December 19th, 2008 at 04:46 AM.

  3. #3
    Join Date
    Jun 2006
    Location
    Gwangju, Korea
    Beans
    3,479

    Re: [PyGTK] gtk.Entry set cursor position

    I managed to work around this problem by replacing
    Code:
    self.text_entry.paste_clipboard()
    with
    Code:
    self.text_entry.set_text(gtk.Clipboard().wait_for_text())
    However, I think that paste_clipboard() is a bit cleaner, and I still don't understand why it didn't work. Any ideas?

  4. #4
    Join Date
    Dec 2008
    Beans
    1

    Re: [PyGTK] gtk.Entry set cursor position

    Hello.

    I posted an explanatory post at http://gtkforums.com/viewtopic.php?p=6454#6454

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •