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
| try:import Tkinter
except:import tkinter as Tkinter
import tkFileDialog
import tkMessageBox
import sys
class NOTEPAD(Tkinter.Toplevel):
def __init__(self,container, fname_var="", *args, **kwds):
Tkinter.Toplevel.__init__(self, *args, **kwds)
self.hide()
self.editor = Tkinter.Text(self)
self.editor.grid()
self.fname_var = fname_var
self.protocol('WM_DELETE_WINDOW', self.hide)
self.editor.bind('<Escape>', self.escape)
self.attributes("-toolwindow", 1)
def setText(self):
try:
text = open(self.fname_var.get(), 'r').read(-1)
except:
return False
self.editor['state'] = 'normal'
self.editor.delete('1.0', 'end')
self.editor.insert('end', text)
self.editor['state'] = 'disabled'
return True
def show(self):
if not self.setText():
return
self.title(self.fname_var.get())
self.resizable(False, False)
self.deiconify()
self.editor.focus()
def hide(self):
self.withdraw()
def escape(self, event=None):
self.hide()
win = Tkinter.Tk()
fname_var = Tkinter.StringVar(value=sys.argv[0])
print fname_var.get()
notepad = NOTEPAD(win, fname_var)
Tkinter.Button(win, text="View code", width= 40, height =5, command=notepad.show).grid()
win.mainloop() |
Partager