Python 3 tkinter notepad Cut and Copy

时间:2018-03-22 23:50:13

标签: python tkinter

Hello I'm trying to make a notepad written in python using tkinter. I'm having trouble making the edit menu work. I'm currently trying to implement copy, cut, and paste but I'm having issues. When I try to press the cut button I get a message like this "TypeError: cut() missing 2 required positional arguments: 'self' and 'event'" I'm honestly new to the whole class thing in Python so this is my first attempt at using that. I've shortened my code for convenience. Any help would be appreciated!

class Notepad:

    #Functions
    def __init__(self, master, **kw):
        Text.__init__(self, master, **kw)
        self.bind('<Control-c>', self.copy)
        self.bind('<Control-x>', self.cut)
        self.bind('<Control-v>', self.paste)

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

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

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

    root = Tk()
    menu = Menu(root)
    root.config(menu=menu)

    root.title('Written in Python')
    root.minsize(width=100, height=100)
    root.geometry('800x500+350+150') #Height, Width, X, Y coordinates of the program

    #NotePad
    textArea = ScrolledText.ScrolledText(root, width=1000, height=100) 
    #Height and width of notepad
    textArea.pack()

    root = Tk()
    menu = Menu(root)
    root.config(menu=menu)
    editMenu = Menu(menu)
    menu.add_cascade(label="Edit",menu=editMenu)
    editMenu.add_separator()
    editMenu.add_command(label="Cut", command=cut)
    editMenu.add_command(label="Copy", command=copy)
    editMenu.add_command(label="Paste", command=paste)

    root.mainloop()

1 个答案:

答案 0 :(得分:0)

函数cut定义了两个参数:selfevent,但是菜单命令不带参数调用cut(),因此出现错误消息。

另外,你的类结构对我来说很奇怪,特别是将最后一段代码直接放在类中,而不是放在类方法中。我建议您使用自定义方法和绑定创建继承自Notepad(或Text)的ScrolledText类,并将root = Tk() ...放在类之外,如下所示:

import tkinter as tk
from tkinter.scrolledtext import ScrolledText

class Notepad(ScrolledText):

    def __init__(self, master, **kw):
        ScrolledText.__init__(self, master, **kw)
        self.bind('<Control-c>', self.copy)
        self.bind('<Control-x>', self.cut)
        self.bind('<Control-v>', self.paste)

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

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

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


if __name__ == '__main__':
    root = tk.Tk()
    menu = tk.Menu(root)
    root.config(menu=menu)

    root.title('Written in Python')
    root.minsize(width=100, height=100)
    root.geometry('800x500+350+150') #Height, Width, X, Y coordinates of the program

    #NotePad
    notepad = Notepad(root, width=1000, height=100) 
    #Height and width of notepad
    notepad.pack()

    editMenu = tk.Menu(menu)
    menu.add_cascade(label="Edit", menu=editMenu)
    editMenu.add_separator()
    editMenu.add_command(label="Cut", command=notepad.cut)
    editMenu.add_command(label="Copy", command=notepad.copy)
    editMenu.add_command(label="Paste", command=notepad.paste)

    root.mainloop()

在上面的代码中,函数cutcopypasteNotepad类的方法,您可以使用notepad.cut(event)调用它们。因为,你没有在方法中使用event参数,它只是绑定,我建议你def cut(self, event=None)使event成为可选的,默认值为{ {1}}。这样,您可以直接在None中使用notepad.copy作为命令。