tkinter右键菜单的实现

最近在写一个项目,网上找了一下tkinter的右键菜单功能,简单实用,贴上来。

from tkinter import *
abc = Tk()
abc.title('试试文本框右键菜单')
abc.resizable(False, False)
abc.geometry("300x100+200+20")
Label(abc, text='下面是一个刚刚被生成的文本框,试试操作吧').pack(side="top")
Label(abc).pack(side="top")
show = StringVar()
Entry = Entry(abc, textvariable=show, width="30")
Entry.pack()


class section:
    def onPaste(self):
        try:
            self.text = abc.clipboard_get()
        except TclError:
            pass
        show.set(str(self.text))

    def onCopy(self):
        self.text = Entry.get()
        abc.clipboard_append(self.text)

    def onCut(self):
        self.onCopy()
        try:
            Entry.delete('sel.first', 'sel.last')
        except TclError:
            pass


section = section()
menu = Menu(abc, tearoff=0)
menu.add_command(label="复制", command=section.onCopy)
menu.add_separator()
menu.add_command(label="粘贴", command=section.onPaste)
menu.add_separator()
menu.add_command(label="剪切", command=section.onCut)


def popupmenu(event):
    menu.post(event.x_root, event.y_root)


Entry.bind("<Button-3>", popupmenu)
abc.mainloop()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值