How to do it...

We have already broken out all the names our GUI displays when we internationalized it in the previous recipe. That was an excellent start to refactoring our code.

Refactoring is the process of improving the structure, readability, and maintainability of the existing code. We are not adding new functionality.

In the previous chapters and recipes, we have been extending our GUI in a top-to-bottom waterfall development approach, adding import to the top and code towards the bottom of the existing code.

While this was useful when looking at the code, it now looks a little bit messy and we can improve this to help our future development.

Let's first clean up our import statement section, which currently looks as follows:

#====================== 
# imports
#======================
import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
from tkinter import Menu
from tkinter import Spinbox
import Ch08_Code.ToolTip as tt
from threading import Thread
from time import sleep
from queue import Queue
from tkinter import filedialog as fd
from os import path
from tkinter import messagebox as mBox
from Ch08_Code.LanguageResources import I18N
from datetime import datetime
from pytz import all_timezones, timezone

# Module level GLOBALS
GLOBAL_CONST = 42

By simply grouping related imports, we can reduce the number of lines of code, which improves the readability of our imports, making them appear less overwhelming:

#====================== 
# imports
#======================
import tkinter as tk
from tkinter import ttk, scrolledtext, Menu, Spinbox, filedialog as fd,
messagebox as mBox
from queue import Queue
from os import path
import Ch08_Code.ToolTip as tt
from Ch08_Code.LanguageResources import I18N
from Ch08_Code.Logger import Logger, LogLevel

# Module level GLOBALS
GLOBAL_CONST = 42

We can further refactor our code by breaking out the callback methods into their own modules. This improves readability by separating the different import statements into the modules they are required in.

Let's rename our GUI.py as GUI_Refactored.py and create a new module, which we name Callbacks_Refactored.py.

This gives us this new architecture:

#====================== 
# imports
#======================
import tkinter as tk
from tkinter import ttk, scrolledtext, Menu, Spinbox,
filedialog as fd, messagebox as mBox
from queue import Queue
from os import path
import Ch08_Code.ToolTip as tt
from Ch08_Code.LanguageResources import I18N
from Ch08_Code.Logger import Logger, LogLevel
from Ch08_Code.Callbacks_Refactored import Callbacks

# Module level GLOBALS
GLOBAL_CONST = 42

class OOP():
def __init__(self):
# Callback methods now in different module
self.callBacks = Callbacks(self)

Note how we are passing an instance of our own GUI class (self) when calling the Callbacks initializer.

Our new Callbacks class is as follows:

#====================== 
# imports
#======================
import tkinter as tk
from time import sleep
from threading import Thread
from pytz import all_timezones, timezone
from datetime import datetime

class Callbacks():
def __init__(self, oop):
self.oop = oop

def defaultFileEntries(self):
self.oop.fileEntry.delete(0, tk.END)
self.oop.fileEntry.insert(0, 'Z:') # bogus path
self.oop.fileEntry.config(state='readonly')
self.oop.netwEntry.delete(0, tk.END)
self.oop.netwEntry.insert(0, 'Z:Backup') # bogus path

# Combobox callback
def _combo(self, val=0):
value = self.oop.combo.get()
self.oop.scr.insert(tk.INSERT, value + ' ')

In the initializer of our new class, the passed-in GUI instance is saved under the name self.oop and used throughout this new Python class module.

Running the refactored GUI code still works. We have only increased its readability and reduced the complexity of our code in preparation for further development work.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.87.161