Linux/Windows Text Editor in Tkinter 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?
3.x Compatibility

If you want to make this compatible with Python 3.x, you'd have to do a few things. First off, in Python 3.x, Tkinter has been renamed to tkinter, so at the top of your file you might want to do something like this:

try:
    import tkinter as tk
except ImportError:
    import Tkinter as tk


You should also use parentheses in print, like this:

print( ... )




Style and other nitpicks

Generally, you shouldn't be using wildcard imports like this:

from Tkinter import *


If you don't like prefixing everything with Tkinter, you can just do this:

import Tkinter as tk


Rather than importing multiple modules on the same line, like this:

import sys, os


It's better to import them all on separate lines, like this:

import sys
import os


It's also very bad practice to have a bare except clause, like this:

try:
    ...
except:
    ...


Any program error that you don't want to catch will go un-noticed . This is not good. You should be doing something like this instead:

try:
    ...
except TheErrorYouWantToCatch:
    ...


You should also have two blank lines between functions, not one, like this:

def a():
    ...


def b():
    ...


On the note of functions, you should also have docstrings describing the purpose of your functions. A typical docstring looks something like this:

def my_function( ... ):
    """Brief description.

    More detailed description.

    Description of arguments.
    """
    ...


Your naming is also, not the greatest. For example, I'd rename the following variables:


file_input to input_file_path.
_FILE_ to file_contents.