2

In the EVT_CLOSE handler of a wxpython app, I'm trying to flush the clipboard to leave the copied text available to other applications. However, the Flush() method always returns false.

Example:

cp = wx.Clipboard.Get()
print cp.Flush()

Returns:

False

I'm using python 2.7.3 on Fedora 16 (Gnome 3 default spin) and wx.VERSION returns (2, 8, 12, 0, '')

Thanks for any help you can give me

UPDATE: Apparently it's been a "known bug" since 2009. Not sure what'll happen with it. Please add solutions if you're way into WX

Michael Clerx
  • 2,928
  • 2
  • 33
  • 47

1 Answers1

1

I just finished creating a simple demo app and mine works. Here's my code:

import wx

########################################################################
class ClipboardPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        lbl = wx.StaticText(self, label="Enter text to copy to clipboard:")
        self.text = wx.TextCtrl(self, style=wx.TE_MULTILINE)
        copyBtn = wx.Button(self, label="Copy")
        copyBtn.Bind(wx.EVT_BUTTON, self.onCopy)
        copyFlushBtn = wx.Button(self, label="Copy and Flush")
        copyFlushBtn.Bind(wx.EVT_BUTTON, self.onCopyAndFlush)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(lbl, 0, wx.ALL, 5)
        sizer.Add(self.text, 1, wx.EXPAND)
        sizer.Add(copyBtn, 0, wx.ALL|wx.CENTER, 5)
        sizer.Add(copyFlushBtn, 0, wx.ALL|wx.CENTER, 5)
        self.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onCopy(self, event):
        """"""
        self.dataObj = wx.TextDataObject()
        self.dataObj.SetText(self.text.GetValue())
        if wx.TheClipboard.Open():
            wx.TheClipboard.SetData(self.dataObj)
            wx.TheClipboard.Close()
        else:
            wx.MessageBox("Unable to open the clipboard", "Error")

    #----------------------------------------------------------------------
    def onCopyAndFlush(self, event):
        """"""
        self.dataObj = wx.TextDataObject()
        self.dataObj.SetText(self.text.GetValue())
        if wx.TheClipboard.Open():
            wx.TheClipboard.SetData(self.dataObj)
            wx.TheClipboard.Flush()
        else:
            wx.MessageBox("Unable to open the clipboard", "Error")

        self.GetParent().Close()

########################################################################
class ClipboardFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Clipboard Tutorial")
        panel = ClipboardPanel(self)
        self.Show()


if __name__ == "__main__":
    app = wx.App(False)
    frame = ClipboardFrame()
    app.MainLoop()

I'm on Windows 7, wxPython 2.8.12 and Python 2.6.6. What are you on? Does this code work for you?

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
  • Thanks! This code gives the same result as my original version: copy-pasting works while the app is running, but the Flush method fails and returns False (I've added a "print" statement to the line where the clipboard is flushed to check). – Michael Clerx May 23 '12 at 07:23
  • As mentioned, I'm on Fedora 16 (the default 'spin', which means gnome3 and GTK), wxPython 2.8.12 and Python 2.7.3 – Michael Clerx May 23 '12 at 07:26
  • Sorry...I somehow missed that. I just tested on CentOS and I have the same behavior you've reported. I would recommend asking on the wxPython mailing list and/or reporting a bug. – Mike Driscoll May 23 '12 at 13:19
  • Known bug since February 2009 :'( – Michael Clerx May 23 '12 at 22:06
  • Five years after it was reported, and the bug is still not fixed. Probably won't happen. – tylerl Dec 25 '13 at 07:43