from tkinter import Tk, END, INSERT
from tkinter.ttk import Frame, Style
from tkinter.scrolledtext import ScrolledText
from tkinter.filedialog import asksaveasfilename, askopenfilename
class Scratchpad:
def __init__(self):
self.window = Tk()
self.window.title("Onager Scratchpad")
self.create_frame()
self.create_editing_window()
self.window.bind('<F7>', self.save_file)
self.window.bind('<F5>', self.open_file)
def create_frame(self):
frame_style = Style()
frame_style.theme_use('alt')
frame_style.configure("TFrame",
relief='raised')
self.frame = Frame(self.window, style="frame_style.TFrame")
self.frame.rowconfigure(1)
self.frame.columnconfigure(1)
self.frame.grid(row=0, column=0)
def create_editing_window(self):
self.editing_window = ScrolledText(self.frame)
self.editing_window.configure(fg='gold',
bg='blue',
font='serif 12',
padx='15',
pady='15',
wrap='word',
borderwidth='10',
relief='sunken',
tabs='48',
insertbackground='cyan')
self.editing_window.grid(row=0, column=0)
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 main():
onager = Scratchpad()
onager.window.mainloop()
if __name__=='__main__':
main()
Diff to Previous Revision
--- revision 1 2016-04-24 02:17:39
+++ revision 2 2016-04-24 02:34:03
@@ -1,62 +1,59 @@
- from tkinter import Tk, END, INSERT
- from tkinter.scrolledtext import ScrolledText
- from tkinter.filedialog import asksaveasfilename, askopenfilename
+from tkinter import Tk, END, INSERT
+from tkinter.ttk import Frame, Style
+from tkinter.scrolledtext import ScrolledText
+from tkinter.filedialog import asksaveasfilename, askopenfilename
- # Onager Text Editor
- # 2016 Mickey Kocic
+class Scratchpad:
- class Scratchpad:
+ def __init__(self):
+ self.window = Tk()
+ self.window.title("Onager Scratchpad")
+ self.create_frame()
+ self.create_editing_window()
+ self.window.bind('<F7>', self.save_file)
+ self.window.bind('<F5>', self.open_file)
- 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)
- self.display.bind('<Control-d>', self.clear_editor)
+ def create_frame(self):
+ frame_style = Style()
+ frame_style.theme_use('alt')
+ frame_style.configure("TFrame",
+ relief='raised')
+ self.frame = Frame(self.window, style="frame_style.TFrame")
+ self.frame.rowconfigure(1)
+ self.frame.columnconfigure(1)
+ self.frame.grid(row=0, column=0)
- 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 create_editing_window(self):
+ self.editing_window = ScrolledText(self.frame)
+ self.editing_window.configure(fg='gold',
+ bg='blue',
+ font='serif 12',
+ padx='15',
+ pady='15',
+ wrap='word',
+ borderwidth='10',
+ relief='sunken',
+ tabs='48',
+ insertbackground='cyan')
+ self.editing_window.grid(row=0, column=0)
- 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 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 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 main():
+ onager = Scratchpad()
+ onager.window.mainloop()
- 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 clear_editor(self, event=None):
- self.editing_window.delete(0.0, END)
-
- def main():
- onager = Scratchpad(Tk)
- onager.create_editor(ScrolledText)
- onager.display.mainloop()
-
- if __name__=='__main__':
- main()
+if __name__=='__main__':
+ main()