/Notepad.py Secret
Created
May 12, 2019 13:31
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tkinter import * | |
from tkinter.messagebox import showinfo | |
from tkinter import filedialog | |
import os | |
fullpath = "" | |
def setWinTitle(): | |
filepath, filename = os.path.split(fullpath) | |
docname, ext = os.path.splitext(filename) | |
if docname == "": | |
titleText = "NOTEPAD - Untitled" | |
else: | |
titleText = "NOTEPAD - " + docname | |
root.title(titleText) | |
def donothing(): | |
pass | |
def newfile(): | |
global fullpath | |
fullpath = "" | |
setWinTitle() | |
text.delete("1.0", END) | |
def openfile(): | |
global fullpath | |
filepath, filename = os.path.split(fullpath) | |
fullpath = filedialog.askopenfilename(initialdir = filepath, title = "Open", filetypes = (("text files", "*.txt"), ("all files", "*.*"))) | |
if fullpath == "": | |
return | |
filepath, filename = os.path.split(fullpath) | |
docname, ext = os.path.splitext(filename) | |
setWinTitle() | |
file = open(fullpath, "r") | |
text.delete("1.0", END) | |
text.insert(INSERT, file.read()) | |
file.close() | |
def savefile(): | |
global fullpath, docname | |
filepath, filename = os.path.split(fullpath) | |
if filename == "": | |
fullpath = filedialog.asksaveasfilename(initialdir = filepath, title = "Save", filetypes = (("text files", "*.txt"), ("all files", "*.*"))) | |
if fullpath == "": | |
return | |
filepath, filename = os.path.split(fullpath) | |
docname, ext = os.path.splitext(filename) | |
setWinTitle() | |
if ext.lower() != ".txt": | |
fullpath = fullpath + ".txt" | |
file = open(fullpath, "w") | |
file.write(text.get("1.0", END)) | |
file.close() | |
def saveasfile(): | |
global fullpath | |
filepath, filename = os.path.split(fullpath) | |
fullpath = filedialog.asksaveasfilename(initialdir = filepath, title = "Save As", filetypes = (("text files", "*.txt"), ("all files", "*.*"))) | |
if filename == "": | |
return | |
filepath, filename = os.path.split(fullpath) | |
docname, ext = os.path.splitext(filename) | |
setWinTitle() | |
if ext.lower() != ".txt": | |
fullpath = fullpath + ".txt" | |
file = open(fullpath, "w") | |
file.write(text.get("1.0", END)) | |
file.close() | |
def showhelp(): | |
showinfo("Notepad Help","You can use File/Open, File/Save, File/Save As, File/Exit, Help/Help Index, Help/About") | |
def showabout(): | |
showinfo("About","This is a simple Notepad\nMade using TkInter\nBundled with Python distribution") | |
def makeMenu(): | |
menubar = Menu(root) | |
filemenu = Menu(menubar) | |
menubar.add_cascade(label = "File", menu = filemenu) | |
filemenu.add_command(label = "New", command = newfile) | |
filemenu.add_command(label = "Open", command = openfile) | |
filemenu.add_command(label = "Save", command = savefile) | |
filemenu.add_command(label = "Save As", command = saveasfile) | |
filemenu.add_separator() | |
filemenu.add_command(label = "Exit", command = root.destroy) | |
editmenu = Menu(menubar) | |
menubar.add_cascade(label = "Edit", menu = editmenu) | |
editmenu.add_command(label = "Undo", command = donothing) | |
editmenu.add_separator() | |
editmenu.add_command(label = "Cut", command = donothing) | |
editmenu.add_command(label = "Copy", command = donothing) | |
editmenu.add_command(label = "Paste", command = donothing) | |
editmenu.add_command(label = "Delete", command = donothing) | |
editmenu.add_command(label = "Select All", command = donothing) | |
helpmenu = Menu(menubar) | |
menubar.add_cascade(label = "Help", menu = helpmenu) | |
helpmenu.add_command(label = "Help Index", command = showhelp) | |
helpmenu.add_command(label = "About...", command = showabout) | |
root.config(menu = menubar) | |
root = Tk() | |
setWinTitle() | |
makeMenu() | |
scrollbar = Scrollbar(root) | |
scrollbar.pack(side = RIGHT, fill = Y) | |
text = Text(root) | |
text.pack() | |
scrollbar.config(command = text.yview) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment