|
| 1 | +#!/usr/bin/env python3 |
| 2 | +## Import modules |
| 3 | +import tkinter as tk |
| 4 | +from tkinter import filedialog as tkfd |
| 5 | +from tkinter import messagebox as tkmb |
| 6 | +import os |
| 7 | +from tkinter import colorchooser |
| 8 | + |
| 9 | + |
| 10 | +## Classes |
| 11 | +def verify_f_name(f_name): |
| 12 | + if f_name in [None, (), '']: |
| 13 | + return False |
| 14 | + return True |
| 15 | + |
| 16 | +root_window = False |
| 17 | +root_window_obj = None |
| 18 | +class Window: |
| 19 | + def __init__(self, title, root_win=None): |
| 20 | + global root_window, root_window_obj |
| 21 | + self.title = title |
| 22 | + if root_window == False: |
| 23 | + self.win = tk.Tk() |
| 24 | + root_window_obj = self.win |
| 25 | + else: |
| 26 | + self.win = tk.Toplevel() |
| 27 | + self.win.title(self.title) |
| 28 | + |
| 29 | + def window_raw(self): |
| 30 | + return self.win |
| 31 | + |
| 32 | + |
| 33 | +class TextEditor: |
| 34 | + def __init__(self, parent, root_title, set_root_title): |
| 35 | + ## Init |
| 36 | + self.parent = parent |
| 37 | + self.widget = tk.Text(self.parent) |
| 38 | + self.ftypes = [ |
| 39 | + ('All files', '*'), |
| 40 | + ('Python code files', '*.py'), |
| 41 | + ('Perl code files', '*.pl;*.pm'), # semicolon trick |
| 42 | + ('Java code files', '*.java'), |
| 43 | + ('C++ code files', '*.cpp;*.h'), # semicolon trick |
| 44 | + ('Text files', '*.txt'), |
| 45 | + ] |
| 46 | + self.initial_dir = os.path.expanduser('~') |
| 47 | + self.curr_file = None |
| 48 | + self.set_root_title = set_root_title |
| 49 | + self.root_title = root_title |
| 50 | + def __savefiledata__(self, f_name, data): |
| 51 | + print('Saving', f_name) |
| 52 | + self.initial_dir = os.path.dirname(f_name) |
| 53 | + f_obj = open(f_name, 'w') |
| 54 | + f_obj.write(data) |
| 55 | + f_obj.close() |
| 56 | + self.curr_file = f_name |
| 57 | + def __updatetitle__(self, f_name=None): |
| 58 | + if f_name == None: |
| 59 | + f_name = self.curr_file |
| 60 | + if f_name == None: |
| 61 | + self.set_root_title(self.root_title) |
| 62 | + else: |
| 63 | + self.set_root_title(self.root_title+' - '+f_name) |
| 64 | + def widget_raw(self): |
| 65 | + ## Return the raw Tk text widget |
| 66 | + return self.widget |
| 67 | + def pack(self, *args, **kwargs): |
| 68 | + ## Pack the text widget |
| 69 | + self.widget.pack(kwargs) |
| 70 | + def grid(self, *args, **kwargs): |
| 71 | + ## Grid the text widget |
| 72 | + self.widget.grid(kwargs) |
| 73 | + def clear_text(self, *args, **kwargs): |
| 74 | + ## Clear text from text widget |
| 75 | + self.widget.delete(*args, **kwargs) |
| 76 | + def set_text(self, text, *args, **kwargs): |
| 77 | + ## Clears and sets the text |
| 78 | + self.clear_text(0.0) |
| 79 | + self.widget.insert(0.0, text, args, kwargs) |
| 80 | + def get_text(self, *args, **kwargs): |
| 81 | + ## Gets the text from the text widget |
| 82 | + text = self.widget.get(*args, **kwargs) |
| 83 | + return text |
| 84 | + def open_file(self, *args, **kwargs): |
| 85 | + ## Asks for a file to open and shows it in text widget |
| 86 | + #args and kwargs to this function are thrown away |
| 87 | + f_name = tkfd.askopenfilename(filetypes=self.ftypes, initialdir=self.initial_dir) |
| 88 | + if not verify_f_name(f_name): |
| 89 | + return |
| 90 | + print('Opening', f_name) |
| 91 | + self.initial_dir = os.path.dirname(f_name) |
| 92 | + f_text = open(f_name, 'r').read() |
| 93 | + self.set_text(text=f_text) |
| 94 | + self.curr_file = f_name |
| 95 | + self.__updatetitle__() |
| 96 | + def save_file(self, *args, **kwargs): |
| 97 | + if verify_f_name(self.curr_file): |
| 98 | + #Save file as currently open file |
| 99 | + self.__savefiledata__(self.curr_file, self.get_text(0.0)) |
| 100 | + else: |
| 101 | + #No file currently open |
| 102 | + self.saveas_file() |
| 103 | + def saveas_file(self, *args, **kwargs): |
| 104 | + ## Asks for a file to save the contents of the text widget |
| 105 | + ## in and saves it there |
| 106 | + #args and kwargs to this function are thrown away |
| 107 | + f_name = tkfd.asksaveasfilename(filetypes=self.ftypes, initialdir=self.initial_dir) |
| 108 | + if not verify_f_name(f_name): |
| 109 | + return 0 |
| 110 | + self.__savefiledata__(f_name, self.get_text(0.0)) |
| 111 | + return 1 |
| 112 | + def new_file(self, *args, **kwargs): |
| 113 | + ## Clears the text widget |
| 114 | + dscn = tkmb.askyesnocancel('Save file?', 'Do you want to save the file already open?') #dscn = Desicion |
| 115 | + if dscn == True: #User said yes |
| 116 | + if self.save_file() == 0: |
| 117 | + self.new_file() |
| 118 | + return |
| 119 | + elif dscn == None: #User cancelled |
| 120 | + return |
| 121 | + #User has saved/pressed no |
| 122 | + self.clear_text(0.0, 'end') |
| 123 | + self.curr_file = None |
| 124 | + self.__updatetitle__() |
| 125 | + def config(self, **kwargs): |
| 126 | + #print(kwargs) |
| 127 | + self.widget_raw().config(kwargs) |
| 128 | + |
| 129 | + |
| 130 | +class MenuBar: |
| 131 | + def __init__(self, parent): |
| 132 | + ## Init |
| 133 | + self.parent = parent |
| 134 | + self.buttons = {} |
| 135 | + def add_button(self, button_name, command, text): |
| 136 | + ## Add a button |
| 137 | + button = tk.Button(self.parent, text=text, command=command) |
| 138 | + self.buttons[button_name] = {'cmd': command, 'text': text, 'raw': button} |
| 139 | + def pack_button(self, button_name, *args, **kwargs): |
| 140 | + ## Pack a button |
| 141 | + self.buttons[button_name]['raw'].pack(args, kwargs) |
| 142 | + def grid_button(self, button_name, *args, **kwargs): |
| 143 | + ## Grid a button |
| 144 | + self.buttons[button_name]['raw'].grid(kwargs) |
| 145 | + def raw_button(self, button_name): |
| 146 | + ## Returns the raw Tk button widget |
| 147 | + return self.buttons[button_name]['raw'] |
| 148 | + def config_button(self, button_name, *args, **kwargs): |
| 149 | + self.buttons[button_name]['raw'].config(args, kwargs) |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +version = '0.0.1.5' |
| 154 | + |
| 155 | +print('Starting Fedit', version) |
| 156 | +print('Home dir:', os.path.expanduser('~')) |
| 157 | + |
| 158 | +root_title = 'Fedit ('+str(version)+')' |
| 159 | +root = Window(root_title).window_raw() |
| 160 | + |
| 161 | +def set_root_title(title): |
| 162 | + root.title(title) |
| 163 | + root.update() |
| 164 | + |
| 165 | +## Main text editor |
| 166 | +frame_maintextedit = tk.Frame(root) |
| 167 | +widget_maintextedit = TextEditor(frame_maintextedit, root_title, set_root_title) |
| 168 | + |
| 169 | +## MenuBar |
| 170 | +frame_menubar = tk.Frame(root) |
| 171 | +menubar = MenuBar(frame_menubar) |
| 172 | + |
| 173 | +menubar.add_button('open', widget_maintextedit.open_file, 'Open') |
| 174 | +menubar.add_button('save', widget_maintextedit.save_file, 'Save') |
| 175 | +menubar.add_button('saveas', widget_maintextedit.saveas_file, 'Save As') |
| 176 | +menubar.add_button('new', widget_maintextedit.new_file, 'New') |
| 177 | + |
| 178 | +menubar.grid_button('open', row=0, column=0) |
| 179 | +menubar.grid_button('save', row=0, column=1) |
| 180 | +menubar.grid_button('saveas', row=0, column=2) |
| 181 | +menubar.grid_button('new', row=0, column=3) |
| 182 | + |
| 183 | +## Main text editor |
| 184 | +widget_maintextedit.pack(expand=1, fill='both') |
| 185 | + |
| 186 | +## Theme |
| 187 | +theme_current = False #False=Light True=Dark |
| 188 | +theme_light = '#f5f5f5' |
| 189 | +theme_dark = '#000000' |
| 190 | +def update_theme(theme, text_theme, custom=0): |
| 191 | + print('Custom:', custom, 'BG:', theme, 'FG:', text_theme) |
| 192 | + root.config(bg=theme) |
| 193 | + widget_maintextedit.config(bg=theme, fg=text_theme, insertbackground=text_theme) |
| 194 | + frame_menubar.config(bg=theme) |
| 195 | + for button in menubar.buttons: |
| 196 | + menubar.buttons[button]['raw'].config(bg=theme, fg=text_theme) |
| 197 | + |
| 198 | +def switch_theme(): |
| 199 | + global theme_current |
| 200 | + theme_current = not theme_current |
| 201 | + messages = {True: 'Switching theme to Dark', False: 'Switching theme to Light'} |
| 202 | + print(messages[theme_current]) |
| 203 | + if theme_current == True: |
| 204 | + update_theme(theme_dark, theme_light) |
| 205 | + else: |
| 206 | + update_theme(theme_light, theme_dark) |
| 207 | + root.update() |
| 208 | + |
| 209 | +def set_theme(): |
| 210 | + bg_col = colorchooser.askcolor(title='Choose background colour')[1] |
| 211 | + fg_col = colorchooser.askcolor(title='Choose foreground colour')[1] |
| 212 | + update_theme(bg_col, fg_col, custom=1) |
| 213 | + |
| 214 | +def prefs_window(): |
| 215 | + win = Window('Preferences', root_win=root).window_raw() |
| 216 | + button_switch_theme = tk.Button(win, text='Switch Theme', command=switch_theme) |
| 217 | + button_set_theme = tk.Button(win, text='Set Theme', command=set_theme) |
| 218 | + button_switch_theme.pack(fill='both', expand=1) |
| 219 | + button_set_theme.pack(fill='both', expand=1) |
| 220 | + |
| 221 | +menubar.add_button('prefs', prefs_window, 'Prefrences') |
| 222 | +update_theme(theme_light, theme_dark) |
| 223 | + |
| 224 | +menubar.grid_button('prefs', row=0, column=4) |
| 225 | + |
| 226 | +## Experimental |
| 227 | +changing_theme = [0, 0, 0] |
| 228 | +def changing_theme_func(): |
| 229 | + global changing_theme |
| 230 | + for i, item in enumerate(changing_theme): |
| 231 | + item += 1 |
| 232 | + if item > 255: |
| 233 | + item = 0 |
| 234 | + #changing_theme[i] = item |
| 235 | + else: |
| 236 | + changing_theme[i] = item |
| 237 | + break |
| 238 | + if changing_theme == [255,255,255]: |
| 239 | + changing_theme = [0,0,0] |
| 240 | + update_theme('#%02x%02x%02x' % tuple(changing_theme), '#000000') |
| 241 | + root.after(10, changing_theme_func) |
| 242 | +#changing_theme_func() |
| 243 | + |
| 244 | + |
| 245 | + |
| 246 | +## Packing |
| 247 | +frame_menubar.pack(side='top', expand=1, fill='both') |
| 248 | +frame_maintextedit.pack(expand=1, fill='both') |
| 249 | + |
| 250 | +## Start the window |
| 251 | +root.mainloop() |
| 252 | + |
| 253 | +## Exited |
| 254 | +print('Exiting') |
0 commit comments