wx clipboard issue

Hi!

Um, new to the list. This question has probably been asked, and answered, before - but please, bear with me.

I am currently developing a small Python program to accomplish a simple task (I thought); To take text A from source X and transform it to format B, and place the result in destination Y. For instance, take simple HTML and transform it into simple LaTeX, or RTF, or some other format. This is all well and good, you say - what does this have to do with wxpython?

Well, you see, I got it into my head that it'd be pretty neat to be able to use the clipboard as source and destination. You copy the text you want translated, run the program, and paste wherever you want.

Since I was already using wx for the ui part of the program I thought I'd experiment a bit with the wx.clipboard component. The first part, getting stuff from the clipboard, worked just fine. Now, putting stuff in it... not so much.

It seems like I am able to put stuff in the clipboard - but when my program exists, the clipboard becomes empty. And I cannot for the life of me understand why.

I attach a simple testing program I found somewhere; I've tried it both in windows and in ubuntu. Writing, copying and pasting works well enough when the program is running - but if it is closed, particularly if it's closed before the contents of the clipboard have been pasted outside the program, I just get an empty clipboard.

Now to me, this seems strange. Any suggestions?

Thanks,
//Mikael "Krank" Bergström

testing-clipboard.py (1.32 KB)

Krank wrote:

Hi!

Um, new to the list. This question has probably been asked, and answered, before - but please, bear with me.

I am currently developing a small Python program to accomplish a simple task (I thought); To take text A from source X and transform it to format B, and place the result in destination Y. For instance, take simple HTML and transform it into simple LaTeX, or RTF, or some other format. This is all well and good, you say - what does this have to do with wxpython?

Well, you see, I got it into my head that it'd be pretty neat to be able to use the clipboard as source and destination. You copy the text you want translated, run the program, and paste wherever you want.

Since I was already using wx for the ui part of the program I thought I'd experiment a bit with the wx.clipboard component. The first part, getting stuff from the clipboard, worked just fine. Now, putting stuff in it... not so much.

It seems like I am able to put stuff in the clipboard - but when my program exists, the clipboard becomes empty. And I cannot for the life of me understand why.

I attach a simple testing program I found somewhere; I've tried it both in windows and in ubuntu. Writing, copying and pasting works well enough when the program is running - but if it is closed, particularly if it's closed before the contents of the clipboard have been pasted outside the program, I just get an empty clipboard.

Now to me, this seems strange. Any suggestions?

Thanks,
//Mikael "Krank" Bergström

Looks like you'll need to call the Flush method on your clipboard object for it to remain in memory:

<quote>

      wxClipboard::Flush

*bool* *Flush*()

Flushes the clipboard: this means that the data which is currently on clipboard will stay available even after the application exits (possibly eating memory), otherwise the clipboard will be emptied on exit. Returns false if the operation is unsuccessful for any reason.

</quote>

http://docs.wxwidgets.org/stable/wx_wxclipboard.html
http://www.wxpython.org/docs/api/wx.Clipboard-class.html

Note that this can eat memory if you copy large data into the clipboard. It would seem that wx tries to avoid that unless you specifically tell it to leave it there. Microsoft Office does much the same thing. If you copy large amounts of data into the clipboard and go to close the app, it will ask you if you want to save the data in the clipboard.

Hopefully that is what you're looking for.

···

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org

Looks exactly right... Just one small problem: I can't seem to get the little bugger to do anything. It always reports false, and there's no effect whatsoever for me.

Unfortunately, it doesn't say what went wrong. It just says False. "for any reason" indeed... Any ideas?

//Mikael "Krank" Bergström

···

Looks like you'll need to call the Flush method on your clipboard object for it to remain in memory:

<quote>

     wxClipboard::Flush

*bool* *Flush*()

Flushes the clipboard: this means that the data which is currently on clipboard will stay available even after the application exits (possibly eating memory), otherwise the clipboard will be emptied on exit. Returns false if the operation is unsuccessful for any reason.

</quote>

http://docs.wxwidgets.org/stable/wx_wxclipboard.html
http://www.wxpython.org/docs/api/wx.Clipboard-class.html

Note that this can eat memory if you copy large data into the clipboard. It would seem that wx tries to avoid that unless you specifically tell it to leave it there. Microsoft Office does much the same thing. If you copy large amounts of data into the clipboard and go to close the app, it will ask you if you want to save the data in the clipboard.

Hopefully that is what you're looking for.

-------------------
Mike Driscoll

Hi,

Hard to say. What is reporting "false"? Are you calling the flush in your close handler or what? Can you put together a small runnable example?

See the following for how: http://wiki.wxpython.org/MakingSampleApps

Mike

···

Looks exactly right... Just one small problem: I can't seem to get the little bugger to do anything. It always reports false, and there's no effect whatsoever for me.

Unfortunately, it doesn't say what went wrong. It just says False. "for any reason" indeed... Any ideas?

//Mikael "Krank" Bergström

Looks like you'll need to call the Flush method on your clipboard object for it to remain in memory:

<quote>

     wxClipboard::Flush

*bool* *Flush*()

Flushes the clipboard: this means that the data which is currently on clipboard will stay available even after the application exits (possibly eating memory), otherwise the clipboard will be emptied on exit. Returns false if the operation is unsuccessful for any reason.

</quote>

http://docs.wxwidgets.org/stable/wx_wxclipboard.html
http://www.wxpython.org/docs/api/wx.Clipboard-class.html

Note that this can eat memory if you copy large data into the clipboard. It would seem that wx tries to avoid that unless you specifically tell it to leave it there. Microsoft Office does much the same thing. If you copy large amounts of data into the clipboard and go to close the app, it will ask you if you want to save the data in the clipboard.

Hopefully that is what you're looking for.

-------------------
Mike Driscoll

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

Krank wrote:

It seems like I am able to put stuff in the clipboard - but when my program exists, the clipboard becomes empty. And I cannot for the life of me understand why.

Just to add a bit to what Mike already said about this: Putting something in the clipboard normally does not actually put the data there, it simply informs the system that the app has some data in a particular format available. In other words, all that gets put into the clipboard is a promise to provide some data in a specific format. Then when the pasting app wants to paste it goes through the clipboard APIs and fetches the data directly from the app that made the promise.

So you see that when the app exits it can no longer provide that data or fulfill its promise. The Flush method is provided to transfer that data to the system so it can still be provided to the pasting app.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Krank wrote:

Looks exactly right... Just one small problem: I can't seem to get the little bugger to do anything. It always reports false, and there's no effect whatsoever for me.

Unfortunately, it doesn't say what went wrong. It just says False. "for any reason" indeed... Any ideas?

Platform and version? What is the format of the data in the clipboard?

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!