#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    24

    Putting something to the clipboard


    Is there a way that is not os dependant that puts something into the clipboard(you know when you hit copy for text), i am not wanting the user to have to highlight and select copy. I want to insert something into the clipboard so that it will be able to be pasted everywhere else(even outside the python program)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    In wxPython there is an easy to use clipboard class. Putting text to the clipboard looks like this:
    Code:
    clip = wxClipboard()
    clip.Open()
    clip.SetData(wxTextDataObject(some_text))
    clip.Close()
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    24
    I just got time to test that out, I found it really isn't working. I am glad it is done iwth wxPython bcause i am starting to use it. However when i run that it doesn't put it in the clipboard. I cannot past it anywhere else. I looked up the c code for wxClipboard, and i read through what everything does. I even tested a few thigns it still doesn't let me past it anywhere else. It just clears the clipboard and it freezes up a bit before not putting anything. almost like maybe the clipboard is being curroped or it can't read something

    any ideas?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Bimingham,AL
    Posts
    68
    Rep Power
    10
    Did you see if Clipboard is supported for you platform.

    Did you intialize wx app first before using clipboard ?

    I tried out the following code and it worked perfect
    Code:
    do = wx.TextDataObject()
    do.SetText("Some Text")
    wx.TheClipboard.Open()
    wx.TheClipboard.SetData(do)
    wx.TheClipboard.Close()
    hope that helps
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    24
    well, I was testing it on linux. I hope it works on linux because that is the main os that i am going ot be using it under. What tyou did should give the same output of what mine did. Yes i initized wxPython and such. I did not get any error msges i just got nothing put to the clipboard. Actually it deleted everything in the clipboard and then put nothing into it. When i try to past it actually kinda freezes up(try to past outside of the app, it also doesn't work inside the app).
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    24
    Ok, i have played around with it some more, I fond out it isn't in the wxPython module it is just in wx. So i imported just that. Then i ran what you put it works like when i import wxPython but now when it trys to set data to the clipboard i get a strange error. here is what a get:

    14:46:07: Error: Failed to put data on the clipboard (error 2147221008: coInitialize has not been called.)
    False
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Location
    Bimingham,AL
    Posts
    68
    Rep Power
    10
    Originally Posted by CyBerHigh
    Ok, i have played around with it some more, I fond out it isn't in the wxPython module it is just in wx. So i imported just that. Then i ran what you put it works like when i import wxPython but now when it trys to set data to the clipboard i get a strange error. here is what a get:

    14:46:07: Error: Failed to put data on the clipboard (error 2147221008: coInitialize has not been called.)
    False

    This means that you have not created and intialized Wx.app ( which calls coInitalize) .
    Add the following line to you code

    Code:
    wx.App(False)
    that should work

    look into wx python demo for more on clipboard
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    24
    man, i don't think i have ever had this difficult of a problem. last time i checked i couldn't find anything in the demo files on it either.

    now when i run this i get no errors but it just clears my clipboard memorie

    Code:
    from wxPython.wx import *
    
    wxApp(False)
    clipdata = wxTextDataObject()
    clipdata.SetText("Hi folks!")
    wxTheClipboard.Open()
    wxTheClipboard.SetData(clipdata)
    
    
    wxTheClipboard.Close()
    i just can't figure out why my code isn't work. I have tryed to put the flush in it, but it says that it doesn't exsite. i read in the c ducs that to save it to the clipboard so it doesn't get deleted when you close it you have to run flush. but that doesn't work. either.

    any ideas?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    Sorry for beeing too short with the example.

    The problem is that wx objects must be run from within a working GUI environment. If you run them from the python command line interface, they will not work as expected, even if the command line is started from a GUI (like Konsole on KDE or cmd on windows).

    To have the example working, you must run it either in an IDLE session (regardless of the operating sytem, it works on Linux AND windows) or in another Python GUI Shell, or from within a running wx application.

    If you want to use wxPython, you may want to consider Boa Constructor as a RAD tool. The Boa IDE contains a Python Shell which can be used for testing code like the this.

    If you run the example from IDLE or a Boa Shell you don't need to create a wxApp object. At least this works for me on windows and KDE. I didn't try it on Gnome or another GUI.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    24
    o ok, well that makes since now why i coudln't get it to work. THank you very much for you help. I will test that out and try to figure out a way to put it in my tkinter app now..lol

IMN logo majestic logo threadwatch logo seochat tools logo