Skip to content

Instantly share code, notes, and snippets.

@thinkallabout
Created May 28, 2014 11:48
Show Gist options
  • Save thinkallabout/736feb91037bcf070405 to your computer and use it in GitHub Desktop.
Save thinkallabout/736feb91037bcf070405 to your computer and use it in GitHub Desktop.
Basic text editor WIP
#!/usr/bin/env python
# encoding: utf-8
import os
import tkinter
from tkinter.filedialog import *
from tkinter.messagebox import *
from tkinter.ttk import *
def donothing():
pass
class editor():
""" Primary editor functions """
def create_window(self):
self.root = Tk()
self.root.geometry('900x450')
self.root.wm_state('normal')
self.root.title('Argon') # Window title
self.create_text()
self.create_label()
def create_text(self):
self.editor = Text(self.root)
self.editor.config(width=500, wrap=WORD, bg='#222222',
fg='#eeeeee', padx=5,pady=5, insertbackground='#eeeeee',
highlightthickness=0)
self.editor.pack(fill=Y, expand=1)
def create_label(self):
self.status = Label(self.root)
self.status.config(text='New File', anchor=W)
self.status.pack(expand=0, fill=X)
def create_menu(self):
self.menu_bar = Menu(self.root)
self.menu_bar.config(bg='#f9f9f9', borderwidth=0)
def create_menu(label, layout):
menu = Menu(self.menu_bar, tearoff=0)
self.menu_bar.add_cascade(label=label, menu=menu)
for i in layout:
sub_label = i[0]
command = i[1]
menu.add_command(label=sub_label, command=command)
create_menu("File", [["New", self.new_file],
["Open", self.open_file],
["Save", self.save_file],
["Save as..", self.save_file],
["Exit", self.root.quit]])
self.root.config(menu=self.menu_bar)
def open_file(self):
path = askopenfilename()
the_file = open(path, 'r')
content = str(the_file.read())
the_file.close()
self.path = path
self.status.config(text=path)
self.root.update_idletasks()
# Clear in case file is already loaded
self.editor.delete('1.0', 'end')
self.editor.insert('1.0', content)
def save_file(self):
the_file = open(self.path, 'w')
the_file.write(self.editor.get('1.0', 'end'))
the_file.close()
def save_file_as(self):
path = asksaveasfilename()
the_file = open(path, 'w')
the_file.write(self.editor.get('1.0', 'end'))
the_file.close()
def new_file(self):
editor = self.editor.get('1.0', 'end')
print(len(editor))
if len(editor) > 1:
answer = askokcancel('Are you sure?',
'Discard current changes?')
if answer == True:
self.editor.delete('1.0', 'end')
self.status.config(text = 'New File')
def __init__(self):
self.path = None
self.create_window()
self.create_menu()
app = editor()
if __name__ == '__main__':
app.root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment