Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1bd9288

Browse files
Create notepad.py
A simple Notepad like application implemented using PySimpleGUI.
1 parent a43a4a0 commit 1bd9288

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

‎Sample GUI Implementation/notepad.py

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
'''A simple Notepad application implemented using PySimpleGUI.'''
2+
# pylint: disable=no-member
3+
# pylint: disable=invalid-name
4+
5+
import wx
6+
import PySimpleGUI as sg
7+
8+
# change the default theme.
9+
sg.theme('dark grey 9')
10+
11+
WINDOW_WIDTH: int = 90
12+
WINDOW_HEIGHT: int = 25
13+
FILE_NAME: str = None
14+
DEFAULT_FONT_NAME: str = 'Times New Roman'
15+
16+
def ShowFontDialog():
17+
app=[]
18+
app = wx.App(None)
19+
# app = Font()
20+
# app.MainLoop()
21+
dialog = wx.FontDialog(None, wx.FontData())
22+
if dialog.ShowModal() == wx.ID_OK:
23+
data = dialog.GetFontData()
24+
font = data.GetChosenFont()
25+
font_info = font.GetNativeFontInfoUserDesc()
26+
27+
styles = [style for style in font_info.split(' ')]
28+
underline = font.GetUnderlined()
29+
colour = data.GetColour()
30+
31+
32+
# string variables to shorten loop and menu code
33+
file_new: str = 'New (CTRL+N)'
34+
file_open: str = 'Open (CTRL+O)'
35+
file_save: str = 'Save (CTRL+S)'
36+
37+
menu_layout: list = [['&File', [file_new, file_open, file_save, 'Save As', '__________________', 'Exit']],
38+
['&Statistics', ['Word Count', 'Character With Spaces', 'Character Without Spaces', ]],
39+
['&Help', ['About']]]
40+
41+
layout: list = [[sg.Menu(menu_layout)],
42+
[sg.Text('New File:', font=(DEFAULT_FONT_NAME, 10), size=(WINDOW_WIDTH, 1), key='-FILE_INFO-')],
43+
[sg.Multiline(font=(DEFAULT_FONT_NAME, 12, 'underline italic'), size=(WINDOW_WIDTH, WINDOW_HEIGHT), key='-BODY-')]]
44+
45+
WINDOW = sg.Window('Notepad', layout=layout, margins=(0, 0),
46+
resizable=True, return_keyboard_events=True)
47+
WINDOW.read(timeout=1)
48+
WINDOW.maximize()
49+
WINDOW['-BODY-'].expand(expand_x=True, expand_y=True)
50+
51+
def new_file() -> str:
52+
''' Reset body and info bar, and clear FILE_NAME variable '''
53+
WINDOW['-BODY-'].update(value='')
54+
WINDOW['-FILE_INFO-'].update(value='New File:')
55+
56+
def open_file() -> str:
57+
''' Open file and update the infobar '''
58+
try:
59+
file_name = sg.popup_get_file('Open File', no_window=True)
60+
except: # pylint: disable=bare-except
61+
return
62+
if file_name not in (None, '') and not isinstance(file_name, tuple):
63+
with open(file_name, 'r') as f:
64+
WINDOW['-BODY-'].update(value=f.read())
65+
WINDOW['-FILE_INFO-'].update(value=file_name)
66+
return file_name
67+
68+
def save_file(file_name: str):
69+
''' Save file instantly if already open; otherwise display `save-as` popup '''
70+
71+
file_name = WINDOW['-FILE_INFO-'].DisplayText # pylint: disable=no-member # Get the filename if already saved in the same session.
72+
if file_name not in (None, '', 'New File:'):
73+
with open(file_name, 'w') as f:
74+
f.write(VALUES.get('-BODY-'))
75+
WINDOW['-FILE_INFO-'].update(value=file_name)
76+
else:
77+
save_as()
78+
79+
def save_as() -> str:
80+
''' Save new file or save existing file with another name '''
81+
try:
82+
file_name: str = sg.popup_get_file('Save As', save_as=True, no_window=True)
83+
except: # pylint: disable=bare-except
84+
return
85+
if file_name not in (None, '') and not isinstance(FILE_NAME, tuple):
86+
with open(file_name, 'w') as f:
87+
f.write(VALUES.get('-BODY-'))
88+
WINDOW['-FILE_INFO-'].update(value=file_name)
89+
return file_name
90+
91+
def get_word_count():
92+
''' Get the estimated word count '''
93+
words: list = [word for word in VALUES['-BODY-'].split(' ') if word != '\n']
94+
word_count: int = len(words)
95+
return word_count
96+
97+
def character_count():
98+
'''Get the total number of characters in the file.'''
99+
chars = len(VALUES['-BODY-']) - 1
100+
return chars
101+
102+
def characters_without_spaces():
103+
'''Get the total number of characters in the file.'''
104+
105+
chars_without_spaces: int = 0
106+
# total number of spaces is 1 less than the number of words.
107+
total_spaces: int = get_word_count() - 1
108+
109+
if total_spaces == -1:
110+
chars_without_spaces = character_count()
111+
else:
112+
chars_without_spaces = character_count() - total_spaces
113+
114+
return chars_without_spaces
115+
116+
def about():
117+
'''About the application'''
118+
119+
sg.PopupQuick('A simple Notepad like application created using\
120+
PySimpleGUI framework.', auto_close=False)
121+
122+
while True:
123+
EVENT, VALUES = WINDOW.read()
124+
125+
if EVENT in (None, 'Exit'):
126+
break
127+
if EVENT in (file_new, 'n:78'):
128+
FILE_NAME = new_file()
129+
if EVENT in (file_open, 'o:79'):
130+
FILE_NAME = open_file()
131+
if EVENT in (file_save, 's:83'):
132+
save_file(FILE_NAME)
133+
if EVENT in ('Save As',):
134+
FILE_NAME = save_as()
135+
if EVENT in ('Word Count',):
136+
WORDS = get_word_count()
137+
sg.PopupQuick('Word Count: {:,d}'.format(WORDS), auto_close=False)
138+
if EVENT in ('Characters With Spaces',):
139+
TOTAL_CHARS = character_count()
140+
sg.PopupQuick('Characters With Spaces: {:,d}'.format(TOTAL_CHARS), auto_close=False)
141+
if EVENT in ('Character Without Spaces',):
142+
CHAR_WITHOUT_SPACES = characters_without_spaces()
143+
sg.PopupQuick('Characters Without Spaces: {:,d}'.format(CHAR_WITHOUT_SPACES), auto_close=False)
144+
if EVENT in ('About',):
145+
# about()
146+
ShowFontDialog()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /