How to Use wxPython to obtain data in the system clipboard: wxpython clipboard

Source: Internet
Author: User

How to Use wxPython to obtain data in the system clipboard: wxpython clipboard

When it comes to developing desktop programs, especially text processing, clipboard is very common. Unlike java, the access to the clipboard in wxpython is very simple and a few words are enough.

# Obtain the clipboard and make sure it is in the open status text_obj = wx. textDataObject () wx. theClipboard. open () if wx. theClipboard. isOpened () or wx. theClipboard. open (): # do something... wx. theClipboard. close ()

Valid value:

if wx.TheClipboard.GetData(text_obj):  text = text_obj.GetText()

Write value:

Text_obj.SetText ('value to be written ') wx. TheClipboard. SetData (text_obj)

In the following example, click Copy to Copy the value in the text box to the clipboard. Click Paste to Paste the text in the clipboard to the text box.

"""Get text from and put text on the clipboard."""import wxclass MyFrame(wx.Frame):  def __init__(self):    wx.Frame.__init__(self, None, title='Accessing the clipboard', size=(400, 300))    # Components    self.panel = wx.Panel(self)    self.text = wx.TextCtrl(self.panel, pos=(10, 10), size=(370, 220))    self.copy = wx.Button(self.panel, wx.ID_ANY, label='Copy', pos=(10, 240))    self.paste = wx.Button(self.panel, wx.ID_ANY, label='Paste', pos=(100, 240))    # Event bindings.    self.Bind(wx.EVT_BUTTON, self.OnCopy, self.copy)    self.Bind(wx.EVT_BUTTON, self.OnPaste, self.paste)  def OnCopy(self, event):    text_obj = wx.TextDataObject()    text_obj.SetText(self.text.GetValue())    if wx.TheClipboard.IsOpened() or wx.TheClipboard.Open():      wx.TheClipboard.SetData(text_obj)      wx.TheClipboard.Close()  def OnPaste(self, event):    text_obj = wx.TextDataObject()    if wx.TheClipboard.IsOpened() or wx.TheClipboard.Open():      if wx.TheClipboard.GetData(text_obj):        self.text.SetValue(text_obj.GetText())      wx.TheClipboard.Close()app = wx.App(False)frame = MyFrame()frame.Show(True)app.MainLoop()


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.