-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.8.1 Simple Notepad Project.py
executable file
·117 lines (106 loc) · 4.93 KB
/
2.8.1 Simple Notepad Project.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
108
109
110
111
112
113
114
115
116
117
import tkinter
from PIL import ImageTk, Image
from tkinter import StringVar, IntVar, scrolledtext, END, messagebox, filedialog
# Define window
root = tkinter.Tk()
root.title('Notepad')
#root.iconbitmap("./Files and Images/pad.ico")
root.geometry('600x600')
root.resizable(0,0)
# Define fonts and colors
text_color = "#fffacd"
menu_color = "#dbd9db"
root_color = "#6c809a"
root.config(bg=root_color)
# Define functions To Work With The Fonts
def change_font(event):
"""Change the given font based off dropbox options."""
if font_option.get() == 'none':
my_font = (font_family.get(), font_size.get())
else:
my_font = (font_family.get(), font_size.get(), font_option.get())
# Change the font style
input_text.config(font=my_font)
# Function To Work With New Note
def new_note():
"""Create a new Note which essentially clears the screen."""
# Use a messagebox to ask for a new note
question = messagebox.askyesno("New Note", "Are you sure you want to start a new note?")
if question == 1:
# ScrolledText widgets starting index is 1.0 not 0.
input_text.delete("1.0", END)
def close_note():
"""Closes the note which essentially quits the program."""
#Use a messagebox to ask to close
question = messagebox.askyesno("Close Note", "Are you sure you want to close your note?")
if question == 1:
root.destroy()
def save_note():
"""Save the given note. First three lines are saved as font family, font size, and
font option."""
#Use filedialog to get location and name of where/what to save the file as.
save_name = filedialog.asksaveasfilename(initialdir="./", title="Save Note",
filetypes=(("Text Files", "*.txt"), ("All Files", "*.*")))
with open(save_name, 'w') as f:
#First three lines of save file are font_family, font_size, and font_options.Font_size must be a string noot int.
f.write(font_family.get() + "\n")
f.write(str(font_size.get()) + "\n")
f.write(font_option.get() + "\n")
#write remaining text in field to the file
f.write(input_text.get("1.0", END))
def open_note():
"""Open a previously saved note. First three lines of note are font family, font
size, and font option. First set the font, then load the text."""
#Use filedialog to get location and directory of note file
open_name = filedialog.askopenfilename(initialdir="./", title='Open Note',
filetypes=(("Text Files", "*.txt"), ("All Files", "*.*")))
with open(open_name, 'r') as f:
#Clear the current text
input_text.delete("1.0", END)
#First three lines are font_faimly, font_size, and font_option...You must strip
# the new line char at the end of each line!
font_family.set(f.readline().strip())
font_size.set(int(f.readline().strip()))
font_option.set(f.readline().strip())
#Call the change font for these .set() and pass an arbitrary value
change_font(1)
#Read the rest of the file and insert it into the text field
text = f.read()
input_text.insert("1.0", text)
#Define Layout
#Define frames
menu_frame = tkinter.Frame(root, bg=menu_color)
text_frame = tkinter.Frame(root, bg=text_color)
menu_frame.pack(padx=5, pady=5)
text_frame.pack(padx=5, pady=5)
#Layout for menu frame
#Create the menu: new, open, save, close, font family, font size, font option
new_image = ImageTk.PhotoImage(Image.open('./Files and Images/new.png'))
new_button = tkinter.Button(menu_frame, image=new_image, command=new_note)
new_button.grid(row=0, column=0, padx=5, pady=5)
open_image = ImageTk.PhotoImage(Image.open('./Files and Images/open.png'))
open_button = tkinter.Button(menu_frame, image=open_image, command=open_note)
open_button.grid(row=0, column=1, padx=5, pady=5)
save_image = ImageTk.PhotoImage(Image.open('./Files and Images/save.png'))
save_button = tkinter.Button(menu_frame, image=save_image, command=save_note)
save_button.grid(row=0, column=2, padx=5, pady=5)
close_image = ImageTk.PhotoImage(Image.open('./Files and Images/close.png'))
close_button = tkinter.Button(menu_frame, image=close_image, command=close_note)
close_button.grid(row=0, column=3, padx=5, pady=5)
#Create a list of fonts to use
families = ['Terminal', 'Modern', 'Script', 'Courier', 'Arial', 'Calibri', 'Cambria',
'Georgia', 'MS Gothic', 'SimSun', 'Tahoma', 'Times New Roman', 'Verdana', 'Wingdings']
font_family = StringVar()
font_family_drop = tkinter.OptionMenu(menu_frame, font_family, *families,
command=change_font)
font_family.set('Terminal')
#Set the width so it will fit "times new roman" and remain constant
font_family_drop.config(width=16)
font_family_drop.grid(row=0, column=4, padx=5, pady=5)
sizes = [8, 10, 12, 14, 16, 20, 24, 32, 48, 64, 72, 96]
font_size = IntVar()
font_size_drop = tkinter.OptionMenu(menu_frame, font_size, *sizes, command=change_font)
font_size.set(12)
#Set width to be constant even if its 8.
font_size_drop.config(width=2)
font_size_drop.grid(row=0, column=5, padx=5, pady=5)