import tkinter as tk
w=tk.Tk()
code='''
proc ahora {{f "%T"}} {return [clock format [clock seconds] -format $f -timezone -0500]}proc tmsg {{txt ""} {ty "ok"} {img "info"}} {return [tk_messageBox -message $txt -type $ty -icon $img]}
catch {destroy .t}toplevel .t
wm geometry .t "1000x900+0+0"
wm title .t "Filtrar texto con contexto"
wm withdraw .
pack [panedwindow .t.p -sashwidth 10 -orient vertical] -side top -fill both -expand 1
labelframe .t.p.f -bg #AAA -height 100 -width 800
labelframe .t.p.k -bg #BBB -height 100 -width 800
labelframe .t.p.g -bg #CCC -height 400 -width 800
labelframe .t.p.h -bg #DDD -height 100 -width 800
.t.p add .t.p.f -minsize 60
.t.p add .t.p.k -minsize 60
.t.p add .t.p.g -minsize 40
.t.p add .t.p.h -minsize 40
# Variables globales
set arch ""
set filtro ""
set ::data ""
# Panel archivo
pack [button .t.p.f.ba -text "Archivo:" -command { if {![file exists $arch]} { set arch [tk_getOpenFile -filetypes {{{TXT} {*.txt}}}] if {$arch eq ""} {return} set fh [open $arch r]
set ::data [read $fh]
close $fh
.t.p.g.t delete 0.0 end
.t.p.g.t insert end $::data
}
}] [entry .t.p.f.ea -textvar arch -width 50] -side left -padx 10
# Spinbox para contexto (líneas antes y después)
pack [label .t.p.f.lb -text "Lineas Contiguas (-X a X):"] [spinbox .t.p.f.bsp -from -50 -to 50 -width 5 -textvariable context] -side left -padx 1
set context 0
# Panel filtro
pack [button .t.p.k.ba -text "Filtrar por:" -command { .t.p.h.t delete 0.0 end
set lines [split $::data "\n"]
set total [llength $lines]
set context [expr {[string trim $context]}] array unset mostrar
for {set i 0} {$i < $total} {incr i} { if {[string first $filtro [lindex $lines $i]] >= 0} {## for {set j [expr {$i - $context}]} {$j <= [expr {$i + $context}]} {incr j} for {set j [expr {$i}]} {$j <= [expr {$i + $context}]} {incr j} { if {$j >= 0 && $j < $total} { set mostrar($j) 1
}
}
}
}
for {set i 0} {$i < $total} {incr i} { if {[info exists mostrar($i)]} { .t.p.h.t insert end "[lindex $lines $i]\n"
}
}
}] [entry .t.p.k.ef -textvar filtro -width 50] -side left -padx 10
# Botón para guardar filtrado
pack [button .t.p.k.bs -text "Guardar filtrado:" -command { set sale [tk_getSaveFile -filetypes {{{TXT} {*.txt}} {{LOG} {*.log}}}] if {$sale eq ""} {return} set fh [open $sale w]
set ::filtrado [.t.p.h.t get 0.0 end]
puts $fh $::filtrado
close $fh
}] -side left -padx 10
# Scrollbars y Text widget para archivo
place [scrollbar .t.p.g.sy -width 20 -command {.t.p.g.t yview}] -x 0 -y 20 -height 400place [scrollbar .t.p.g.sx -width 20 -command {.t.p.g.t xview} -orient horizontal] -x 20 -y 0 -width 800place [text .t.p.g.t -undo 1 -font "arial 12" -wrap none -xscrollcommand {.t.p.g.sx set} -yscrollcommand {.t.p.g.sy set}] -x 20 -y 20 -width 860 -height 400# Scrollbars y Text widget para filtrado
place [scrollbar .t.p.h.sy -width 20 -command {.t.p.h.t yview}] -x 0 -y 20 -height 400place [scrollbar .t.p.h.sx -width 20 -command {.t.p.h.t xview} -orient horizontal] -x 20 -y 0 -width 800place [text .t.p.h.t -undo 1 -tabs 50 -font "arial 12" -wrap none -xscrollcommand {.t.p.h.sx set} -yscrollcommand {.t.p.h.sy set}] -x 20 -y 20 -width 860 -height 400'''
w.eval(code)
w.mainloop()