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
|
import Tkinter as tk
import sys
def buttonCallback(y, text):
print "button %s[%i] pressed"%(text, y)
def cellCallback(event, x, y, text):
print "cell[%i, %i]=%s"%(x, y, text)
class FIXED_OBJECT(tk.LabelFrame):
def __init__(self, container, title='', text='', **kwds):
tk.LabelFrame.__init__(self, container, text=title, **kwds)
self.editor = tk.Text(self, **kwds)
self.editor.insert('end', text)
self.editor.grid(column=0, row=0, sticky='nsew')
self.scrollbar = tk.Scrollbar(self, orient='vertical')
self.scrollbar.grid(column=1, row=0, sticky='ns')
self.editor['yscrollcommand'] = self.scrollbar.set
self.scrollbar.config(command=self.editor.yview)
class RESIZABLE_OBJECT(tk.LabelFrame):
def __init__(self, container, title='', data=None, **kwds):
tk.LabelFrame.__init__(self, container, text=title, **kwds)
self.canvas = tk.Canvas(self, height=250)
self.frm = tk.Frame(self.canvas)
self.canvas.create_window(0, 0, window=self.frm)
self.placeData(data)
self.frm.grid()
self.canvas.grid(column=0, row=0, sticky='nsew')
self.scrollbar = tk.Scrollbar(self, orient='vertical')
self.scrollbar.grid(column=1, row=0, sticky='ns')
self.canvas['yscrollcommand'] = self.scrollbar.set
self.scrollbar['command'] = self.canvas.yview
def placeData(self, data):
for y, line in enumerate(data):
if y & 1:
color = 'green'
else:
color = 'white'
for x in range(6):
if x < len(line):
cell = line[x]
else:
cell = '. . . .'
cellgui = tk.Label(self.frm, text=cell, width=16, bg=color, cursor='hand2')
cellgui.grid(column=x, row=y)
cellgui.bind("<Button-1>", lambda event=None, x=x, y=y, label2=cell:cellCallback(event, x, y, label2))
tk.Button(self.frm, text='read', width=3, command=lambda y=y, label='read':buttonCallback(y, label)).grid(column=6, row=y)
tk.Button(self.frm, text='write', width=3, command=lambda y=y, label='write':buttonCallback(y, label)).grid(column=7, row=y)
class WINDOW(tk.Toplevel):
def __init__(self, container, *args, **kwds):
tk.Toplevel.__init__(self, container, *args, **kwds)
with open(sys.argv[0]) as fp:
default_text= fp.read(-1)
with open(sys.argv[0]) as fp:
lines= fp.readlines()[:30]
data = [line.split()[:5] for line in lines]
self.widget1 = FIXED_OBJECT(self, title='RAW PDO', text=default_text, width=40, height=2)
self.widget2 = FIXED_OBJECT(self, title='PDO OBJECTS', text=default_text, width=80, height=4)
self.widget3 = RESIZABLE_OBJECT(self, "EDS OBJECTS", data)
self.widget1.grid()
self.widget2.grid()
self.widget3.grid(sticky='nsew')
self.grid_rowconfigure(2, weight=1)
self.offset = self.winfo_height() - self.widget3.canvas.winfo_height()
self.resizable(height=True, width=False)
if __name__ == "__main__":
win = tk.Tk()
node = WINDOW(win)
win.mainloop() |
Partager