fork(34) download
  1. #!/usr/bin/env python
  2.  
  3. from Tkinter import *
  4.  
  5. from tkSimpleDialog import askstring
  6.  
  7. from tkFileDialog import asksaveasfilename
  8. from tkFileDialog import askopenfilename
  9.  
  10. from tkMessageBox import askokcancel
  11.  
  12. Window = Tk()
  13. Window.title("TekstEDIT")
  14. index = 0
  15. class Quitter(Frame):
  16.  
  17. def __init__(self, parent=None):
  18.  
  19. Frame.__init__(self, parent)
  20.  
  21. self.pack()
  22.  
  23. widget = Button(self, text='Quit', command=self.quit)
  24.  
  25. widget.pack(expand=YES, fill=BOTH, side=LEFT)
  26.  
  27. def quit(self):
  28.  
  29. ans = askokcancel('Verify exit', "Really quit?")
  30.  
  31. if ans: Frame.quit(self)
  32.  
  33.  
  34.  
  35. class ScrolledText(Frame):
  36.  
  37. def __init__(self, parent=None, text='', file=None, background='black'):
  38.  
  39. Frame.__init__(self, parent)
  40.  
  41. self.pack(expand=YES, fill=BOTH)
  42.  
  43. self.makewidgets()
  44.  
  45. self.settext(text, file)
  46.  
  47. def makewidgets(self):
  48.  
  49. sbar = Scrollbar(self)
  50.  
  51. text = Text(self, relief=SUNKEN)
  52.  
  53. sbar.config(command=text.yview)
  54.  
  55. text.config(yscrollcommand=sbar.set)
  56.  
  57. sbar.pack(side=RIGHT, fill=Y)
  58.  
  59. text.pack(side=LEFT, expand=YES, fill=BOTH)
  60.  
  61. self.text = text
  62.  
  63. def settext(self, text='', file=None):
  64.  
  65. if file:
  66.  
  67. text = open(file, 'r').read()
  68.  
  69. self.text.delete('1.0', END)
  70.  
  71. self.text.insert('1.0', text)
  72.  
  73. self.text.mark_set(INSERT, '1.0')
  74.  
  75. self.text.focus()
  76.  
  77. def gettext(self):
  78.  
  79. return self.text.get('1.0', END+'-1c')
  80.  
  81.  
  82.  
  83. class SimpleEditor(ScrolledText):
  84.  
  85. def __init__(self, parent=None, file=None):
  86.  
  87. frm = Frame(parent)
  88.  
  89. frm.pack(fill=X)
  90.  
  91. Button(frm, text='Save', command=self.onSave).pack(side=LEFT)
  92.  
  93. Button(frm, text='Open', command=self.onOpen).pack(side=LEFT)
  94.  
  95. Button(frm, text='Cut', command=self.onCut).pack(side=LEFT)
  96.  
  97. Button(frm, text='Copy', command=self.onCopy).pack(side=LEFT)
  98.  
  99. Button(frm, text='Paste', command=self.onPaste).pack(side=LEFT)
  100.  
  101. Button(frm, text='Find', command=self.onFind).pack(side=LEFT)
  102.  
  103. Button(frm, text='Night-Mode', command=self.onNightMode).pack(side=LEFT)
  104.  
  105. Quitter(frm).pack(side=RIGHT)
  106.  
  107. ScrolledText.__init__(self, parent, file=file)
  108.  
  109. self.text.config(font=('courier', 12, 'normal'))
  110.  
  111. def onSave(self):
  112.  
  113. filename = asksaveasfilename()
  114.  
  115. if filename:
  116.  
  117. alltext = self.gettext()
  118.  
  119. open(filename, 'w').write(alltext)
  120.  
  121. def onOpen(self):
  122.  
  123. self.choosen = askopenfilename(initialdir='~')
  124.  
  125. self.text.insert(END, open(self.choosen).read())
  126.  
  127. def onCut(self):
  128.  
  129. text = self.text.get(SEL_FIRST, SEL_LAST)
  130.  
  131. self.text.delete(SEL_FIRST, SEL_LAST)
  132.  
  133. self.clipboard_clear()
  134.  
  135. self.clipboard_append(text)
  136.  
  137. def onCopy(self):
  138.  
  139. text = self.text.get(SEL_FIRST, SEL_LAST)
  140.  
  141. self.clipboard_clear()
  142.  
  143. self.clipboard_append(text)
  144.  
  145.  
  146. def onPaste(self):
  147.  
  148. try:
  149.  
  150. text = self.selection_get(selection='CLIPBOARD')
  151.  
  152. self.text.insert(INSERT, text)
  153.  
  154. except TclError:
  155.  
  156. pass
  157.  
  158. def onFind(self):
  159.  
  160. target = askstring('SimpleEditor', 'Search String?')
  161.  
  162. if target:
  163.  
  164. where = self.text.search(target, INSERT, END)
  165.  
  166. if where:
  167.  
  168. print where
  169.  
  170. pastit = where + ('+%dc' % len(target))
  171.  
  172. #self.text.tag_remove(SEL, '1.0', END)
  173.  
  174. self.text.tag_add(SEL, where, pastit)
  175.  
  176. self.text.mark_set(INSERT, pastit)
  177.  
  178. self.text.see(INSERT)
  179.  
  180. self.text.focus()
  181.  
  182. def onNightMode(self):
  183. if index:
  184. self.text.config(font=('courier', 12, 'normal'), background='black', fg='green')
  185.  
  186. else:
  187. self.text.config(font=('courier', 12, 'normal'))
  188.  
  189. index = not index
  190.  
  191. if __name__ == '__main__':
  192.  
  193. try:
  194.  
  195. SimpleEditor(file=sys.argv[1]).mainloop()
  196.  
  197. except IndexError:
  198.  
  199. SimpleEditor().mainloop()
  200. # your code goes here
Runtime error #stdin #stdout #stderr 0.08s 25496KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "prog.py", line 12, in <module>
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1712, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable