Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # workingtexteditor.py
- import sys
- import textwrap
- from tkinter import *
- import tkinter.filedialog as dialog
- import tkinter.scrolledtext as scrollbar
- root = Tk()
- # Define the 'Save' function
- def save(root, text):
- data = text.get('0.0', END)
- filename = dialog.asksaveasfilename(
- parent = root,
- # Give several different types of files to save as
- # that the editor can handle
- filetypes = [('Text Files', '*.txt'), ('Python Files', '*.py', '*.pyc')],
- title = 'Save as...')
- writer = open(filename, 'w')
- writer.write(data)
- writer.close()
- # Define the 'Open' function
- def open(root, text):
- data = text.read('0.0', END)
- filename = dialog.askopenfilename(
- parent = root,
- # Give several different types of files to open that
- # the editor can handle
- filetypes = [('Text Files', '*.txt'),
- ('Python Files', '*.py'),
- ('All Files', '*.*')],
- title = 'Open...')
- writer = open(filename, 'w+')
- writer.write(data)
- writer.close()
- # Define the 'Exit' function
- def quit(root):
- root.destroy()
- # Define other functions for the other menus
- def cut(root):
- cut()
- def copy(root):
- copy()
- def paste(root):
- paste()
- def about(root):
- about()
- window = Tk()
- # Specify a scrollbar
- def scrollbar(root):
- tkinter.scrolledtext()
- # Make the text editor automatically wrap text to default length
- def wrap(root):
- textwrap.TextWrapper()
- wrapper = TextWrapper()
- wrapper.width(70)
- # Make the actual GUI text editor window
- window = Tk()
- text = Text(window)
- text.pack()
- # Create a menu
- menubar = Menu(window)
- filemenu = Menu(menubar)
- filemenu.add_command(label = 'Open', command = lambda : open(window, 'w+'))
- filemenu.add_command(label = 'Save', command = lambda : save(window, text))
- filemenu.add_command(label = 'Exit', command = lambda : quit(window))
- menubar.add_cascade(label = 'File', menu = filemenu)
- # Create another pulldown menu
- editmenu = Menu(menubar, tearoff = 0)
- editmenu.add_command(label = 'Cut', command = lambda : cut(window, text))
- editmenu.add_command(label = 'Copy', command = lambda : copy(text))
- editmenu.add_command(label = 'Paste', command = lambda : paste(window, text))
- menubar.add_cascade(label = 'Edit', menu = editmenu)
- # Yet another
- helpmenu = Menu(menubar, tearoff = 0)
- helpmenu.add_command(label = 'About', command = lambda : about(window, text))
- menubar.add_cascade(label = 'Help', menu = helpmenu)
- window.config(menu = menubar)
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement