1
+ #GUI Notepad
2
+
3
+ from tkinter import *
4
+ from tkinter .messagebox import showinfo
5
+ from tkinter .filedialog import askopenfilename , asksaveasfilename
6
+ import os
7
+
8
+ def newFile ():
9
+ global file
10
+ root .title ("Untitled - Notepad" )
11
+ file = None
12
+ TextArea .delete (1.0 , END )
13
+
14
+ def openFile ():
15
+ global file
16
+
17
+ file = askopenfilename (defaultextension = ".txt" , filetypes = [("All files" , "*.*" ), ("Text Documents" , "*.txt" )])
18
+
19
+ if file == "" :
20
+ file = None
21
+ else :
22
+ root .title (os .path .basename (file ) + "- Notepad" )
23
+ TextArea .delete (1.0 , END )
24
+
25
+ f = open (file , "r" )
26
+ TextArea .insert (1.0 ,f .read ())
27
+
28
+ f .close ()
29
+
30
+ def saveFile ():
31
+ global file
32
+ if file == None :
33
+ file = asksaveasfilename (initialfile = "Untitled.txt" ,defaultextension = ".txt" , filetypes = [("All files" , "*.*" ), ("Text Documents" , "*.txt" )])
34
+
35
+ if file == "" :
36
+ file = None
37
+
38
+ else :
39
+ #Save it as a New File
40
+ f = open (file , "w" )
41
+ f .write (TextArea .get (1.0 , END ))
42
+ f .close ()
43
+
44
+ root .title (os .path .basename (file ) + " - Notepad" )
45
+ else :
46
+ f = open (file , "w" )
47
+ f .write (TextArea .get (1.0 , END ))
48
+ f .close ()
49
+
50
+
51
+ def quitApp ():
52
+ root .destroy ()
53
+
54
+ def cut ():
55
+ TextArea .event_generate (("<<Cut>>" ))
56
+
57
+ def copy ():
58
+ TextArea .event_generate (("<<Copy>>" ))
59
+
60
+ def paste ():
61
+ TextArea .event_generate (("<<Paste>>" ))
62
+
63
+ def about ():
64
+ showinfo ("About Notepad" , "Notepad by farizma" )
65
+
66
+
67
+ if __name__ == '__main__' :
68
+
69
+ # ---------------- Basic tkinter setup ----------------
70
+ # Creating an instance of tkinter
71
+ root = Tk ()
72
+ # Adding title
73
+ root .title ("Untitled - Notepad" )
74
+ # Setting icon
75
+ root .iconbitmap ("icon.ico" )
76
+ # Setting default size
77
+ root .geometry ("644x588" )
78
+ # Setting minimum size
79
+ root .minsize (600 , 500 )
80
+
81
+
82
+
83
+ # ---------------- Creating a Menu Bar ----------------
84
+ MenuBar = Menu (root )
85
+ root .config (menu = MenuBar )
86
+
87
+ # File Menu
88
+ FileMenu = Menu (MenuBar , tearoff = 0 )
89
+ # To open a New File
90
+ FileMenu .add_command (label = "New" , command = newFile )
91
+ # To open already existing File
92
+ FileMenu .add_command (label = "Open" , command = openFile )
93
+ # To save the current file
94
+ FileMenu .add_command (label = "Save" , command = saveFile )
95
+ # To add a seperating line
96
+ FileMenu .add_separator ()
97
+ # To quit the notepad
98
+ FileMenu .add_command (label = "Exit" , command = quitApp )
99
+
100
+ MenuBar .add_cascade (label = "File" , menu = FileMenu )
101
+
102
+
103
+ # Edit Menu
104
+ EditMenu = Menu (MenuBar , tearoff = 0 )
105
+ # To give a feature of Cut, Copy, Paste
106
+ EditMenu .add_command (label = "Cut" , command = cut )
107
+ EditMenu .add_command (label = "Copy" , command = copy )
108
+ EditMenu .add_command (label = "Paste" , command = paste )
109
+
110
+ MenuBar .add_cascade (label = "Edit" , menu = EditMenu )
111
+
112
+
113
+ # Help Menu
114
+ HelpMenu = Menu (MenuBar , tearoff = 0 )
115
+ HelpMenu .add_command (label = "About Notepad" , command = about )
116
+
117
+ MenuBar .add_cascade (label = "Help" , menu = HelpMenu )
118
+
119
+
120
+
121
+ # ---------------- Creating a Text Area ----------------
122
+ # Text area for writing text
123
+ TextArea = Text (root , font = "lucida 13" )
124
+ file = None
125
+ TextArea .pack (expand = True , fill = BOTH )
126
+
127
+ # Adding Scrollbar using rules from tkinter
128
+ scroll = Scrollbar (TextArea )
129
+ scroll .pack (side = RIGHT , fill = Y )
130
+ scroll .config (command = TextArea .yview )
131
+ TextArea .config (yscrollcommand = scroll .set )
132
+
133
+ # ---------------- Creating a Bottom Status Bar ----------------
134
+ # line_col = StringVar()
135
+ # line_col.set("Ln 1, Col 1")
136
+
137
+ statusbar = Frame (root , bd = 1 , relief = SUNKEN )
138
+ statusbar .pack (side = BOTTOM , fill = X )
139
+
140
+ Label (statusbar , text = "UTF-8" , width = 20 ).pack (side = RIGHT )
141
+ Label (statusbar , text = "Windows(CRLF)" , width = 20 ).pack (side = RIGHT )
142
+ # Label(statusbar, textvariable=line_col, width=20).pack(side=RIGHT)
143
+
144
+ root .mainloop ()
0 commit comments