Skip to content

Commit 377911d

Browse files
committed
Added "New" button
1 parent c300724 commit 377911d

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

__pycache__/tktools.cpython-37.pyc

405 Bytes
Binary file not shown.

main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
menubar.add_button('open', widget_maintextedit.open_file, 'Open')
2020
menubar.add_button('save', widget_maintextedit.save_file, 'Save')
21+
menubar.add_button('new', widget_maintextedit.new_file, 'New')
2122

2223
menubar.grid_button('open', row=0, column=0)
2324
menubar.grid_button('save', row=0, column=1)
25+
menubar.grid_button('new', row=0, column=2)
2426

2527
## Main text editor
2628
widget_maintextedit.pack(expand=1, fill='both')

tktools.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Import modules
22
import tkinter as tk
33
from tkinter import filedialog as tkfd
4+
from tkinter import messagebox as tkmb
45
import os
56

67

@@ -77,13 +78,26 @@ def save_file(self, *args, **kwargs):
7778
global initial_dir
7879
f_name = tkfd.asksaveasfilename(filetypes=self.ftypes, initialdir=self.initial_dir)
7980
if not verify_f_name(f_name):
80-
return
81+
return 0
8182
print('Saving', f_name)
8283
self.initial_dir = os.path.dirname(f_name)
8384
text = self.get_text(0.0)
8485
f_obj = open(f_name, 'w')
8586
f_obj.write(text)
8687
f_obj.close()
88+
return 1
89+
def new_file(self, *args, **kwargs):
90+
## Clears the text widget
91+
dscn = tkmb.askyesnocancel('Save file?', 'Do you want to save the file already open?') #dscn = Desicion
92+
if dscn == True: #User said yes
93+
if self.save_file() == 0:
94+
self.new_file()
95+
return
96+
elif dscn == None: #User cancelled
97+
return
98+
#User has saved/pressed no
99+
self.clear_text(0.0, 'end')
100+
87101

88102
class MenuBar:
89103
def __init__(self, parent):

0 commit comments

Comments
 (0)