-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
130 lines (113 loc) · 4.76 KB
/
main.py
File metadata and controls
130 lines (113 loc) · 4.76 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from custom_widgets.custom_prompt import CustomPrompt
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master.title("PyNote")
self.master.protocol("WM_DELETE_WINDOW", self.on_closing)
self.master.bind('<Control-s>',self.save)
# Save file location for opened files
self.file_location = None
self.save_location = ''
self.saved_text = ""
#getting users screen width and dividing it by some factor to get some screen size
self.screen_width = round(self.winfo_screenwidth()/20)
self.screen_height = round(self.winfo_screenheight()/25)
# Create menu bar
self.menu_bar = tk.Menu(self.master)
file_menu = tk.Menu(self.menu_bar, tearoff=0)
self.master.config(menu=self.menu_bar)
file_menu.add_command(label="New", command=self.new)
file_menu.add_command(label="Open", command=self.open)
file_menu.add_command(label="Save", command=self.save)
file_menu.add_command(label="Save as...", command=self.saveas)
file_menu.add_separator()
file_menu.add_command(label="Quit", command=self.on_closing)
self.menu_bar.add_cascade(label="File", menu=file_menu)
# Create text box for window
self.text = tk.Text(self, width= self.screen_width, height = self.screen_height)
self.text.pack(side="top")
self.pack()
#Check if the text was changed (True if was - otherwise False)
def text_is_changed(self):
t = self.text.get("1.0", "end-1c")
if(t == self.saved_text):
return False
else:
return True
# New Function
def new(self):
if self.text_is_changed():
answer = messagebox.askyesnocancel("Pynote", "Save changes?") # Prompt to save changes
if answer:
self.save()
# After saving, call new again
self.new()
elif answer is None:
pass
else:
self.saved_text = self.text.get("1.0", "end-1c") # Update saved_text and rerun new
self.new()
else:
self.file_location = None
self.text.delete("1.0", "end")
self.saved_text = ""
#Save function
def save(self, *args):
if(self.file_location is None):
self.saveas()
else:
self.saved_text = self.text.get("1.0", "end-1c")
file1 = open(self.file_location, "w+")
file1.write(self.saved_text + "\n")
file1.close()
def saveas(self):
self.save_location = filedialog.asksaveasfilename()
if(self.save_location != ''):
self.saved_t = self.text.get("1.0", "end-1c")
self.file_location = self.save_location
file1 = open(self.save_location, "w+")
file1.write(self.saved_t + "\n")
file1.close()
# Open function
def open(self):
if self.text_is_changed():
answer = messagebox.askyesnocancel("Pynote", "Save changes?") # Prompt to save changes
if answer:
self.save()
self.open()
elif answer is None:
pass
else:
self.saved_text = self.text.get("1.0", "end-1c") # Set current text to saved_text
self.open()
else:
# Find location of file to open
open_location = filedialog.askopenfilename(title='Open Text File', filetypes=[('text files', '*.txt')])
if (open_location != ''): # Check file was selected
opened_file = open(open_location, "r") # Open file
opened_text = opened_file.read() # Read file and save text
self.text.delete("1.0", "end-1c") # Delete old text
self.text.insert("1.0", opened_text) # Insert text to text box at line 1, character 0
opened_file.close() # Close file
self.file_location = open_location # Set file_location variable to opened file location
self.saved_text = self.text.get("1.0", "end-1c") # Save new text for next check
def on_closing(self):
if(self.text_is_changed()):
answer = tk.messagebox.askyesnocancel("PyNote", "Do you want to save the changes?")
if answer:
self.save()
if(self.save_location != ''):
self.master.destroy()
elif(answer is None):
pass
else:
self.master.destroy()
else:
self.master.destroy()
# Start program
root = tk.Tk()
app = Application(root)
app.mainloop()