在我的记事本类型程序中复制/剪切/粘贴功能

时间:2014-03-06 20:19:58

标签: python notepad

我正在写一个记事本类型的程序,我无法使用复制,粘贴和剪切功能。我似乎无法弄清楚代码有什么问题。我看过许多修改代码的来源,我已经提出了以下内容。

from tkinter import *

#Class
class Edit():
    def __init__(self):
        textbox.__init__(self)
        self.bind('<Control-c>', self.copy)
        self.bind('<Control-x>', self.cut)
        self.bind('<Control-v>', self.paste)

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

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

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

这些是编辑功能。 该计划的其余部分如下。

#root window
edit = Edit
root = Tk()
root.title("Note")

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

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

#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=edit.cut)
editmenu.add_command(label="Copy", command=edit.copy)
editmenu.add_command(label="Paste", command=edit.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)

#Popup menu
menu = Menu(root, tearoff=0)
menu.add_command(label="Copy", command=edit.copy)
menu.add_command(label="Paste", command=edit.paste)

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

textbox.bind("<Button-3>", popup)

root.mainloop()

运行完整程序会抛出错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
    return self.func(*args)
TypeError: **<<paste,copy or cut>>**() missing 1 required positional argument: 'self'

我正在使用Windows 8运行python 3.3.3。

0 个答案:

没有答案