X windows and Python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul Rubin

    X windows and Python?

    I'd like to program my Python script to put a string into the X
    windows cut buffer. Can anyone suggest the simplest way to do that?
    Maybe I can do it by putting up a Tkinter text widget, sticking the
    string into it, and selecting it (I'm checking the docs) but uggh.
    I'd prefer not to have to put anything on the screen. I just want to
    put the string into the cut buffer so I can paste it into another
    program.

    Thanks for any suggestions.
  • Paul Rubin

    #2
    Re: X windows and Python?

    Paul Rubin <http://phr.cx@NOSPAM.i nvalidwrites:
    I'd like to program my Python script to put a string into the X
    windows cut buffer.
    Hmm, looks like I can use w.clipboard_app end with an arbitrary tkinter
    widget, maybe without having to actually display anything. I'll try
    that.

    Comment

    • David Boddie

      #3
      Re: X windows and Python?

      Paul Rubin wrote: I'd like to program my Python script to put a
      string into the X windows cut buffer. Can anyone suggest the
      simplest way to do that? Maybe I can do it by putting up a Tkinter
      text widget, sticking the string into it, and selecting it (I'm
      checking the docs) but uggh. I'd prefer not to have to put anything
      on the screen. I just want to put the string into the cut buffer so
      I can paste it into another program. Maybe this does what you need:
      http://python-xlib.sourceforge.net/ David

      Comment

      • Paul Rubin

        #4
        Re: X windows and Python?

        Paul Rubin <http://phr.cx@NOSPAM.i nvalidwrites:
        Hmm, looks like I can use w.clipboard_app end with an arbitrary tkinter
        widget, maybe without having to actually display anything. I'll try
        that.
        Nope, that's some kind of internal Tk clipboard. Any other ideas, how
        to communicate with the X window manager?

        Comment

        • David Boddie

          #5
          Re: X windows and Python?

          The usual follow-up to "fix" Google's "formatting ". Maybe this does
          what you need:




          Comment

          • skip@pobox.com

            #6
            Re: X windows and Python?

            >>>>"Paul" == Paul Rubin <"http://phr.cx"@NOSPAM. invalidwrites:

            PaulI'd like to program my Python script to put a string into the X
            Paulwindows cut buffer. Can anyone suggest the simplest way to do
            Paulthat?

            Maybe there's some useful functionality exposed through the Python Xlib
            module:



            Skip

            Comment

            • Grant Edwards

              #7
              Re: X windows and Python?

              On 2006-08-15, Paul Rubin <httpwrote:
              I'd like to program my Python script to put a string into the
              X windows cut buffer. Can anyone suggest the simplest way to
              do that?
              There isn't a simple way to do that.

              The basic problem is that there's not really such a thing as
              "_the_ X windows cut buffer". The selection mechanism under X
              is rather complicated.

              The way (well, _one_ way) it's supposed to work is you make an
              Xlib call to claim ownership of "the selection". Then when
              somebody wants to get the contents of the selection you get an
              event to which you respond by sending the current contents of
              the selection.

              However, if your app exits before somebody requests the
              contents of the selection, then you have to use one of the
              fallback mechanisms. In addition to the "selection" , there are
              a set of cut buffer buffers that you can put things into. The
              first of those cut buffers is where apps _usually_ look if
              there is no current selection owner.

              In order to work properly, you have cover both bases:

              1) You need to request ownership of the selection and then
              respond to a selection notify event if you get one.

              2) You need to write the selection contents into cut buffer 0.

              I think it might be sufficient to do the following:

              0) Open a connection to the server and create a window.

              1) Write the selected string into cut buffer 0.

              2) Call XSetSelectionOw ner to make yourself the owner of the
              selction.

              3) Call XSetSelectionOn wer to give up ownership of the
              selection (or just close the connection to the server).

              At that point, the selection won't have an owner, and most
              applications will look in cut buffer 0. But if somebody tries
              to get the selection contents between steps 2 and 3, then
              things break unless you handle the request.

              --
              Grant Edwards grante Yow! Is this "BOOZE"?
              at
              visi.com

              Comment

              • Grant Edwards

                #8
                Re: X windows and Python?

                On 2006-08-15, Grant Edwards <grante@visi.co mwrote:
                >I'd like to program my Python script to put a string into the
                >X windows cut buffer. Can anyone suggest the simplest way to
                >do that?
                >
                There isn't a simple way to do that.
                I forgot to mention, there are actually three different
                selections (plus the cut buffers).

                Here's a nice explanation of just the selection part of the
                mess:



                If you want something quick and dirty, you can just use
                os.popen() to call xsel:



                xsel doesn't do the "cut buffer" thing. It obtains ownership of
                the selection, and then forks off a copy of itself to service
                requests for the selection contents. I would probably have
                just copied the selection to cut buffer 0 and exited, but this
                way works fine.

                --
                Grant Edwards grante Yow! I will SHAVE and
                at buy JELL-O and bring my
                visi.com MARRIAGE MANUAL!!

                Comment

                • Paul Rubin

                  #9
                  Re: X windows and Python?

                  "David Boddie" <david@boddie.o rg.ukwrites:
                  The usual follow-up to "fix" Google's "formatting ". Maybe this does
                  what you need:
                  >
                  http://python-xlib.sourceforge.net/
                  Thanks, wow, a complete xlib implementation in Python, so I'd get to
                  do low-level X programming and implement the stuff Grant Edwards
                  described. I hope I can avoid that. What I'm doing right now is
                  writing the stuff out to a file and loading it into OpenOffice, then
                  selecting the contents there and copying it the cut buffer. Blecccch.

                  Comment

                  • Paul Rubin

                    #10
                    Re: X windows and Python?

                    Grant Edwards <grante@visi.co mwrites:
                    The way (well, _one_ way) it's supposed to work is you make an
                    Xlib call to claim ownership of "the selection". ...
                    Thanks, this explanation was very interesting. It would be great if
                    tkinter had some calls to do this stuff, which I guess means the
                    underlying Tk needs them (I dunno if it has them). Anyway, for what I
                    was doing, it wasn't that urgent, and I'm using an awful workaround.

                    Comment

                    Working...