HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Linux/Windows Text Editor in Tkinter

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
texttkintereditorwindowslinux

Problem

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 = tar

Solution

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.

Code Snippets

try:
    import tkinter as tk
except ImportError:
    import Tkinter as tk
print( ... )
from Tkinter import *
import Tkinter as tk
import sys, os

Context

StackExchange Code Review Q#98007, answer score: 2

Revisions (0)

No revisions yet.