3
3
# pylint: disable=invalid-name
4
4
5
5
import shlex
6
+ from tkinter import Tk
7
+ import clipboard
6
8
import wx
7
9
import PySimpleGUI as sg
8
10
11
+ # initialize the tkinter framework and call the withdraw() API
12
+ # to ensure the blank Tkinter root dialog does not popup at runtime.
13
+ tk = Tk ()
14
+ tk .withdraw ()
15
+
9
16
# change the default theme.
10
17
sg .theme ('dark grey 9' )
11
18
@@ -72,11 +79,18 @@ def rgb2hex(r, g, b):
72
79
return "#{:02x}{:02x}{:02x}" .format (r , g , b )
73
80
74
81
# file menu constants.
75
- file_new : str = 'New (CTRL+N)'
82
+ file_new : str = 'New (CTRL+N)'
76
83
file_open : str = 'Open (CTRL+O)'
77
- file_save : str = 'Save (CTRL+S)'
84
+ file_save : str = 'Save (CTRL+S)'
85
+
86
+ # edit menu constants.
87
+ edit_cut : str = 'Cut (CTRL+X)'
88
+ edit_copy : str = 'Copy (CTRL+C)'
89
+ edit_paste : str = 'Paste (CTRL+V)'
90
+ edit_delete : str = 'Delete (Del)'
78
91
79
- menu_layout : list = [['&File' , [file_new , file_open , file_save , 'Save As' , '__________________' , 'Exit' ]],
92
+ menu_layout : list = [['&File' , [file_new , file_open , file_save , 'Save As' , '______________________' , 'Exit' ]],
93
+ ['&Edit' , [edit_cut , edit_copy , edit_paste , edit_delete ]],
80
94
['&Statistics' , ['Word Count' , 'Line Count' , 'Character With Spaces' , 'Character Without Spaces' , ]],
81
95
['F&ormat' , ['Font' , ]],
82
96
['&Help' , ['About' ]]]
@@ -127,7 +141,7 @@ def save_as() -> str:
127
141
try :
128
142
file_name : str = sg .popup_get_file ('Save As' , save_as = True , no_window = True )
129
143
except : # pylint: disable=bare-except
130
- return
144
+ return ''
131
145
if file_name not in (None , '' ) and not isinstance (FILE_NAME , tuple ):
132
146
with open (file_name , 'w' ) as f :
133
147
f .write (VALUES .get ('-BODY-' ))
@@ -221,7 +235,8 @@ def about():
221
235
if EVENT in (sg .WINDOW_CLOSED , 'Exit' ):
222
236
# exit out of the application is close or exit clicked.
223
237
break
224
-
238
+
239
+ # file menu events.
225
240
if EVENT in (file_new , 'n:78' ):
226
241
new_file ()
227
242
if EVENT in (file_open , 'o:79' ):
@@ -230,6 +245,27 @@ def about():
230
245
save_file (FILE_NAME )
231
246
if EVENT in ('Save As' ,):
232
247
FILE_NAME = save_as ()
248
+
249
+ # edit menu events.
250
+ if EVENT == edit_cut :
251
+ selected_text = WINDOW ['-BODY-' ].Widget .selection_get ()
252
+ tk .clipboard_clear ()
253
+ tk .clipboard_append (selected_text )
254
+ tk .update ()
255
+
256
+ if EVENT == edit_copy :
257
+ selected_text = WINDOW ['-BODY-' ].Widget .selection_get ()
258
+ tk .clipboard_clear ()
259
+ tk .clipboard_append (selected_text )
260
+ tk .update () # now it stays on the clipboard after the window is closed
261
+
262
+ if EVENT == edit_paste :
263
+ clip_text = tk .clipboard_get ()
264
+ WINDOW ['-BODY-' ].Widget .insert ("insert" , clip_text )
265
+
266
+ if EVENT == edit_delete :
267
+ WINDOW ['-BODY-' ].Widget .delete ("sel.first" , "sel.last" )
268
+
233
269
if EVENT in ('Word Count' ,):
234
270
WORDS = get_word_count ()
235
271
if WORDS != 0 :
0 commit comments