wxpython 在整个应用程序中剪切复制并粘贴

发布于 2024-12-18 01:46:42 字数 183 浏览 0 评论 0原文

我正在开发一个带有多个 TextCtrl 和 ComboBox 小部件的小型应用程序。我希望当我按下 Ctrl-C Ctrl-V 和 Ctrl-X 键时,我会得到复制、粘贴和剪切适当条目的通常行为。

我现在得到的是,虽然我可以右键单击并显示文本 copd/past/cutd,但我无法通过键绑定或菜单项。我怎样才能以简单的方式获得这个?

I'm developing a small application with multiple TextCtrl and ComboBox widgets. I want that when I press the key Ctrl-C Ctrl-V and Ctrl-X I get the usual behaviour of copying, pasting and cutting in the appropriate entry.

What I obtain right now is that, while I can right-click and have the text copd/past/cutd, I can't through the keybindings or the menu entries. How can I obtain this in a simple way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小红帽 2024-12-25 01:46:42

菜单键绑定默认使用 Alt-first_menu_letter ->子菜单第一个字母。
所选项目的菜单事件应绑定相应的事件处理程序:

self.Bind(wx.EVT_MENU, self.on_copy, self.copy)

对于复制方法,您首先用鼠标选择要复制的文本。然后,您可以通过以下方式获取焦点小部件(具有要复制的选定字符串的特定文本控件):

widget = self.FindFocus()

通过这种方式,现在您可以从该小部件中获取选定的字符串:

self.copied = widget.GetStringSelection()

并且必须执行相同的操作才能将复制的文本粘贴到你放置光标的textctrl。
这里有一个工作示例:

import wx

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.tctrl_1 = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE)
        self.tctrl_2 = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE)

        self.menubar = wx.MenuBar()
        self.test = wx.Menu()
        self.copy = wx.MenuItem(self.test, wx.NewId(), "copy", "is_going to copy", wx.ITEM_NORMAL)
        self.test.AppendItem(self.copy)
        self.paste = wx.MenuItem(self.test, wx.NewId(), "paste", "will paste", wx.ITEM_NORMAL)
        self.test.AppendItem(self.paste)
        self.menubar.Append(self.test, "Test")
        self.SetMenuBar(self.menubar)

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_MENU, self.on_copy, self.copy)
        self.Bind(wx.EVT_MENU, self.on_paste, self.paste)

    def __set_properties(self):
        self.SetTitle("frame_1")

    def __do_layout(self):
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_2.Add(self.tctrl_1, 1, wx.EXPAND, 0)
        sizer_2.Add(self.tctrl_2, 1, wx.EXPAND, 0)
        sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()

    def on_copy(self, event): 
        widget = self.FindFocus()
        self.copied = widget.GetStringSelection()

    def on_paste(self, event): 
        widget = self.FindFocus()
        widget.WriteText(self.copied)


if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    frame = MyFrame(None, -1, "")
    frame.Show()
    app.MainLoop()

Menu keybindings work by default with Alt-first_menu_letter -> submenu_first_letter.
The menu event of the selected item should bind the corresponding event handler:

self.Bind(wx.EVT_MENU, self.on_copy, self.copy)

for a copy method, you first select the text you want to copy with the mouse. Then you can get the widget that is focused (the specific textcontrol with the selected string to be copied) with:

widget = self.FindFocus()

in this way now you can get the selected string from that widget:

self.copied = widget.GetStringSelection()

And the same has to be done for pasting the copied text in the textctrl you situate the cursor.
Here you have a working example:

import wx

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.tctrl_1 = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE)
        self.tctrl_2 = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE)

        self.menubar = wx.MenuBar()
        self.test = wx.Menu()
        self.copy = wx.MenuItem(self.test, wx.NewId(), "copy", "is_going to copy", wx.ITEM_NORMAL)
        self.test.AppendItem(self.copy)
        self.paste = wx.MenuItem(self.test, wx.NewId(), "paste", "will paste", wx.ITEM_NORMAL)
        self.test.AppendItem(self.paste)
        self.menubar.Append(self.test, "Test")
        self.SetMenuBar(self.menubar)

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_MENU, self.on_copy, self.copy)
        self.Bind(wx.EVT_MENU, self.on_paste, self.paste)

    def __set_properties(self):
        self.SetTitle("frame_1")

    def __do_layout(self):
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_2.Add(self.tctrl_1, 1, wx.EXPAND, 0)
        sizer_2.Add(self.tctrl_2, 1, wx.EXPAND, 0)
        sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()

    def on_copy(self, event): 
        widget = self.FindFocus()
        self.copied = widget.GetStringSelection()

    def on_paste(self, event): 
        widget = self.FindFocus()
        widget.WriteText(self.copied)


if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    frame = MyFrame(None, -1, "")
    frame.Show()
    app.MainLoop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文