Advertisement
Stosswalkinator

Text Editor (with Tk)

Oct 18th, 2012
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. # workingtexteditor.py
  2. import sys
  3. import textwrap
  4.  
  5. from tkinter import *
  6.  
  7. import tkinter.filedialog as dialog
  8. import tkinter.scrolledtext as scrollbar
  9.  
  10. root = Tk()
  11.  
  12. # Define the 'Save' function
  13. def save(root, text):
  14.     data = text.get('0.0', END)
  15.     filename = dialog.asksaveasfilename(
  16.         parent = root,
  17.         # Give several different types of files to save as
  18.         # that the editor can handle
  19.         filetypes = [('Text Files', '*.txt'), ('Python Files', '*.py', '*.pyc')],
  20.         title = 'Save as...')
  21.     writer = open(filename, 'w')
  22.     writer.write(data)
  23.     writer.close()
  24.  
  25. # Define the 'Open' function
  26. def open(root, text):
  27.     data = text.read('0.0', END)
  28.     filename = dialog.askopenfilename(
  29.         parent = root,
  30.         # Give several different types of files to open that
  31.         # the editor can handle
  32.         filetypes = [('Text Files', '*.txt'),
  33.                      ('Python Files', '*.py'),
  34.                      ('All Files', '*.*')],
  35.         title = 'Open...')
  36.     writer = open(filename, 'w+')
  37.     writer.write(data)
  38.     writer.close()
  39.  
  40. # Define the 'Exit' function
  41. def quit(root):
  42.     root.destroy()
  43.  
  44. # Define other functions for the other menus
  45. def cut(root):
  46.     cut()
  47.  
  48. def copy(root):
  49.     copy()
  50.  
  51. def paste(root):
  52.     paste()
  53.  
  54. def about(root):
  55.     about()
  56.  
  57. window = Tk()
  58.  
  59. # Specify a scrollbar
  60. def scrollbar(root):
  61.     tkinter.scrolledtext()
  62.  
  63. # Make the text editor automatically wrap text to default length
  64. def wrap(root):
  65.     textwrap.TextWrapper()
  66.     wrapper = TextWrapper()
  67.     wrapper.width(70)
  68.  
  69. # Make the actual GUI text editor window
  70. window = Tk()
  71. text = Text(window)
  72. text.pack()
  73.  
  74. # Create a menu
  75. menubar = Menu(window)
  76. filemenu = Menu(menubar)
  77. filemenu.add_command(label = 'Open', command = lambda : open(window, 'w+'))
  78. filemenu.add_command(label = 'Save', command = lambda : save(window, text))
  79. filemenu.add_command(label = 'Exit', command = lambda : quit(window))
  80. menubar.add_cascade(label = 'File', menu = filemenu)
  81.  
  82. # Create another pulldown menu
  83. editmenu = Menu(menubar, tearoff = 0)
  84. editmenu.add_command(label = 'Cut', command = lambda : cut(window, text))
  85. editmenu.add_command(label = 'Copy', command = lambda : copy(text))
  86. editmenu.add_command(label = 'Paste', command = lambda : paste(window, text))
  87. menubar.add_cascade(label = 'Edit', menu = editmenu)
  88.  
  89. # Yet another
  90. helpmenu = Menu(menubar, tearoff = 0)
  91. helpmenu.add_command(label = 'About', command = lambda : about(window, text))
  92. menubar.add_cascade(label = 'Help', menu = helpmenu)
  93.  
  94. window.config(menu = menubar)
  95.  
  96. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement