Welcome, guest | Sign In | My Account | Store | Cart

I wrote this simple text editor to use for my diary. It's customized the way I like it, but the code is set up so it's easy for other people to change bg, fg, font, etc. I've also compiled a standalone Windows executable (thank you very much ActiveState! without ActivePython the compilation would have been impossible). Anyone who wants a copy of the executable is free to message or email me.

NOTE: If you get an error that the theme is not recognized, just comment out line 18 or run the following code in your python3 interpreter:

>>>from tkinter.ttk import Style
>>>s = Style()
>>>s.theme_use()

You'll get a list of the available themes and can replace the 'alt' in line 18 with any one of them you want.

Python, 59 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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()

All standard keyboard shortcuts work, in addition to <F7> being bound to the save file dialog and <F5> to the open file dialog. Text from opened files is inserted at cursor point by my preference, but it's easy to change that. Files can be saved with any extension whatsoever, as the script does not hide extensions. Opening binary files and other such files doesn't seem possible.

If you improve upon this script, I warmly encourage you to let me know and, if you wish, share your ideas with me. And if you use Onager Scratchpad, as is or modified, and it works for you, I'd love to hear about it.

1 comment

Mickey Kocic (author) 7 years, 11 months ago  # | flag

I apologize for the excessive indentation of lines that shouldn't be indented. It was a misunderstanding of the instructions for submission. Just load the script into PythonWin and select Edit->Select All and then Edit->Source Code->Dedent Region before saving and executing.