RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 814546
Accepted
оаав ирыыва
оаав ирыыва
Asked:2020-04-15 21:12:08 +0000 UTC2020-04-15 21:12:08 +0000 UTC 2020-04-15 21:12:08 +0000 UTC

如何为多个条目和文本添加上下文菜单?

  • 772

这是代码草图:

from tkinter import*

root = Tk()

can = Canvas(root, bg="white")
can.pack()

text1 = Entry(can, bg="white", fg="black", width=50)
text1.grid(column=1,row=1)

text2 = Entry(can, bg="white", fg="black", width=50)
text2.grid(column=2,row=1)

text3 = Entry(can, bg="white", fg="black", width=50)
text3.grid(column=3,row=1)


root.mainloop()

如何使它看起来像在照片中,当您右键单击Enrty或时Text,会出现一个带有按钮的上下文菜单:剪切选定的文本或剪切。如何实施?

在此处输入图像描述

python
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Twiss
    2020-04-16T02:23:44Z2020-04-16T02:23:44Z

    使用内置事件的略微缩短的版本。
    在这种情况下,您设置

    entry_1.bind_class("Entry", "<Button-3><ButtonRelease-3>", func)
    

    就像使用特定类下的函数(在这种情况下Entry)。然后在这个 ( func) 中编写动作(例如

    lambda: w.focus_force() or w.event_generate("<<Cut>>")  
    

    这本质上是突出显示小部件并删除某些选定的文本)

    import tkinter as tk
    
    
    class Main(tk.Tk):
        def __init__(self):
            super().__init__()
    
            self.menu = tk.Menu(tearoff=0)
            self.menu.add_command(label="Вырезать", accelerator="Ctrl+X", command=lambda: self.w.focus_force() or self.w.event_generate("<<Cut>>"))
            self.menu.add_command(label="Копировать", accelerator="Ctrl+С", command=lambda: self.w.focus_force() or self.w.event_generate("<<Copy>>"))
            self.menu.add_command(label="Вставить", accelerator="Ctrl+V", command=lambda: self.w.focus_force() or self.w.event_generate("<<Paste>>"))
            self.menu.add_command(label="Удалить", accelerator="Delete", command=lambda: self.w.focus_force() or self.w.event_generate("<<Clear>>"))
            self.menu.add_separator()
            self.menu.add_command(label="Выделить все", accelerator="Ctrl+A", command=lambda: self.w.focus_force() or self.w.event_generate("<<SelectAll>>"))
    
            entry_1 = tk.Entry()
            entry_1.pack()
    
            entry_2 = tk.Entry()
            entry_2.pack()
            entry_3 = tk.Entry()
            entry_3.pack()
    
            text = tk.Text()
            text.pack()
            text.bind("<Button-3>", self.func)
            self.bind_class("Entry", "<Button-3><ButtonRelease-3>", self.func)
    
        def func(self, event):
    
            self.menu.post(event.x_root, event.y_root)
            self.w = event.widget
    
    
    if __name__ == "__main__":
        main = Main()
        main.mainloop()
    

    在此处输入图像描述

    • 3
  2. Best Answer
    insolor
    2020-04-15T22:33:22Z2020-04-15T22:33:22Z

    一个简单的选项:我们继承自tk.Text,在新类中添加上下文菜单的外观,我们实现所有必要的操作。然后我们创建不是类的小部件tk.Text,而是我们自己的类。

    实现tk.Text并tk.Entry使用多重继承。我确信在这种情况下可以更正确地完成多重继承,如果有人发表评论,我会很高兴。

    import tkinter as tk
    
    class AddPopupMenu:
        def copy_selection(self):
            try:
                selection_text = self.selection_get()
            except tk.TclError:
                return
    
            root.clipboard_clear()
            root.clipboard_append(selection_text)
            print('Copied:', selection_text)
    
        def delete_selection(self):
            try:
                self.delete('sel.first', 'sel.last')
            except tk.TclError:
                pass  # Nothing selected
    
        def cut_selection(self):
            self.copy_selection()
            self.delete_selection()
    
        def paste_from_clipboard(self):
            try:
                clipboard_text = root.clipboard_get()
            except tk.TclError:
                pass
            else:
                self.delete_selection()
                self.insert(tk.INSERT, clipboard_text)
    
        def select_all(self):
            # Пример отсюда https://stackoverflow.com/a/13808423/4752653
            self.tag_add(tk.SEL, "1.0", tk.END)
            self.mark_set(tk.INSERT, "1.0")
            self.see(tk.INSERT)
    
        def show_context_menu(self, event):
            pos_x = self.winfo_rootx() + event.x
            pos_y = self.winfo_rooty() + event.y
            self.popup_menu.tk_popup(pos_x, pos_y)
    
        def init_menu(self):
            menu = tk.Menu(self, tearoff=False)
            menu.add_command(label="Вырезать", command=self.cut_selection)
            menu.add_command(label="Копировать", command=self.copy_selection)
            menu.add_command(label="Вставить", command=self.paste_from_clipboard)
            menu.add_command(label="Удалить", command=self.delete_selection)
            menu.add_separator()
            menu.add_command(label="Выделить все", command=self.select_all)
            return menu
    
        def __init__(self, widget_class, *args, **kwargs):
            widget_class.__init__(self, *args, **kwargs)
            self.popup_menu = self.init_menu()
            self.bind("<3>", self.show_context_menu)
    
    
    class MyText(tk.Text, AddPopupMenu):
        def __init__(self, *args, **kwargs):
            AddPopupMenu.__init__(self, tk.Text, *args, **kwargs)
    
    
    class MyEntry(tk.Entry, AddPopupMenu):
        def __init__(self, *args, **kwargs):
            AddPopupMenu.__init__(self, tk.Entry, *args, **kwargs)
    
    
    root = tk.Tk()
    
    text1 = MyText(root, width=50, height=10)
    text1.pack()
    
    text2 = MyText(root, width=50, height=10)
    text2.pack()
    
    entry1 = MyEntry(root, width=50)
    entry1.pack()
    
    entry2 = MyEntry(root, width=50)
    entry2.pack()
    
    root.mainloop()
    

    截屏

    • 2

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    是否可以在 C++ 中继承类 <---> 结构?

    • 2 个回答
  • Marko Smith

    这种神经网络架构适合文本分类吗?

    • 1 个回答
  • Marko Smith

    为什么分配的工作方式不同?

    • 3 个回答
  • Marko Smith

    控制台中的光标坐标

    • 1 个回答
  • Marko Smith

    如何在 C++ 中删除类的实例?

    • 4 个回答
  • Marko Smith

    点是否属于线段的问题

    • 2 个回答
  • Marko Smith

    json结构错误

    • 1 个回答
  • Marko Smith

    ServiceWorker 中的“获取”事件

    • 1 个回答
  • Marko Smith

    c ++控制台应用程序exe文件[重复]

    • 1 个回答
  • Marko Smith

    按多列从sql表中选择

    • 1 个回答
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Suvitruf - Andrei Apanasik 什么是空? 2020-08-21 01:48:09 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5