如何制作“清除剪贴板”功能菜单项?

时间:2018-10-28 17:31:33

标签: wxpython wxpython-phoenix

我试图制作一个功能明确的“清除剪贴板”菜单项,但是它不起作用。控制台没有显示任何错误。知道发生了什么事吗?

编辑器:

import wx
import wx.stc as stc

class Window(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(500, 500))
        self.control = stc.StyledTextCtrl(self, style=wx.TE_MULTILINE | wx.TE_WORDWRAP)
        self.clipboard = wx.Clipboard()
        self.EditMenu(), self.BindsMenu(), self.MenuBar()
    def EditMenu(self):
        self.editmenu = wx.Menu()
        self.clear_clipboard = self.editmenu.Append(wx.ID_ANY, "&Clear Clipboard\tCtrl+Del", "Clear clipboard")
    def MenuBar(self):
        self.menu = wx.MenuBar()
        self.menu.Append(self.editmenu, "&Edit")
        self.SetMenuBar(self.menu)
    def BindsMenu(self):
        self.Bind(wx.EVT_MENU, self.ClearClipboard, self.clear_clipboard)
    def ClearClipboard(self, e):
        self.clipboard.Clear()

def main():
    app = wx.App()
    frame = Window(None, "Text Editor")
    frame.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()

谢谢

1 个答案:

答案 0 :(得分:1)

至少在wx 4.0.3中的Linux上,clipboard中似乎确实存在一个“错误”
如果您定义自己的wx.Clipboard并使用它,则会收到以下错误消息:

/home/vagrant/wxPython-4.0.3/ext/wxWidgets/src/gtk/clipbrd.cpp(91): assert "clipboard == ms_clipboard" failed in OnDone(): got notification for alien clipboard

该消息表明未考虑Linux平台,即对ms_clipboard的引用
但是,如果您使用wx.TheClipboard,则效果很好,即

import wx
import wx.stc as stc

class Window(wx.Frame):

    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(500, 500))
        self.control = stc.StyledTextCtrl(self, style=wx.TE_MULTILINE | wx.TE_WORDWRAP)
        self.EditMenu()
        self.BindsMenu()
        self.MenuBar()

    def EditMenu(self):
        self.editmenu = wx.Menu()
        self.copy_clipboard = self.editmenu.Append(wx.ID_ANY, "&Copy to Clipboard", "Copy to clipboard")
        self.clear_clipboard = self.editmenu.Append(wx.ID_ANY, "C&lear Clipboard", "Clear clipboard")
        self.paste_clipboard = self.editmenu.Append(wx.ID_ANY, "&Paste Clipboard", "Paste clipboard")

    def MenuBar(self):
        self.menu = wx.MenuBar()
        self.menu.Append(self.editmenu, "&Edit")
        self.SetMenuBar(self.menu)

    def BindsMenu(self):
        self.Bind(wx.EVT_MENU, self.CopyClipboard, self.copy_clipboard)
        self.Bind(wx.EVT_MENU, self.ClearClipboard, self.clear_clipboard)
        self.Bind(wx.EVT_MENU, self.PasteClipboard, self.paste_clipboard)

    def ClearClipboard(self, e):
        wx.TheClipboard.Open()
        wx.TheClipboard.Clear()
        wx.TheClipboard.Close()

    def CopyClipboard(self, e):
        clipdata = wx.TextDataObject()
        copy_data = self.control.GetSelectedText()
        if copy_data:
            clipdata.SetText(copy_data)
            wx.TheClipboard.Open()
            wx.TheClipboard.SetData(clipdata)
            wx.TheClipboard.Close()

    def PasteClipboard(self, e):
        clipdata = wx.TextDataObject()
        wx.TheClipboard.Open()
        wx.TheClipboard.GetData(clipdata)
        wx.TheClipboard.Close()
        self.control.AddText(clipdata.GetText())

def main():
    app = wx.App()
    frame = Window(None, "Text Editor")
    frame.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()