-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor-tk.py
More file actions
executable file
·104 lines (81 loc) · 3.18 KB
/
editor-tk.py
File metadata and controls
executable file
·104 lines (81 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#! /usr/bin/python
# Copyright (c) 2010 Patricio Paez <pp@pp.com.mx>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
import sys
import Tkinter
from arithmetic import Parser
def loadFile( event):
'Load file content into the buffer.'
cursorPosition = event.widget.index( 'insert' )
text = open( event.widget.filename ).read()
event.widget.delete( '1.0', Tkinter.END )
event.widget.insert( "1.0", text)
event.widget.mark_set( 'insert', cursorPosition )
def saveFile( event):
'Save the text buffer contents.'
text = open( event.widget.filename, 'w' )
text.write( event.widget.get( '1.0', Tkinter.END ) )
class Editor(object):
'A minimal editor'
def __init__(self):
self.root = Tkinter.Tk()
self.texto = Tkinter.Text(self.root, height=40)
self.texto.pack()
self.texto.focus_set()
# Minimal configuration and key bindings
self.texto['font'] = ("lucida console", 11)
self.texto['wrap'] = Tkinter.NONE
self.texto.bind('<Control-l>', loadFile)
self.texto.bind('<Control-s>', saveFile)
self.texto.bind('<Control-q>', self.quit)
self.texto.bind( '<F5>', calculate)
# Handle single parameter: filename
if len(sys.argv) > 1:
self.texto.filename = sys.argv[1]
try:
text = open( self.texto.filename ).read()
self.texto.insert( Tkinter.END, text )
except:
pass # new file
else:
self.texto.filename = 'unnamed'
self.root.title( self.texto.filename + ' - editor-Tk' )
def run(self):
self.root.mainloop()
def quit( self, event ):
'End the application.'
self.root.quit()
class ParserTk(Parser):
''
def parse( self, textWidget ):
''
for i in range( self.countLines( textWidget ) ):
self.parseLine( i, textWidget, variables=self.variables, functions=self.functions )
def countLines( self, textWidget ):
''
return int(textWidget.index( 'end' ).split('.')[0])
def readLine( self, i, textWidget ):
''
return textWidget.get( str(i) + '.0', str(i) + '.end' )
def writeResult( self, i, textWidget, start, end, text ):
'Write text in line i of lines from start to end offset.'
textWidget.delete( str(i) + '.' + str(start), str(i) + '.' + str(end) )
textWidget.insert( str(i) + '.' + str(start), text )
def calculate( event ):
'Perform arithmetic operations'
parser = ParserTk()
parser.parse( event.widget )
editor = Editor()
editor.run()