-
Notifications
You must be signed in to change notification settings - Fork 1
/
10_rich_text_notepad.py
140 lines (116 loc) · 4.84 KB
/
10_rich_text_notepad.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont, QTextCursor, QColor
from PyQt5.QtWidgets import QMainWindow, QApplication, QTextEdit, QAction, QStatusBar, QInputDialog, QFontDialog, \
QMessageBox, QFileDialog, QColorDialog
class Notepad(QMainWindow):
def __init__(self):
super().__init__()
self.Gui()
def Gui(self):
self.setGeometry(100, 100, 350, 400)
self.setWindowTitle('NOTEPAD 2')
self.widgets()
self.notepadMenu()
self.show()
def widgets(self):
self.text_field = QTextEdit()
self.setCentralWidget(self.text_field)
self.setStatusBar(QStatusBar())
def notepadMenu(self):
# x -> as separator
menus = {'File':'New x Open Save Exit',
'Edit':'Undo Redo Cut Copy Paste x Find',
'Tools':'Font Color x Highlight',
'Help':'About'}
shortcuts = 'NOSQZRXCVFBLHA'
handlers = {
'New': self.newFile,
'Open': self.openFile,
'Save': self.saveFile,
'Find': self.findText,
'Font': self.selectFont,
'Color': self.chooseFontColor,
'Highlight': self.highlightColor,
'Exit': self.close,
'About': self.aboutDialog,
}
menubar = self.menuBar()
i=0
for key,val in menus.items():
menu = menubar.addMenu(key)
for v in val.split(' '):
if v=='x':
menu.addSeparator()
else:
action = QAction(v, self)
# eliminated doubles in shortcuts
action.setShortcut(f'Ctrl+{shortcuts[i]}')
i+=1 # index for shortcuts
if handlers.get(v):
action.triggered.connect(handlers[v])
menu.addAction(action)
# handlers:
def saveFile(self):
# display dialog asking user if they want to save the text in the text edit field to a text file.
file_name, _ = QFileDialog.getSaveFileName(self, 'Save File', "","HTML Files (*.html);;Text Files (*.txt)")
notepad_text = ''
if file_name.endswith('.txt'):
notepad_text = self.text_field.toPlainText()
elif file_name.endswith('.html'):
notepad_text = self.text_field.toHtml()
try:
with open(file_name, 'w') as file:
file.write(notepad_text)
except:
QMessageBox.information(self, "Error", "Unable to save file.", QMessageBox.Ok)
def openFile(self):
file_name, _ = QFileDialog.getOpenFileName(self, "Open File", "", "HTML Files (*.html);;Text Files (*.txt)")
if file_name:
with open(file_name, 'r') as file:
text = file.read()
self.text_field.setText(text)
else:
QMessageBox.information(self, "Error", "Unable to open file.", QMessageBox.Ok)
def newFile(self):
self.backup_copy = self.text_field.toPlainText()
self.text_field.clear()
def aboutDialog(self):
QMessageBox.about(self, "About Notepad", "Beginner's Practical Guide to PyQt\n\nProject 5.1 - Notepad GUI")
def selectFont(self):
current = self.text_field.currentFont()
#font, ok = QFontDialog.getFont(QFont("Helvetica", 10), self)
font, ok = QFontDialog.getFont(current, self) # MacOs - , options=QFontDialog.DontUseNativeDialog
if ok:
self.text_field.setCurrentFont(font)
def chooseFontColor(self):
color = QColorDialog.getColor()
if color.isValid():
self.text_field.setTextColor(color)
def highlightColor(self):
color = QColorDialog.getColor()
if color.isValid():
self.text_field.setTextBackgroundColor(color)
def findText(self):
find_text, ok = QInputDialog.getText(self, "Search Text", 'Find:')
# Check to make sure the text can be modified
selections = []
if ok and not self.text_field.isReadOnly():
self.text_field.moveCursor(QTextCursor.Start)
color = QColor(Qt.yellow)
while self.text_field.find(find_text):
# Use ExtraSelections to mark the text you are searching for as yellow
selection = QTextEdit.ExtraSelection()
selection.format.setBackground(color)
# Set the cursor of the selection
selection.cursor = self.text_field.textCursor()
# Add selection to list
selections.append(selection)
# Highlight selections in text edit widget
for _ in selections:
self.text_field.setExtraSelections(selections)
# run the program
if __name__ == '__main__':
app = QApplication(sys.argv) # or []
window = Notepad()
sys.exit(app.exec_())