I'm developing a dirt-simple text editor for personal use, customized the way I like it. My biggest difficulty is with grasping how OOP works. Could you kindly review the following code and suggest improvements (or at least link further information)? Much appreciated.
from tkinter import Tk, END, INSERT
from tkinter.scrolledtext import ScrolledText
from tkinter.filedialog import asksaveasfilename, askopenfilename
class Scratchpad:
def __init__(self, Tk):
self.display = Tk()
self.display.title("Onager Text Editor--<F7> save, <F5> open and insert")
self.display.bind('<F7>', self.save_file)
self.display.bind('<F5>', self.open_file)
self.display.bind('<Control-c>', self.copy)
self.display.bind('<Control-p>', self.cut)
self.display.bind('<Control-v>', self.paste)
def create_editor(self, ScrolledText):
self.editing_window = ScrolledText(self.display)
self.editing_window.configure(fg='gold', bg='blue', insertbackground='cyan',
height='25', width='70', padx='12', pady='12',
wrap='word', tabs='48', font='serif 12')
self.editing_window.pack()
def save_file(self, event=None):
name = asksaveasfilename()
outfile = open(name, 'w')
contents = self.editing_window.get(0.0, END)
outfile.write(contents)
outfile.close()
def open_file(self, event=None):
name = askopenfilename()
infile = open(name, 'r')
contents = infile.read()
self.editing_window.insert(INSERT, contents)
infile.close()
def copy(self, event=None):
self.display.clipboard_clear()
text = self.editing_window.get("sel.first", "sel.last")
self.display.clipboard_append(text)
def cut(self, event):
self.copy()
self.editing_window.delete("sel.first", "sel.last")
def paste(self, event):
self.editing_window.insert(INSERT, self.display.clipboard_get())
def main():
onager = Scratchpad(Tk)
onager.create_editor(ScrolledText)
onager.display.mainloop()
if __name__=='__main__':
main()