How to make menubar cut / copy / paste using Python / Tkinter

I would like to make menu items (in the menu bar, and not in the pop-up window of the right mouse button) that can cut / copy / paste any text.

Equivalent keyboard commands already work if I haven’t done anything to enable them. For example, I can enter text in an input field, cut it out with Control-X and paste it back (or in another place) with Control-C.

Messages on a topic that I saw come down to cut / copy / paste for individual widgets, but that already works. How to make menu items activate them?

Thank.

EDIT: To be clear, the problems are:

  • how to make menu items for cut / copy action on any text selected in any widget
  • how to have a paste menu item, paste text wherever the text cursor

Again, the key commands for this (Control-x, Control-c, Control-v) already work without my having to do anything. I know how to make a menu; the question is which command should I attach to the menu items in order to have the desired effect.

EDIT 2: Ok, I have a way that works. Since the key commands are already working, we can simply generate them. In my case, everything is a notebook called noteBook, so

lambda: self.noteBook.event_generate('<Control-x>')

cuts as desired. For instance:

editmenu.add_command(label="Cut", accelerator="Ctrl+X", command=lambda: self.noteBook.event_generate('<Control-x>'))

When using: https://github.com/lnmaurer/qubit-control-interface/commit/c08c10a7fbc4a637c1e08358fb9a8593dfdf116e

However, there may be a cleaner way to do this; answer if you know it.

+5
source share
4 answers

:

import Tkinter

def make_menu(w):
    global the_menu
    the_menu = Tkinter.Menu(w, tearoff=0)
    the_menu.add_command(label="Cut")
    the_menu.add_command(label="Copy")
    the_menu.add_command(label="Paste")

def show_menu(e):
    w = e.widget
    the_menu.entryconfigure("Cut",
    command=lambda: w.event_generate("<<Cut>>"))
    the_menu.entryconfigure("Copy",
    command=lambda: w.event_generate("<<Copy>>"))
    the_menu.entryconfigure("Paste",
    command=lambda: w.event_generate("<<Paste>>"))
    the_menu.tk.call("tk_popup", the_menu, e.x_root, e.y_root)

t = Tkinter.Tk()
make_menu(t)

e1 = Tkinter.Entry(); e1.pack()
e2 = Tkinter.Entry(); e2.pack()
e1.bind_class("Entry", "<Button-3><ButtonRelease-3>", show_menu)

t.mainloop()
+9

focus_get(), , , . .

editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Cut", \
                     accelerator="Ctrl+X", \
                     command=lambda: \
                             mywindow.focus_get().event_generate('<<Cut>>'))
+5

from Tkinter import *

class Test(Text):
    def __init__(self, master, **kw):
        Text.__init__(self, master, **kw)
        self.bind('<Control-c>', self.copy)
        self.bind('<Control-x>', self.cut)
        self.bind('<Control-v>', self.paste)

    def copy(self, event=None):
        self.clipboard_clear()
        text = self.get("sel.first", "sel.last")
        self.clipboard_append(text)

    def cut(self, event):
        self.copy()
        self.delete("sel.first", "sel.last")

    def paste(self, event):
        text = self.selection_get(selection='CLIPBOARD')
        self.insert('insert', text)

def test():
    r = Tk()
    t = Test(r)
    t.pack(fill='both', expand=1)
    r.mainloop()

if __name__ == '__main__':
    test()
+2
source

I just stumbled upon your question nine months too late (does this make a pregnant pause?). This code works for me:

    editmenu = Menu(menubar, tearoff=0)
    editmenu.add_command(label="Cut", \
                         accelerator="Ctrl+X", \
                         command=lambda: \
                                 self.editor.event_generate('<<Cut>>'))
    editmenu.add_command(label="Copy", \
                         accelerator="Ctrl+C", \
                         command=lambda: \
                                 self.editor.event_generate('<<Copy>>'))
    editmenu.add_command(label="Paste", \
                         accelerator="Ctrl+V", \
                         command=lambda: \
                                 self.editor.event_generate('<<Paste>>'))
    menubar.add_cascade(label="Edit", menu=editmenu)
-1
source

All Articles