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
| # -*- coding: utf-8 -*-
#
#
#
from sys import version_info
import string
from subprocess import Popen, PIPE
if version_info[0] == 2:
from Tkinter import *
else:
from tkinter import *
class pyConsole(Tk):
""" Fenêtre console """
def __init__(self, width=80, fg='white', bg='black'):
Tk.__init__(self)
self.title("pyConsole 0.1")
self.prompt='\n> '
self.textconsole=Text(self, width=width, fg=fg, bg=bg, state=NORMAL, wrap=CHAR)
scrollconsole = Scrollbar(self, command = self.textconsole)
self.textconsole.configure(yscrollcommand = scrollconsole.set)
self.textconsole.bind("<Return>", self.traite_Return)
self.textconsole.bind("<KP_Enter>", self.traite_KP_Enter)
self.textconsole.bind("<BackSpace>", self.traite_backspace)
self.textconsole.pack(side=LEFT, fill=BOTH)
scrollconsole.pack(side=RIGHT, fill=Y)
self.textconsole.insert(END, self.prompt)
""" Gestions EVENTS """
def traite_KP_Enter(self, event):
""" Gestion de KP_Enter """
self.execute()
self.textconsole.insert(END, self.prompt)
def traite_Return(self, event):
""" Gestion de Return. A corriger """
self.execute()
self.textconsole.insert(END, self.prompt)
def traite_backspace(self, event):
""" Gestion backspace. A corriger """
cindex = self.getindex(CURRENT)
print cindex
print type(cindex)
if int(cindex[1]) > 3:
self.textconsole.delete("%s-0c" % INSERT, INSERT)
else:
nindex = str(cindex[0]) + '.0'
self.textconsole.delete(nindex, END)
self.textconsole.insert(END, self.prompt)
""" Traitements """
def clean(self):
""" Traitement cls/clear """
self.textconsole.delete(1.0, END)
self.textconsole.insert(END, self.prompt)
def quit(self):
""" Traitement exit/quit """
self.destroy()
def getindex(self, index):
return self.textconsole.index(index).split('.')
def execute(self):
""" A corriger """
cindex = self.getindex(CURRENT)
nindexs = str(cindex[0]) + '.0'
nindexf = str(cindex[0])+'.end'
commande = self.textconsole.get(nindexs, nindexf)[2:]
self.traite(commande)
def traite(self, commande):
if commande == 'cls' or commande == 'clear': self.clean()
elif commande.lower == 'exit' or commande == 'quit': self.quit()
else:
self.textconsole.insert(END, self.prompt)
p = Popen([commande], shell=True,stdout=PIPE)
retour = []
for elems in p.stdout:
self.textconsole.insert(END, elems)
elems=elems.replace("\n","")
retour.append(elems)
return retour
""" Appels externes """
def insercmd(self, commande):
retour = self.traite(commande)
return retour
if __name__== '__main__':
fen=pyConsole(80, 'white', 'black')
fen.mainloop() |
Partager