如何使用 Python/Tkinter 制作菜单栏剪切/复制/粘贴 [英] How to make menubar cut/copy/paste with Python/Tkinter
问题描述
我想制作可以剪切/复制/粘贴任何选定文本的菜单项(在菜单栏中,而不是在右键单击弹出窗口中).
等价的键盘命令已经可以工作了,而我没有做任何事情来启用它们.例如,我可以在输入框中输入文本,使用 Control-X 剪切它,然后使用 Control-C 将其粘贴回(或其他地方).
我看到的有关该主题的帖子归结为单个小部件的剪切/复制/粘贴,但这已经有效.如何让菜单项激活它们?
谢谢.
需要明确的是,问题是:
- 如何使剪切/复制的菜单项作用于任何小部件中选择的任何文本
- 如何让粘贴菜单项在文本光标所在的位置粘贴文本
同样,执行此操作的关键命令(Control-x、Control-c、Control-v)已经可以在我没有做任何事情的情况下工作.我知道如何制作菜单;问题是我应该将什么命令附加到菜单项才能达到预期效果.
编辑 2:好的,我有一个有效的方法.由于关键命令已经工作,我们可以生成它们.就我而言,一切都是一个名为 noteBook 的笔记本,所以
lambda: self.noteBook.event_generate('
根据需要进行切割.例如:
editmenu.add_command(label="Cut",Accelerator="Ctrl+X", command=lambda: self.noteBook.event_generate('<Control-x>'))
>
不过,可能有一种更简洁的方法来做到这一点;知道的请回复.
试试这个:来源
导入Tkinterdef make_menu(w):全局 the_menuthe_menu = Tkinter.Menu(w,tearoff=0)the_menu.add_command(label="Cut")the_menu.add_command(label="复制")the_menu.add_command(label="粘贴")def show_menu(e):w = e.widgetthe_menu.entryconfigure("剪切",command=lambda: w.event_generate("<>"))the_menu.entryconfigure("复制",command=lambda: w.event_generate("<<复制>>"))the_menu.entryconfigure("粘贴",command=lambda: w.event_generate("<<粘贴>"))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", "", show_menu)t.mainloop()
I'd like to make menu items (in the menubar, not in a right click pop-up window) that can cut/copy/paste whatever text is selected.
The equivalent keyboard commands already work without my having done anything to enable them. For example, I can enter text in an entry box, cut it with Control-X, and paste it back (or elsewhere) with Control-C.
The posts on the topic I've seen boil down to cut/copy/paste for individual widgets, but that already works. How do I make the menu items activate them?
Thanks.
EDIT: Just to be clear, The issues are:
- how to make the menu items for cut/copy act on whatever text is selected in any widget
- how to have the paste menu item paste text wherever the text cursor is
Again, the key commands to do this (Control-x, Control-c, Control-v) already work without my having done anything. I know how to make the menus; the question is just what command I should attach to the menu items to have the desired effect.
EDIT 2: Ok, I've got a way that works. Since the key commands already work, we can just generate them. In my case, everything is a a notebook named noteBook so
lambda: self.noteBook.event_generate('<Control-x>')
cuts as desired. For example:
editmenu.add_command(label="Cut", accelerator="Ctrl+X", command=lambda: self.noteBook.event_generate('<Control-x>'))
In use: https://github.com/lnmaurer/qubit-control-interface/commit/c08c10a7fbc4a637c1e08358fb9a8593dfdf116e
Still, there's probably a cleaner way to do this; please reply if you know it.
try this: source
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()
这篇关于如何使用 Python/Tkinter 制作菜单栏剪切/复制/粘贴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!