copy/cut/paste functions in my notepad type program

▼魔方 西西 提交于 2019-12-25 06:18:28

问题


I am writing a notepad type program and am unable to use the copy, paste and cut function. I can't seem to figure what is wrong with the code. I have looked at many sources modifying the code and i have come out with the following.

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)

These are the edit functions. The rest of the program is below.

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

running the full program throws back an error:

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'

I am running python 3.3.3 with windows 8.

来源:https://stackoverflow.com/questions/22235069/copy-cut-paste-functions-in-my-notepad-type-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!