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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
from Tkinter import *
from tkFont import Font
def int2asc(byte, fullcharset = 0):
if byte < 32:
return "."
else:
if fullcharset:
return chr(byte)
else:
if byte < 128:
return chr(byte)
else:
return '.'
def dump(bloc, offset = 0, width = 8, fullcharset = 0):
bigstr = ""
for index in range(len(bloc)):
car = bloc[index]
byte = ord(car)
#start of line
if not (index % width):
bigstr += "%04X "%(index + offset)
carbuf = ""
#any byte
bigstr += "%02X "%(byte)
carbuf += int2asc(byte)
#end of line
if (index % width) == (width - 1):
bigstr += carbuf + '\n'
#line completion*
if (index % width) != (width -1):
while (index % width) != (width -1):
bigstr += "-- "
index += 1
bigstr += carbuf + '\n'
return bigstr
class EDITSECWIN(Tk):
def __init__(self,sector):
Tk.__init__(self)
self.cols = 8
self.sector = sector
self.editabletext = dump(sector)
if(len(self.sector) % self.cols):
height += 1
width = 5 + self.cols * 3 + self.cols
height = len(self.sector) / self.cols
self.editabledigit = []
self.editablechar = []
self.notepad = Text(self,font=Font(family="Terminal",size=16),width=width,height=height)
self.notepad.grid()
self.notepad.delete('1.0',END)
self.notepad.insert(END,self.editabletext)
self.notepad.update()
self.notepad.focus()
self.notepad.bind("<Left>", self.CallLeft)
self.notepad.bind("<Right>", self.CallRight)
self.notepad.bind("<Up>", self.CallUp)
self.notepad.bind("<Down>", self.CallDown)
self.notepad.bind("<KeyPress>", self.EventCallback)
for index in range(len(self.sector)):
line = (index / self.cols) + 1
col1 = 5 + (index % self.cols) * 3
col2 = col1 + 1
col3 = 5 + self.cols * 3 + (index % self.cols)
pos = "%u.%u"%(line,col1)
self.editabledigit.append("%u.%u"%(line,col1))
self.editabledigit.append("%u.%u"%(line,col2))
self.editablechar.append("%u.%u"%(line,col3))
self.notepad.tag_add("%u.%u"%(line,col1), "%u.%u"%(line,col1))
self.notepad.tag_add("%u.%u"%(line,col2), "%u.%u"%(line,col2))
self.editcursor = 0
self.gotoxy(1)
def gotoxy(self,active = 1):
locator = self.editabledigit[self.editcursor]
self.title(locator)
if active:
self.notepad.tag_config(locator, foreground = 'WHITE')
self.notepad.tag_config(locator, background = 'BLACK')
else:
self.notepad.tag_config(locator, foreground = 'BLACK')
self.notepad.tag_config(locator, background = 'WHITE')
def CallLeft(self,event):
self.gotoxy(0)
self.editcursor -= 1
if self.editcursor == -1:
self.editcursor = len(self.editabledigit) - 1
self.gotoxy()
return "break"
def CallRight(self,event):
self.gotoxy(0)
self.editcursor += 1
if self.editcursor >= len(self.editabledigit):
self.editcursor = 0
self.gotoxy()
return "break"
def CallUp(self,event):
self.gotoxy(0)
self.editcursor -= self.cols * 2
if self.editcursor < 0:
self.editcursor += len(self.editabledigit)
self.gotoxy()
return "break"
def CallDown(self,event):
self.gotoxy(0)
self.editcursor += self.cols * 2
if self.editcursor >= len(self.editabledigit):
self.editcursor -= len(self.editabledigit)
self.gotoxy()
return "break"
def EventCallback(self,event):
car = event.char.upper()
if not car in ("0123456789abcdefABCDEF"):
pass
else:
self.notepad.delete(self.editabledigit[self.editcursor])
self.notepad.insert(self.editabledigit[self.editcursor],car)
byte_str = "%c%c"%(self.notepad.get(self.editabledigit[self.editcursor / 2 * 2]),self.notepad.get(self.editabledigit[self.editcursor / 2 * 2 + 1]))
car = int2asc(int(byte_str,16))
self.notepad.delete(self.editablechar[self.editcursor >> 1])
self.notepad.insert(self.editablechar[self.editcursor >> 1],car)
self.CallRight(0)
return "break"
if __name__ == "__main__":
root = EDITSECWIN("Ceci est un bloc de 32 octets...")
root.mainloop() |
Partager