1+ ## Import modules
12import tkinter as tk
23from tkinter import filedialog as tkfd
34import os
45
6+
7+ ## Classes
8+ def verify_f_name (f_name ):
9+ if f_name in [None , (), '' ]:
10+ return False
11+ return True
12+
513root_window = False
614class Window :
715 def __init__ (self , title ):
@@ -17,13 +25,9 @@ def window_raw(self):
1725 return self .win
1826
1927
20- def verify_f_name (f_name ):
21- if f_name in [None , (), '' ]:
22- return False
23- return True
24-
2528class TextEditor :
2629 def __init__ (self , parent ):
30+ ## Init
2731 self .parent = parent
2832 self .widget = tk .Text (self .parent )
2933 self .ftypes = [
@@ -36,20 +40,27 @@ def __init__(self, parent):
3640 ]
3741 self .initial_dir = os .path .expanduser ('~' )
3842 def widget_raw (self ):
43+ ## Return the raw Tk text widget
3944 return self .widget
4045 def pack (self , * args , ** kwargs ):
46+ ## Pack the text widget
4147 self .widget .pack (kwargs )
4248 def grid (self , * args , ** kwargs ):
49+ ## Grid the text widget
4350 self .widget .grid (kwargs )
4451 def clear_text (self , * args , ** kwargs ):
52+ ## Clear text from text widget
4553 self .widget .delete (* args , ** kwargs )
4654 def set_text (self , text , * args , ** kwargs ):
55+ ## Clears and sets the text
4756 self .clear_text (0.0 )
4857 self .widget .insert (0.0 , text , args , kwargs )
4958 def get_text (self , * args , ** kwargs ):
59+ ## Gets the text from the text widget
5060 text = self .widget .get (* args , ** kwargs )
5161 return text
5262 def open_file (self , * args , ** kwargs ):
63+ ## Asks for a file to open and shows it in text widget
5364 #args and kwargs to this function are thrown away
5465 global initial_dir
5566 f_name = tkfd .askopenfilename (filetypes = self .ftypes , initialdir = self .initial_dir )
@@ -60,6 +71,8 @@ def open_file(self, *args, **kwargs):
6071 f_text = open (f_name , 'r' ).read ()
6172 self .set_text (text = f_text )
6273 def save_file (self , * args , ** kwargs ):
74+ ## Asks for a file to save the contents of the text widget
75+ ## in and saves it there
6376 #args and kwargs to this function are thrown away
6477 global initial_dir
6578 f_name = tkfd .asksaveasfilename (filetypes = self .ftypes , initialdir = self .initial_dir )
@@ -74,16 +87,21 @@ def save_file(self, *args, **kwargs):
7487
7588class MenuBar :
7689 def __init__ (self , parent ):
90+ ## Init
7791 self .parent = parent
7892 self .buttons = {}
7993 def add_button (self , button_name , command , text ):
94+ ## Add a button
8095 button = tk .Button (self .parent , text = text , command = command )
8196 self .buttons [button_name ] = {'cmd' : command , 'text' : text , 'raw' : button }
8297 def pack_button (self , button_name , * args , ** kwargs ):
98+ ## Pack a button
8399 self .buttons [button_name ]['raw' ].pack (args , kwargs )
84100 def grid_button (self , button_name , * args , ** kwargs ):
101+ ## Grid a button
85102 self .buttons [button_name ]['raw' ].grid (kwargs )
86103 def raw_button (self , button_name ):
104+ ## Returns the raw Tk button widget
87105 return self .buttons [button_name ]['raw' ]
88106
89107
0 commit comments