-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextEditor_python.py
More file actions
106 lines (85 loc) · 2.75 KB
/
TextEditor_python.py
File metadata and controls
106 lines (85 loc) · 2.75 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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# A simple text editor
# Author: Gosen
# Date:2018/12/18
#
import sys
if (sys.version_info > (3, 0)):
from tkinter import *
from tkinter.messagebox import *
from tkinter import filedialog
import tkinter.scrolledtext as tkst
import os
else:
sys.stdout.write("Sorry, it requires Python 3.x\n")
sys.exit(1)
#filename = None
#------------------- Pre-settting ---------------------
root = Tk()
root.title("1st Python Text Editor")
root.minsize(width=400, height=400)
root.maxsize(width=400, height=400)
#text = Text(root, width=400, height=400)
#text.pack()
textPad = tkst.ScrolledText(root, width=400, height=400, wrap=WORD)
textPad.pack()
#-------------------------------------------------------
#------------------ Set-up for specific function ----------------------------------------------
class MenuFunc():
@staticmethod
def newFile():
global filename
result = askokcancel("Warning", "Would you like to open a new file without saving the data?")
if result == 1:
filename = "myfile.dat"
textPad.delete(0.0, END)
else:
return
@staticmethod
def saveFile():
f = filedialog.asksaveasfile(mode='w', parent=root, initialfile='myfile.dat')
text2save = str(textPad.get(0.0, END))
f.write(text2save)
f.close()
@staticmethod
def saveAs():
f = filedialog.asksaveasfile(parent=root, mode='w')
if f is None: # asksaveasfile return 'None' if dialog closed with 'Cancel'
return
text2save = str(textPad.get(0.0, END))
try:
f.write(text2save)
f.close()
except:
showerror(title="Oops,...", message="Unable to save the file...")
@staticmethod
def openFile():
fname = filedialog.askopenfile(mode='r')
# print(fname) #used for debug
if fname != None: # no file to open
# Try to openfile and set the window title
contents = StringVar()
contents.set = fname.read()
textPad.delete('1.0', END)
textPad.insert('1.0', contents.set)
# print(contents.set) # used for debug
fname.close()
# display output in the notepad
w = Label(master=root, textvariable=contents.set)
w.pack()
#------------------ End of setup ----------------------------------------------------------------
#------------------ Starting creating menu in the notepad --------------------------------------
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
menu_func = MenuFunc()
filemenu.add_command(label="New", command=menu_func.newFile)
filemenu.add_command(label="Open", command=menu_func.openFile)
filemenu.add_command(label="Save", command=menu_func.saveFile)
filemenu.add_command(label="Save As...", command=menu_func.saveAs)
filemenu.add_separator()
filemenu.add_command(label="Quit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
root.mainloop()