1- #!/usr/bin/env python3
2- ## Import modules
3- import tkinter as tk
4- from tkinter import filedialog as tkfd
5- from tkinter import messagebox as tkmb
6- import os
1+ import tktools
2+ from tktools import os , tk
73from tkinter import colorchooser
84
9-
10- ## Classes
11- def verify_f_name (f_name ):
12- if f_name in [None , (), '' ]:
13- return False
14- return True
15-
16- root_window = False
17- root_window_obj = None
18- class Window :
19- def __init__ (self , title , root_win = None ):
20- global root_window , root_window_obj
21- self .title = title
22- if root_window == False :
23- self .win = tk .Tk ()
24- root_window_obj = self .win
25- else :
26- self .win = tk .Toplevel ()
27- self .win .title (self .title )
28-
29- def window_raw (self ):
30- return self .win
31-
32-
33- class TextEditor :
34- def __init__ (self , parent , root_title , set_root_title ):
35- ## Init
36- self .parent = parent
37- self .widget = tk .Text (self .parent )
38- self .ftypes = [
39- ('All files' , '*' ),
40- ('Python code files' , '*.py' ),
41- ('Perl code files' , '*.pl;*.pm' ), # semicolon trick
42- ('Java code files' , '*.java' ),
43- ('C++ code files' , '*.cpp;*.h' ), # semicolon trick
44- ('Text files' , '*.txt' ),
45- ]
46- self .initial_dir = os .path .expanduser ('~' )
47- self .curr_file = None
48- self .set_root_title = set_root_title
49- self .root_title = root_title
50- def __savefiledata__ (self , f_name , data ):
51- print ('Saving' , f_name )
52- self .initial_dir = os .path .dirname (f_name )
53- f_obj = open (f_name , 'w' )
54- f_obj .write (data )
55- f_obj .close ()
56- self .curr_file = f_name
57- def __updatetitle__ (self , f_name = None ):
58- if f_name == None :
59- f_name = self .curr_file
60- if f_name == None :
61- self .set_root_title (self .root_title )
62- else :
63- self .set_root_title (self .root_title + ' - ' + f_name )
64- def widget_raw (self ):
65- ## Return the raw Tk text widget
66- return self .widget
67- def pack (self , * args , ** kwargs ):
68- ## Pack the text widget
69- self .widget .pack (kwargs )
70- def grid (self , * args , ** kwargs ):
71- ## Grid the text widget
72- self .widget .grid (kwargs )
73- def clear_text (self , * args , ** kwargs ):
74- ## Clear text from text widget
75- self .widget .delete (* args , ** kwargs )
76- def set_text (self , text , * args , ** kwargs ):
77- ## Clears and sets the text
78- self .clear_text (0.0 )
79- self .widget .insert (0.0 , text , args , kwargs )
80- def get_text (self , * args , ** kwargs ):
81- ## Gets the text from the text widget
82- text = self .widget .get (* args , ** kwargs )
83- return text
84- def open_file (self , * args , ** kwargs ):
85- ## Asks for a file to open and shows it in text widget
86- #args and kwargs to this function are thrown away
87- f_name = tkfd .askopenfilename (filetypes = self .ftypes , initialdir = self .initial_dir )
88- if not verify_f_name (f_name ):
89- return
90- print ('Opening' , f_name )
91- self .initial_dir = os .path .dirname (f_name )
92- f_text = open (f_name , 'r' ).read ()
93- self .set_text (text = f_text )
94- self .curr_file = f_name
95- self .__updatetitle__ ()
96- def save_file (self , * args , ** kwargs ):
97- if verify_f_name (self .curr_file ):
98- #Save file as currently open file
99- self .__savefiledata__ (self .curr_file , self .get_text (0.0 ))
100- else :
101- #No file currently open
102- self .saveas_file ()
103- def saveas_file (self , * args , ** kwargs ):
104- ## Asks for a file to save the contents of the text widget
105- ## in and saves it there
106- #args and kwargs to this function are thrown away
107- f_name = tkfd .asksaveasfilename (filetypes = self .ftypes , initialdir = self .initial_dir )
108- if not verify_f_name (f_name ):
109- return 0
110- self .__savefiledata__ (f_name , self .get_text (0.0 ))
111- return 1
112- def new_file (self , * args , ** kwargs ):
113- ## Clears the text widget
114- dscn = tkmb .askyesnocancel ('Save file?' , 'Do you want to save the file already open?' ) #dscn = Desicion
115- if dscn == True : #User said yes
116- if self .save_file () == 0 :
117- self .new_file ()
118- return
119- elif dscn == None : #User cancelled
120- return
121- #User has saved/pressed no
122- self .clear_text (0.0 , 'end' )
123- self .curr_file = None
124- self .__updatetitle__ ()
125- def config (self , ** kwargs ):
126- #print(kwargs)
127- self .widget_raw ().config (kwargs )
128-
129-
130- class MenuBar :
131- def __init__ (self , parent ):
132- ## Init
133- self .parent = parent
134- self .buttons = {}
135- def add_button (self , button_name , command , text ):
136- ## Add a button
137- button = tk .Button (self .parent , text = text , command = command )
138- self .buttons [button_name ] = {'cmd' : command , 'text' : text , 'raw' : button }
139- def pack_button (self , button_name , * args , ** kwargs ):
140- ## Pack a button
141- self .buttons [button_name ]['raw' ].pack (args , kwargs )
142- def grid_button (self , button_name , * args , ** kwargs ):
143- ## Grid a button
144- self .buttons [button_name ]['raw' ].grid (kwargs )
145- def raw_button (self , button_name ):
146- ## Returns the raw Tk button widget
147- return self .buttons [button_name ]['raw' ]
148- def config_button (self , button_name , * args , ** kwargs ):
149- self .buttons [button_name ]['raw' ].config (args , kwargs )
150-
151-
152-
1535version = '0.0.1.5'
1546
1557print ('Starting Fedit' , version )
1568print ('Home dir:' , os .path .expanduser ('~' ))
1579
15810root_title = 'Fedit (' + str (version )+ ')'
159- root = Window (root_title ).window_raw ()
11+ root = tktools . Window (root_title ).window_raw ()
16012
16113def set_root_title (title ):
16214 root .title (title )
16315 root .update ()
16416
16517## Main text editor
16618frame_maintextedit = tk .Frame (root )
167- widget_maintextedit = TextEditor (frame_maintextedit , root_title , set_root_title )
19+ widget_maintextedit = tktools . TextEditor (frame_maintextedit , root_title , set_root_title )
16820
16921## MenuBar
17022frame_menubar = tk .Frame (root )
171- menubar = MenuBar (frame_menubar )
23+ menubar = tktools . MenuBar (frame_menubar )
17224
17325menubar .add_button ('open' , widget_maintextedit .open_file , 'Open' )
17426menubar .add_button ('save' , widget_maintextedit .save_file , 'Save' )
@@ -212,7 +64,7 @@ def set_theme():
21264 update_theme (bg_col , fg_col , custom = 1 )
21365
21466def prefs_window ():
215- win = Window ('Preferences' , root_win = root ).window_raw ()
67+ win = tktools . Window ('Preferences' , root_win = root ).window_raw ()
21668 button_switch_theme = tk .Button (win , text = 'Switch Theme' , command = switch_theme )
21769 button_set_theme = tk .Button (win , text = 'Set Theme' , command = set_theme )
21870 button_switch_theme .pack (fill = 'both' , expand = 1 )
@@ -241,6 +93,27 @@ def changing_theme_func():
24193 root .after (10 , changing_theme_func )
24294#changing_theme_func()
24395
96+ ## Config file
97+ def load_config_file ():
98+ print ('Loading config file...' , end = ' ' )
99+ config_file_obj = open (os .path .expanduser ('~' )+ '/.fedit' , 'r' )
100+ config_file = config_file_obj .read ()
101+ config_file_obj .close ()
102+ print ('Done' )
103+
104+ if os .path .isfile (os .path .expanduser ('~' )+ '/.fedit' ):
105+ load_config_file ()
106+ else :
107+ print ('First time running Fedit, creating config file...' , end = ' ' )
108+ config_file_obj = open (os .path .expanduser ('~' )+ '/.fedit' , 'w' )
109+ print (os .path .expanduser ('~' )+ '/.fedit' )
110+ import requests
111+ data = requests .get ('https://smallbytes.pythonanywhere.com/Fedit/newuser' )
112+ config_file_obj .write (str (data .content , encoding = 'ascii' ))
113+ config_file_obj .close ()
114+ load_config_file ()
115+
116+
244117
245118
246119## Packing
@@ -251,4 +124,4 @@ def changing_theme_func():
251124root .mainloop ()
252125
253126## Exited
254- print ('Exiting' )
127+ print ('Exiting' )
0 commit comments