patternpythonMinor
Basic text editor in Python with Tkinter
Viewed 0 times
withtexttkintereditorpythonbasic
Problem
I have finished writing my nice and little text editor in Python with Tkinter.
I tried to achieve the best user experience that I could.
-
I want this code to be 2/3 compatible, I tested it in both versions but if you could test it out again in the 2 versions too it would very knd of you.
-
I did not use a class on purpose because I think that there is no need for it in such a small project (
The code:
```
try:
import tkinter as tk
except:
import Tkinter as tk
try:
import tkMessageBox as pop_up
except:
import tkinter.tkMessageBox as pop_up
import time
EMPTY_TITLE_ERROR_MESSAGE_SAVE = "Please write the name of the file you want to save in the given field."
EMPTY_TITLE_ERROR_MESSAGE_OPEN = "Please write the name of the file you want to open in the given field."
FILE_NOT_FOUND_ERROR_MESSAGE = "No file with the given title was found, remember that this text editor can only read files in its directory."
SAVING_SUCCESS_MESSAGE = "Your text is now stored in the {filename} file"
SIGNATURE_TXT_NOT_FOUND_MESSAGE = "Please be sure that the file you want to open exists and that it is in the same folder of this editor."
def _open():
if not file_title.get():
pop_up.showerror("Title is empty.",EMPTY_TITLE_ERROR_MESSAGE_OPEN)
return 1
if not ".txt" in file_title.get():
filename = file_title.get() + ".txt"
try:
with open(filename) as f:
main_text.delete("1.0",tk.END)
main_text.insert(tk.INSERT, f.read(), "a")
except IOError:
pop_up.showerror("File not found.",FILE_NOT_FOUND_ERROR_MESSAGE)
def save():
if not file_title.get():
pop_up.showerror("No tit
I tried to achieve the best user experience that I could.
-
I want this code to be 2/3 compatible, I tested it in both versions but if you could test it out again in the 2 versions too it would very knd of you.
-
I did not use a class on purpose because I think that there is no need for it in such a small project (
- Is the user going to use this without problems?
- Is the code going to be readable if I look back at it after, let's say, a year?
- Do I use good names?
- I did not use a
__name__guard because this file is not meant to be imported, do you think I should use it anyway?
- What other features could I implement?
The code:
```
try:
import tkinter as tk
except:
import Tkinter as tk
try:
import tkMessageBox as pop_up
except:
import tkinter.tkMessageBox as pop_up
import time
EMPTY_TITLE_ERROR_MESSAGE_SAVE = "Please write the name of the file you want to save in the given field."
EMPTY_TITLE_ERROR_MESSAGE_OPEN = "Please write the name of the file you want to open in the given field."
FILE_NOT_FOUND_ERROR_MESSAGE = "No file with the given title was found, remember that this text editor can only read files in its directory."
SAVING_SUCCESS_MESSAGE = "Your text is now stored in the {filename} file"
SIGNATURE_TXT_NOT_FOUND_MESSAGE = "Please be sure that the file you want to open exists and that it is in the same folder of this editor."
def _open():
if not file_title.get():
pop_up.showerror("Title is empty.",EMPTY_TITLE_ERROR_MESSAGE_OPEN)
return 1
if not ".txt" in file_title.get():
filename = file_title.get() + ".txt"
try:
with open(filename) as f:
main_text.delete("1.0",tk.END)
main_text.insert(tk.INSERT, f.read(), "a")
except IOError:
pop_up.showerror("File not found.",FILE_NOT_FOUND_ERROR_MESSAGE)
def save():
if not file_title.get():
pop_up.showerror("No tit
Solution
Style
Python has a guide style called PEP 8. If you have no good reason not to follow it, just follow it. You'll find various tools to check your code (pep8, pep8online.com/, etc) and even to fix it more or less automatically (autopep8).
Here's a list of problems detected :
And here's you code after most of the problems are fixed :
As I am talking about tools, it is probably worth mentionning that you'll find various other tools to check your code.
pylint finds :
```
*** Module txt
C: 13, 0: Line too long (105/80) (line-too-long)
C: 14, 0: Line too long (105/80) (line-too-long
Python has a guide style called PEP 8. If you have no good reason not to follow it, just follow it. You'll find various tools to check your code (pep8, pep8online.com/, etc) and even to fix it more or less automatically (autopep8).
Here's a list of problems detected :
txt_original.py:13:80: E501 line too long (105 > 79 characters)
txt_original.py:14:80: E501 line too long (105 > 79 characters)
txt_original.py:15:80: E501 line too long (141 > 79 characters)
txt_original.py:17:80: E501 line too long (138 > 79 characters)
txt_original.py:22:43: E231 missing whitespace after ','
txt_original.py:25:8: E713 test for membership should be 'not in'
txt_original.py:30:35: E231 missing whitespace after ','
txt_original.py:33:43: E231 missing whitespace after ','
txt_original.py:35:1: E302 expected 2 blank lines, found 1
txt_original.py:37:37: E231 missing whitespace after ','
txt_original.py:40:8: E713 test for membership should be 'not in'
txt_original.py:43:23: E231 missing whitespace after ','
txt_original.py:44:34: E231 missing whitespace after ','
txt_original.py:45:46: E231 missing whitespace after ','
txt_original.py:45:80: E501 line too long (95 > 79 characters)
txt_original.py:47:1: E302 expected 2 blank lines, found 1
txt_original.py:55:1: E302 expected 2 blank lines, found 1
txt_original.py:61:56: E231 missing whitespace after ','
txt_original.py:61:65: W291 trailing whitespace
txt_original.py:68:42: E231 missing whitespace after ','
txt_original.py:69:37: E231 missing whitespace after ','
txt_original.py:75:21: E231 missing whitespace after ','
txt_original.py:76:14: E251 unexpected spaces around keyword / parameter equals
txt_original.py:76:16: E251 unexpected spaces around keyword / parameter equals
txt_original.py:76:20: E231 missing whitespace after ','
txt_original.py:79:20: E251 unexpected spaces around keyword / parameter equals
txt_original.py:79:22: E251 unexpected spaces around keyword / parameter equals
txt_original.py:79:26: E231 missing whitespace after ','And here's you code after most of the problems are fixed :
try:
import tkinter as tk
except:
import Tkinter as tk
try:
import tkMessageBox as pop_up
except:
import tkinter.tkMessageBox as pop_up
import time
EMPTY_TITLE_ERROR_MESSAGE_SAVE = "Please write the name of the file you want to save in the given field."
EMPTY_TITLE_ERROR_MESSAGE_OPEN = "Please write the name of the file you want to open in the given field."
FILE_NOT_FOUND_ERROR_MESSAGE = "No file with the given title was found, remember that this text editor can only read files in its directory."
SAVING_SUCCESS_MESSAGE = "Your text is now stored in the {filename} file"
SIGNATURE_TXT_NOT_FOUND_MESSAGE = "Please be sure that the file you want to open exists and that it is in the same folder of this editor."
def _open():
if not file_title.get():
pop_up.showerror("Title is empty.", EMPTY_TITLE_ERROR_MESSAGE_OPEN)
return 1
if ".txt" not in file_title.get():
filename = file_title.get() + ".txt"
try:
with open(filename) as f:
main_text.delete("1.0", tk.END)
main_text.insert(tk.INSERT, f.read(), "a")
except IOError:
pop_up.showerror("File not found.", FILE_NOT_FOUND_ERROR_MESSAGE)
def save():
if not file_title.get():
pop_up.showerror("No title.", EMPTY_TITLE_ERROR_MESSAGE_SAVE)
return 1
if ".txt" not in file_title.get():
filename = file_title.get() + ".txt"
with open(filename, "w+") as f:
f.write(main_text.get(1.0, tk.END))
pop_up.showinfo(
"File saved succesfully.",
SAVING_SUCCESS_MESSAGE.format(
filename=filename))
def add_date():
full_date = time.localtime()
day = str(full_date.tm_mday)
month = str(full_date.tm_mon)
year = str(full_date.tm_year)
date = "\n" + day + '/' + month + '/' + year
main_text.insert(tk.INSERT, date, "a")
def add_signature():
try:
with open("signature.txt") as f:
main_text.insert(tk.INSERT, "\n" + f.read(), "a")
except IOError:
MESSAGE = SIGNATURE_TXT_NOT_FOUND_MESSAGE
pop_up.showerror("\"signature.txt\" not found.", MESSAGE)
root = tk.Tk()
menubar = tk.Menu(root)
menubar.add_command(label="Open", command=_open)
menubar.add_command(label="Save", command=save)
menubar.add_command(label="Add signature", command=add_signature)
menubar.add_command(label="Add date", command=add_date)
root.config(menu=menubar)
top = tk.Frame(root)
temp = tk.Label(root, text="Title:")
temp.pack(in_=top, side=tk.LEFT)
file_title = tk.Entry(root)
file_title.pack(in_=top, side=tk.RIGHT)
top.pack()
main_text = tk.Text(root)
main_text.pack()
tk.mainloop()As I am talking about tools, it is probably worth mentionning that you'll find various other tools to check your code.
pylint finds :
```
*** Module txt
C: 13, 0: Line too long (105/80) (line-too-long)
C: 14, 0: Line too long (105/80) (line-too-long
Code Snippets
txt_original.py:13:80: E501 line too long (105 > 79 characters)
txt_original.py:14:80: E501 line too long (105 > 79 characters)
txt_original.py:15:80: E501 line too long (141 > 79 characters)
txt_original.py:17:80: E501 line too long (138 > 79 characters)
txt_original.py:22:43: E231 missing whitespace after ','
txt_original.py:25:8: E713 test for membership should be 'not in'
txt_original.py:30:35: E231 missing whitespace after ','
txt_original.py:33:43: E231 missing whitespace after ','
txt_original.py:35:1: E302 expected 2 blank lines, found 1
txt_original.py:37:37: E231 missing whitespace after ','
txt_original.py:40:8: E713 test for membership should be 'not in'
txt_original.py:43:23: E231 missing whitespace after ','
txt_original.py:44:34: E231 missing whitespace after ','
txt_original.py:45:46: E231 missing whitespace after ','
txt_original.py:45:80: E501 line too long (95 > 79 characters)
txt_original.py:47:1: E302 expected 2 blank lines, found 1
txt_original.py:55:1: E302 expected 2 blank lines, found 1
txt_original.py:61:56: E231 missing whitespace after ','
txt_original.py:61:65: W291 trailing whitespace
txt_original.py:68:42: E231 missing whitespace after ','
txt_original.py:69:37: E231 missing whitespace after ','
txt_original.py:75:21: E231 missing whitespace after ','
txt_original.py:76:14: E251 unexpected spaces around keyword / parameter equals
txt_original.py:76:16: E251 unexpected spaces around keyword / parameter equals
txt_original.py:76:20: E231 missing whitespace after ','
txt_original.py:79:20: E251 unexpected spaces around keyword / parameter equals
txt_original.py:79:22: E251 unexpected spaces around keyword / parameter equals
txt_original.py:79:26: E231 missing whitespace after ','try:
import tkinter as tk
except:
import Tkinter as tk
try:
import tkMessageBox as pop_up
except:
import tkinter.tkMessageBox as pop_up
import time
EMPTY_TITLE_ERROR_MESSAGE_SAVE = "Please write the name of the file you want to save in the given field."
EMPTY_TITLE_ERROR_MESSAGE_OPEN = "Please write the name of the file you want to open in the given field."
FILE_NOT_FOUND_ERROR_MESSAGE = "No file with the given title was found, remember that this text editor can only read files in its directory."
SAVING_SUCCESS_MESSAGE = "Your text is now stored in the {filename} file"
SIGNATURE_TXT_NOT_FOUND_MESSAGE = "Please be sure that the file you want to open exists and that it is in the same folder of this editor."
def _open():
if not file_title.get():
pop_up.showerror("Title is empty.", EMPTY_TITLE_ERROR_MESSAGE_OPEN)
return 1
if ".txt" not in file_title.get():
filename = file_title.get() + ".txt"
try:
with open(filename) as f:
main_text.delete("1.0", tk.END)
main_text.insert(tk.INSERT, f.read(), "a")
except IOError:
pop_up.showerror("File not found.", FILE_NOT_FOUND_ERROR_MESSAGE)
def save():
if not file_title.get():
pop_up.showerror("No title.", EMPTY_TITLE_ERROR_MESSAGE_SAVE)
return 1
if ".txt" not in file_title.get():
filename = file_title.get() + ".txt"
with open(filename, "w+") as f:
f.write(main_text.get(1.0, tk.END))
pop_up.showinfo(
"File saved succesfully.",
SAVING_SUCCESS_MESSAGE.format(
filename=filename))
def add_date():
full_date = time.localtime()
day = str(full_date.tm_mday)
month = str(full_date.tm_mon)
year = str(full_date.tm_year)
date = "\n" + day + '/' + month + '/' + year
main_text.insert(tk.INSERT, date, "a")
def add_signature():
try:
with open("signature.txt") as f:
main_text.insert(tk.INSERT, "\n" + f.read(), "a")
except IOError:
MESSAGE = SIGNATURE_TXT_NOT_FOUND_MESSAGE
pop_up.showerror("\"signature.txt\" not found.", MESSAGE)
root = tk.Tk()
menubar = tk.Menu(root)
menubar.add_command(label="Open", command=_open)
menubar.add_command(label="Save", command=save)
menubar.add_command(label="Add signature", command=add_signature)
menubar.add_command(label="Add date", command=add_date)
root.config(menu=menubar)
top = tk.Frame(root)
temp = tk.Label(root, text="Title:")
temp.pack(in_=top, side=tk.LEFT)
file_title = tk.Entry(root)
file_title.pack(in_=top, side=tk.RIGHT)
top.pack()
main_text = tk.Text(root)
main_text.pack()
tk.mainloop()************* Module txt
C: 13, 0: Line too long (105/80) (line-too-long)
C: 14, 0: Line too long (105/80) (line-too-long)
C: 15, 0: Line too long (141/80) (line-too-long)
C: 17, 0: Line too long (138/80) (line-too-long)
C: 1, 0: Missing module docstring (missing-docstring)
W: 3, 0: No exception type(s) specified (bare-except)
F: 4, 4: Unable to import 'Tkinter' (import-error)
W: 8, 0: No exception type(s) specified (bare-except)
F: 7, 4: Unable to import 'tkMessageBox' (import-error)
E: 9, 4: No name 'tkMessageBox' in module 'tkinter' (no-name-in-module)
F: 9, 4: Unable to import 'tkinter.tkMessageBox' (import-error)
C: 20, 0: Missing function docstring (missing-docstring)
C: 29,31: Invalid variable name "f" (invalid-name)
C: 36, 0: Missing function docstring (missing-docstring)
C: 44,33: Invalid variable name "f" (invalid-name)
C: 52, 0: Missing function docstring (missing-docstring)
C: 61, 0: Missing function docstring (missing-docstring)
C: 63,38: Invalid variable name "f" (invalid-name)
C: 66, 8: Invalid variable name "MESSAGE" (invalid-name)
C: 69, 0: Invalid constant name "root" (invalid-name)
C: 71, 0: Invalid constant name "menubar" (invalid-name)
C: 80, 0: Invalid constant name "top" (invalid-name)
C: 81, 0: Invalid constant name "temp" (invalid-name)
C: 84, 0: Invalid constant name "file_title" (invalid-name)
C: 89, 0: Invalid constant name "main_text" (invalid-name)if not ".txt" in file_title.get():
filename = file_title.get() + ".txt"Context
StackExchange Code Review Q#70216, answer score: 5
Revisions (0)
No revisions yet.