forked from x4nth055/pythoncode-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_editor.py
107 lines (75 loc) · 2.77 KB
/
text_editor.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Import
from tkinter import *
from tkinter import scrolledtext
from tkinter import filedialog
import ctypes
import sys
# Increas Dots Per inch so it looks sharper
ctypes.windll.shcore.SetProcessDpiAwareness(True)
# Setup Variables
appName = 'Simple Text Editor'
nofileOpenedString = 'New File'
currentFilePath = nofileOpenedString
# Viable File Types, when opening and saving files.
fileTypes = [("Text Files","*.txt"), ("Markdown","*.md")]
# Tkinter Setup
window = Tk()
# Set the first column to occupy 100% of the width
window.grid_columnconfigure(0, weight=1)
window.title(appName + " - " + currentFilePath)
# Window Dimensions in Pixel
window.geometry('500x400')
# Handler Functions
def fileDropDownHandeler(action):
global currentFilePath
# Opening a File
if action == "open":
file = filedialog.askopenfilename(filetypes = fileTypes)
window.title(appName + " - " + file)
currentFilePath = file
with open(file, 'r') as f:
txt.delete(1.0,END)
txt.insert(INSERT,f.read())
# Making a new File
elif action == "new":
currentFilePath = nofileOpenedString
txt.delete(1.0,END)
window.title(appName + " - " + currentFilePath)
# Saving a file
elif action == "save" or action == "saveAs":
if currentFilePath == nofileOpenedString or action== 'saveAs':
currentFilePath = filedialog.asksaveasfilename(filetypes = fileTypes)
with open(currentFilePath, 'w') as f:
f.write(txt.get('1.0','end'))
window.title(appName + " - " + currentFilePath)
def textchange(event):
window.title(appName + " - *" + currentFilePath)
# Widgets
# Text Area
txt = scrolledtext.ScrolledText(window, height=999)
txt.grid(row=1,sticky=N+S+E+W)
# Bind event in the widget to a function
txt.bind('<KeyPress>', textchange)
# Menu
menu = Menu(window)
# set tearoff to 0
fileDropdown = Menu(menu, tearoff=False)
# Add Commands and and their callbacks
fileDropdown.add_command(label='New', command=lambda: fileDropDownHandeler("new"))
fileDropdown.add_command(label='Open', command=lambda: fileDropDownHandeler("open"))
# Adding a seperator between button types.
fileDropdown.add_separator()
fileDropdown.add_command(label='Save', command=lambda: fileDropDownHandeler("save"))
fileDropdown.add_command(label='Save as', command=lambda: fileDropDownHandeler("saveAs"))
menu.add_cascade(label='File', menu=fileDropdown)
# Set Menu to be Main Menu
window.config(menu=menu)
# Enabling "open with" by looking if the second argument was passed.
if len(sys.argv) == 2:
currentFilePath = sys.argv[1]
window.title(appName + " - " + currentFilePath)
with open(currentFilePath, 'r') as f:
txt.delete(1.0,END)
txt.insert(INSERT,f.read())
# Main Loop
window.mainloop()