Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9070549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:42:30+00:00 2026-06-16T17:42:30+00:00

I want to get selected text automatically in python, using gtk module. There is

  • 0

I want to get selected text automatically in python, using gtk module.
There is a code to get selected text from anywhere:

#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk

class GetSelectionExample:
    # Signal handler invoked when user clicks on the
    # "Get String Target" button
    def get_stringtarget(self, widget):
        # And request the "STRING" target for the primary selection
        ret = widget.selection_convert("PRIMARY", "STRING")
        return

    # Signal handler called when the selections owner returns the data
    def selection_received(self, widget, selection_data, data):

        # Make sure we got the data in the expected form
        if str(selection_data.type) == "STRING":
            # Print out the string we received
            print "STRING TARGET: %s" % selection_data.get_text()

        elif str(selection_data.type) == "ATOM":
            # Print out the target list we received
            targets = selection_data.get_targets()
            for target in targets:
                name = str(target)
                if name != None:
                    print "%s" % name
                else:
                    print "(bad target)"
        else:
            print "Selection was not returned as \"STRING\" or \"ATOM\"!"

        return False


    def __init__(self):
        # Create the toplevel window
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_title("Get Selection")
        window.set_border_width(10)
        window.connect("destroy", lambda w: gtk.main_quit())

        vbox = gtk.VBox(False, 0)
        window.add(vbox)
        vbox.show()

        # Create a button the user can click to get the string target
        button = gtk.Button("Get String Target")
        eventbox = gtk.EventBox()
        eventbox.add(button)
        button.connect_object("clicked", self.get_stringtarget, eventbox)
        eventbox.connect("selection_received", self.selection_received)
        vbox.pack_start(eventbox)
        eventbox.show()
        button.show()

        window.show()

def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    GetSelectionExample()
    main()

But i don’t want to this exactly.

i dont want to click a button. I want to see the selected text only, not after a button clicking. When i start the program ; it must show me the selected text automatically (without clicking any button!).

I want to this exactly :

#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk


class MyApp (object):
    def __init__(self):
        self.window=gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.connect("delete_event", gtk.main_quit )
        self.entry = gtk.Entry()

        try:
            self.s_text=gtk.SelectionData.get_text
            # i expect that : the selected text (from anywhere)
            # but it returns me :
            #<method 'get_text' of 'gtk.SelectionData' objects>
        except:
            self.s_text="it must be selected text"

        self.entry.set_text("Selected Text is : %s"  % self.s_text )

        self.window.add(self.entry)
        self.window.show_all()
    def main(self):
        gtk.main()

app=MyApp()
app.main()

This program must show me the selected text in the entry box automatically.

Only this. But i can’t do!

i expect ” gtk.SelectionData.get_text ” will show me the selected text but it returns “<method 'get_text' of 'gtk.SelectionData' objects>” .

And also i tried self.s_text=gtk.SelectionData.get_text()

But it returns me :

self.s_text=gtk.SelectionData.get_text()
TypeError: descriptor 'get_text' of 'gtk.SelectionData' object needs an argument

How can i do this? And also i am a beginner python programmer; if u can write the code ; it will be very good for me 🙂
thanks a lot !!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:42:31+00:00Added an answer on June 16, 2026 at 5:42 pm
    self.s_text=gtk.SelectionData.get_text
    

    Method get_text is not called yet! You are assigning self.s_text to the method(function) object itself. Which is converted to string in "Selected Text is : %s" % self.s_text
    You should changed it to:

    self.s_text=gtk.SelectionData.get_text()
    

    EDIT: But since you need a SelectionData object to pass to this method, the whole idea is wrong. I combined you two codes as something working:

    #!/usr/bin/env python
    
    import pygtk
    pygtk.require('2.0')
    import gtk
    
    
    class MyApp (object):
        def __init__(self):
            self.window=gtk.Window(gtk.WINDOW_TOPLEVEL)
            self.window.connect("delete_event", gtk.main_quit )
            self.entry = gtk.Entry()
            self.window.selection_convert("PRIMARY", "STRING")
            self.window.connect("selection_received", self.selection_received)
            self.window.add(self.entry)
            self.window.show_all()
        # Signal handler called when the selections owner returns the data
        def selection_received(self, widget, selection_data, data):
            print 'selection_data.type=%r'%selection_data.type
            # Make sure we got the data in the expected form
            if str(selection_data.type) == "STRING":
                self.entry.set_text("Selected Text is : %s"  % selection_data.get_text())
    
            elif str(selection_data.type) == "ATOM":
                # Print out the target list we received
                targets = selection_data.get_targets()
                for target in targets:
                    name = str(target)
                    if name != None:
                        self.entry.set_text("%s" % name)
                    else:
                        self.entry.set_text("(bad target)")
            else:
                self.entry.set_text("Selection was not returned as \"STRING\" or \"ATOM\"!")
    
            return False
        def main(self):
            gtk.main()
    
    app=MyApp()
    app.main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get the selected text from any application. Is there a way
I'm using the following code to get the selected text, my question is, how
I'm using the spinner, and I want to get the text selected for the
I want to get the currently selected item (text, image, etc) and display in
I want to get the context of selected text in web page which means
I want to get a code, that I want the selected tab index in
I have the follwing code. I want to get the value of the selected
I'm using http://jqueryui.com/demos/tabs/#manipulation . I want to get an title of current selected tab
What I want: get a xml from the AppData to use What I code
I want to get the previously selected value of a dropdown control using jquery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.