-
Notifications
You must be signed in to change notification settings - Fork 4
/
file_menu.py
61 lines (50 loc) · 1.67 KB
/
file_menu.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
from tkinter import *
from tkinter.filedialog import *
from tkinter.messagebox import *
import sys
class File():
def newFile(self):
self.filename = "Untitled"
self.text.delete(0.0, END)
def saveFile(self):
try:
t = self.text.get(0.0, END)
f = open(self.filename, 'w')
f.write(t)
f.close()
except:
self.saveAs()
def saveAs(self):
f = asksaveasfile(mode='w', defaultextension='.txt')
t = self.text.get(0.0, END)
try:
f.write(t.rstrip())
except:
showerror(title="Oops!", message="Unable to save file...")
def openFile(self):
f = askopenfile(mode='r')
self.filename = f.name
t = f.read()
self.text.delete(0.0, END)
self.text.insert(0.0, t)
def quit(self):
entry = askyesno(title="Quit", message="Are you sure you want to quit?")
if entry == True:
self.root.destroy()
def __init__(self, text, root):
self.filename = None
self.text = text
self.root = root
def main(root, text, menubar):
filemenu = Menu(menubar)
objFile = File(text, root)
filemenu.add_command(label="New", command=objFile.newFile)
filemenu.add_command(label="Open", command=objFile.openFile)
filemenu.add_command(label="Save", command=objFile.saveFile)
filemenu.add_command(label="Save As...", command=objFile.saveAs)
filemenu.add_separator()
filemenu.add_command(label="Quit", command=objFile.quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
if __name__ == "__main__":
print("Please run 'main.py'")