Python记事本复制,剪切,粘贴功能

时间:2014-03-07 18:35:17

标签: python copy paste notepad

我正在编写一个充当记事本的python程序。我已启用右键单击以进行剪切,复制和粘贴,并具有执行相同操作的编辑菜单。经过多次尝试,我只能得到错误。如何从文本框中剪切,复制和粘贴这些内容? 问题已解决

由于

这是我的固定代码

from tkinter import *

# Define
def New():
   print("New")

def Open():
   OpenFile = filedialog.askopenfilename( filetypes = ( ("ztext file", "*.z"),("zytext", "*.zy") ) )
   print(OpenFile)

def make_menu(w):
    global the_menu
    the_menu = 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)

def Save():
    print("Save")

def Save_as():
    SaveFile = filedialog.asksaveasfilename( filetypes = ( ("ztext file", "*.z"),("zytext",         "*.zy") ) )
    print(SaveFile)

def Close():
    print("Close")

def Exit():
    root.destroy

def Undo():
    print("Undo")

def Select_All():
    print("Select_All")

def Help_Index():
    print("Help Index")

def About():
    #About window
    top = Toplevel()
    top.title("About...")

    msg = Message(top, text="Note pad")
    msg.pack()

    top.mainloop()


#root window
root = Tk()
root.title("Note")
make_menu(root)

scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)

textbox = Text(root, yscrollcommand=scrollbar.set)
textbox.pack(side=LEFT, fill=BOTH)

textbox.bind_class("Text", "<Button-3><ButtonRelease-3>", show_menu)

#Menu Bar
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=New)
filemenu.add_command(label="Open", command=Open)
filemenu.add_command(label="Save", command=Save)
filemenu.add_command(label="Save as...", command=Save_as)
filemenu.add_command(label="Close", command=Close)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)

editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Undo", command=Undo)
editmenu.add_separator()
editmenu.add_command(label="Cut", command=lambda: w.event_generate("<<Cut>>"))
editmenu.add_command(label="Copy", command=lambda: w.event_generate("<<Copy>>"))
editmenu.add_command(label="Paste", command=lambda: w.event_generate("<<Paste>>"))
editmenu.add_command(label="Select All", command=Select_All)
menubar.add_cascade(label="Edit", menu=editmenu)

helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index", command=Help_Index)
helpmenu.add_command(label="About...", command=About)
menubar.add_cascade(label="Help", menu=helpmenu)

root.config(menu=menubar)

root.mainloop()

我正在运行python 3.3.3

1 个答案:

答案 0 :(得分:0)

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()