WxPython 剪切、复制、粘贴功能

WxPython Cut, Copy, Paste functions

我正在制作一个小应用程序,但我无法定义有效的“编辑”菜单功能。

我试过这个:

from pyautogui import hotkey
.
.
.
def OnCopy ( self, event ):
   hotkey ( 'ctrl, 'c' )

但是,上述方法并不总是有效,有时甚至会中断。有没有更好的方法?

wxPython 有它自己的 Clipboard object. Its implementation depends on the use of a wxDataObject 因为,当然,您可以将多种类型的数据复制并粘贴到剪贴板中

import wx


class MainFrame(wx.Frame):
    """Create MainFrame class."""
    def __init__(self, *args, **kwargs):
        super().__init__(None, *args, **kwargs)
        self.size = (400, 1000)

        self.panel = MainPanel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.panel)
        self.SetSizer(sizer)
        self.Center()
        self.Show()

    def on_copy(self, event):
        if wx.TheClipboard.Open():
            # Put some text onto the clipboard
            text = self.panel.txt_input.GetValue()
            data_object = wx.TextDataObject(text)
            wx.TheClipboard.SetData(data_object)

            # Now retrieve it from the clipboard and print the value
            text_data = wx.TextDataObject()
            success = wx.TheClipboard.GetData(text_data)
            wx.TheClipboard.Close()
            if success:
                print(f'This data is on the clipboard: {text_data.GetText()}')


class MainPanel(wx.Panel):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        self.parent = parent
        self.txt_input = wx.TextCtrl(self)
        cmd_copy = wx.Button(self, wx.ID_COPY)
        cmd_copy.Bind(wx.EVT_BUTTON, parent.on_copy)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.txt_input)
        sizer.Add(cmd_copy)
        self.SetSizer(sizer)


if __name__ == '__main__':
        wx_app = wx.App()
        MainFrame()
        wx_app.MainLoop()