Over the past couple of days I have been working on a super-basic text editor with Tkinter.
It can:
- Open new files
- Open files (general)
- Save files
Obviously you can edit any file you open or make. This is very similar to this question, though I did not see that until I checked for duplicates of this question.
I did not use classes.
from Tkinter import *
import sys, os
import tkMessageBox
import error_mes
main = Tk()
#Variables that are globally needed
file_input = "" #whats put into the text box
_FILE_= "" #File the user wants to open; readapt to be synonymous with save?
open_a_file = "" #will be the entry field for opening a file
target = ""
new_file_ = ""
new_file_name = ""
isnewfile = "no"
def get_from_text():
global file_input
try:
file_input = my_text_box.get("1.0", END)
print file_input
except:
file_input = 'UHOH'
print file_input
def save(): #This function can definitely be improved
global file_input, target, _FILE_, my_text_box, new_file_name
try:
file_input = my_text_box.get("1.0", END)
target = open(_FILE_, "r+w")
target.truncate()
target.write(file_input)
except:
file_input = my_text_box.get("1.0", END)
target = open(new_file_name, "r+w")
target.truncate()
target.write(file_input)
def exit_application():
sys.exit(0)
def menu_open_file():
global _FILE_, open_a_file, save, my_text_box
try:
open_a_file = Entry()
open_a_file.grid(row = 3, column = 0)
open_a_file.insert(0, "Path to File to Open")
#save.grid_forget()
Button(main, text = "Click to Open", command = get_file).grid(row = 4,
column = 0)
except:
error_mes.error()
def get_file():
global _FILE_, open_a_file, my_text_box
try:
_FILE_ = open_a_file.get()
target = open(_FILE_, "r+w")
opened_file = target.read()
try:
my_text_box.insert(INSERT, opened_file)
except:
error_mes.error()
except:
error_mes.error()
def new_file():
global new_file_, my_text_box
my_text_box.delete("1.0", END)
try:
new_file_ = Entry()
new_file_.grid(row = 3, column = 0)
Button(main, text = "Click to Save", command = save_new_file).grid(row = 4,
column = 0)
except:
error_mes.error()
def save_new_file():
global new_file_, new_file_name, my_text_box, target
new_file_name = new_file_.get()
target = open(new_file_name, "w")
target.write(my_text_box.get("1.0", END))
my_text_box = Text(main, bg = "black", fg = "white", insertbackground = "white",
tabs = ("1c"))
my_text_box.grid(row = 0, column = 0)
#The Menu
menu = Menu(main)
main.config(menu = menu)
filemenu = Menu(menu)
menu.add_cascade(label = "File", menu = filemenu)
filemenu.add_command(label = "New...", command = new_file)
filemenu.add_command(label = "Open...", command = menu_open_file)
filemenu.add_command(label = "Save", command = save)
filemenu.add_separator()
filemenu.add_command(label = "Exit", command = exit_application)
main.mainloop()
This code works fine in Linux, but when testing in Windows, the save command throws an error for some reason, but aside from that it works fine in both OSes.
My questions are:
- How can my style be improved?
- Is the code understandable? Did I use acceptable variable and function names?
- Building off of that, are the
FILE, FILE_, new_file
etc, variables confusing? - Are classes necessary with a small program like this? Should I think about rewriting the code using object oriented programming if I want to add more features (such as syntax highlighting, python console, preset formats, etc)
- Any suggested edits that would make my code either easier to read or more efficient?