1 #@ MODIF Interp Editeur DATE 02/07/2001 AUTEUR D6BHHJP J.P.LEFEBVRE
2 # CONFIGURATION MANAGEMENT OF EDF VERSION
3 # ======================================================================
4 # COPYRIGHT (C) 1991 - 2001 EDF R&D WWW.CODE-ASTER.ORG
5 # SEE THE FILE "LICENSE.TERMS" FOR INFORMATION ON USAGE AND
6 # REDISTRIBUTION OF THIS FILE.
7 # ======================================================================
9 import Tkinter,ScrolledText
10 import os, sys, string, traceback
16 class PythonInterpreter( code.InteractiveConsole):
17 def __init__( self, text, namespace = None):
18 code.InteractiveConsole.__init__( self, namespace)
21 def showtraceback( self):
22 start = self.text.pos + " - 1 lines"
23 code.InteractiveConsole.showtraceback( self)
25 self.text.tag_add( "exception", start, end)
27 class InterpWindow(Tkinter.Toplevel):
28 def __init__(self,namespace, parent=None):
29 Tkinter.Toplevel.__init__(self,parent)
31 self.stdout = self.stderr = self
36 self.shell = PythonInterpreter( self,namespace)
37 self.write("Python %s on %s\n%s\n(%s)\n" %
38 (sys.version, sys.platform, sys.copyright,
39 self.__class__.__name__))
43 def _initTkWidgets( self):
44 self.text = ScrolledText.ScrolledText( self, bg = "white",fg="black", wrap="word")
45 self.text.pack( fill='both', expand = 1)
46 self.text.bind( '<KeyPress>', self.clearMsg)
47 self.text.bind( '<Return>', self.inputhandler)
48 self.text.bind( '<Up>', self.uphistory)
49 self.text.bind( '<Down>', self.downhistory)
50 self.text.bind( '<Control-a>', self.goto_sol)
51 self.text.bind( '<Control-d>', self.sendeof)
52 self.text.tag_config("exception", foreground = "red")
54 def swapStdFiles(self):
55 sys.stdout,self.stdout = self.stdout,sys.stdout
56 sys.stderr,self.stderr = self.stderr,sys.stderr
58 def write(self, data):
59 self.text.insert("end", data)
60 self.pos = self.text.index("end - 1 char")
61 self.text.yview_pickplace("end")
63 def tag_add( self, tag, start, end):
64 self.text.tag_add( tag, start, end)
66 def inputhandler(self, *args):
67 # Remove any extraneous stuff
68 self.text.delete( self.pos + " lineend", "end")
70 line = self.text.get(self.pos, "end - 1 char")
71 self.text.insert("end", "\n")
72 self.pos = self.text.index("end")
73 self.addHistory( line)
75 if self.shell.push( line):
80 self.text.mark_set("insert", "end")
83 def addHistory( self, line):
85 self.history.insert( len( self.history) - 1, line)
86 self.hpos = len( self.history) - 1
88 def sendeof(self, *args):
92 def uphistory(self, event=None):
93 if not self.history: return "break"
96 self.hpos = self.hpos - 1
98 line = self.history[ self.hpos]
99 self.text.delete( self.pos, "end")
100 self.text.insert( self.pos, line)
104 def downhistory( self, event=None):
105 if not self.history: return "break"
107 if self.hpos < (len( self.history) - 1):
108 self.hpos = self.hpos + 1
110 line = self.history[ self.hpos]
111 self.text.delete( self.pos, "end")
112 self.text.insert( self.pos, line)
116 def goto_sol( self, event=None):
118 Met en mode edition la ligne courante
120 self.text.mark_set( 'insert', 'insert linestart + 4 chars')
123 def clearMsg( self, event=None):
124 index = self.text.index( "insert")
125 self.text.delete( "insert lineend", "end")
128 if __name__ == "__main__":
133 InterpWindow(d,parent=app)
135 Tkinter.Button(app,text="Interp",command=go).pack()
136 Tkinter.Button(app,text="Quit",command=app.destroy).pack()