patternpythonMinor
Yet another Python text editor
Viewed 0 times
texteditoryetpythonanother
Problem
Here's what I managed to do. It is rather limited and I had to get help from the internet in some places.
Can you review it, give me some tips on making the code better, cleaner, faster etc.?
editor.py
```
import tkinter
import tkinter.ttk
import tkinter.filedialog
import tkinter.messagebox
import os
try:
import config
import menu
import syntaxhl
import findtool
except ImportError as e:
import sys
sys.exit("Import error.\nRaw: {}".format(e))
class Editor(object):
def editor(self):
self.root = tkinter.Tk()
self.config = config.config()
self.syntaxcolor = config.syntaxHighligh()
self.root.wm_title(self.config.editor_title)
self.root.attributes("-alpha", self.config.transparency)
# Scrollbar
self.scrollbar = tkinter.Scrollbar(self.root)
self.scrollbar.pack(side = tkinter.RIGHT, fill = tkinter.Y)
# Editor.
self.editor_space = tkinter.Text(self.root)
self.editor_space.config(undo = self.config.undo, width = self.config.width,
height = self.config.height, fg = self.config.foreground,
bg = self.config.background, insertbackground = self.config.cursorColor,
yscrollcommand = self.scrollbar.set,
font = (self.config.font, self.config.font_size))
self.editor_space.pack(fill = tkinter.X)
self.scrollbar.config(command = self.editor_space.yview)
# Find tool
find = findtool.Find(self.editor_space)
# Menu.
# Note: The order of the instances is the order in witch the menus will appear.
self.mn = tkinter.Menu(self.root)
self.filemenu = menu.FileMenu(self.mn, self.openFile,
self.saveFile, self.destroy,
self.root)
self.toolmenu = menu.ToolsMenu(self.mn, self.clear,
Can you review it, give me some tips on making the code better, cleaner, faster etc.?
editor.py
```
import tkinter
import tkinter.ttk
import tkinter.filedialog
import tkinter.messagebox
import os
try:
import config
import menu
import syntaxhl
import findtool
except ImportError as e:
import sys
sys.exit("Import error.\nRaw: {}".format(e))
class Editor(object):
def editor(self):
self.root = tkinter.Tk()
self.config = config.config()
self.syntaxcolor = config.syntaxHighligh()
self.root.wm_title(self.config.editor_title)
self.root.attributes("-alpha", self.config.transparency)
# Scrollbar
self.scrollbar = tkinter.Scrollbar(self.root)
self.scrollbar.pack(side = tkinter.RIGHT, fill = tkinter.Y)
# Editor.
self.editor_space = tkinter.Text(self.root)
self.editor_space.config(undo = self.config.undo, width = self.config.width,
height = self.config.height, fg = self.config.foreground,
bg = self.config.background, insertbackground = self.config.cursorColor,
yscrollcommand = self.scrollbar.set,
font = (self.config.font, self.config.font_size))
self.editor_space.pack(fill = tkinter.X)
self.scrollbar.config(command = self.editor_space.yview)
# Find tool
find = findtool.Find(self.editor_space)
# Menu.
# Note: The order of the instances is the order in witch the menus will appear.
self.mn = tkinter.Menu(self.root)
self.filemenu = menu.FileMenu(self.mn, self.openFile,
self.saveFile, self.destroy,
self.root)
self.toolmenu = menu.ToolsMenu(self.mn, self.clear,
Solution
-
As mentioned by @L3viathan, the
-
Secondly, your naming is off. Functions and variables should be in
As mentioned by @L3viathan, the
config class should be a dictionary, not a static class of attributes. When you ever need to store values in such a way, that a grouped together under a category, use a dictionary, like this:CONFIG = {
"config-attribute": "config-attribute-value"
...
}-
Secondly, your naming is off. Functions and variables should be in
lower_snake_case, and classes should be in PascalCase. If the variable is constant, it should be in UPPERCASE_SNAKE_CASE.- I'm not sure if you're using Python 3.x, or Python 2.x, but if you're using Python 3.x, you don't need to explicitly inherit all your classes from
object. You can just write them like this:class MyClass:. If you're using Python 2.x though, you should continue explicitly inheriting fromobject.
- You're mixing use of single quotes,
'', and double quotes,"". Preferably, you should choose one, and stick with that.
- You need two blank spaces in between your top level functions/classes. This is part of the official style guide.
- Finally, add some comments. Your code is fairly readable, but it could definitely use some comments. First off, add some docstrings,
""" ... """, to describe what your functions and classes do. To describe blocks of code, just use online comments,# ....
Code Snippets
CONFIG = {
"config-attribute": "config-attribute-value"
...
}Context
StackExchange Code Review Q#94181, answer score: 6
Revisions (0)
No revisions yet.