-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotepadapp.py
More file actions
132 lines (108 loc) · 5.69 KB
/
Copy pathnotepadapp.py
File metadata and controls
132 lines (108 loc) · 5.69 KB
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
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.filechooser import FileChooserIconView
import os
from kivy.uix.popup import Popup
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from kivy.graphics import Color
from kivy.uix.label import Label
class NotepadApp(App):
def build(self):
self.analyzer = SentimentIntensityAnalyzer()
# Main layout container with dark background
layout = BoxLayout(orientation="vertical", padding=10, spacing=10)
# Create text input for editing with proper flow and alignment
self.text_input = TextInput(hint_text="Type your text here...", multiline=True, write_tab=False, halign='left')
self.text_input.bind(text=self.update_mood)
# Ensure text input is white background and black text
self.text_input.background_color = (1, 1, 1, 1) # White background for input field
self.text_input.foreground_color = (0, 0, 0, 1) # Black text for input
self.text_input.font_size = 20
self.text_input.text_validate_unfocused = True # Ensure the input works well
self.text_input.height = 100 # Set a minimum height to avoid shrinking
# Create save button
self.save_button = Button(text="Save", size_hint_y=None, height=50)
self.save_button.bind(on_press=self.save_file)
# Container to display colored paragraphs
self.text_display = BoxLayout(orientation="vertical")
self.text_display.add_widget(self.text_input)
# Add the components to the layout
layout.add_widget(self.text_display)
layout.add_widget(self.save_button)
return layout
def update_mood(self, instance, value):
# Split the text into paragraphs based on newlines
paragraphs = value.split('\n')
# Clear the display container before re-rendering
self.text_display.clear_widgets()
# Loop through each paragraph and apply color based on sentiment
for paragraph in paragraphs:
sentiment_score = self.analyzer.polarity_scores(paragraph)['compound']
label = Label(text=paragraph, size_hint_y=None, height=40, halign='left') # Left-aligned text
if sentiment_score >= 0.5:
self.change_text_color(label, (0.63, 0.91, 0.63)) # Happy - light green
elif sentiment_score <= -0.5:
self.change_text_color(label, (1, 0.43, 0.43)) # Angry - red
elif sentiment_score == 0:
self.change_text_color(label, (1, 1, 1)) # Neutral - white
else:
self.change_text_color(label, (0.63, 0.79, 1)) # Sad - light blue
self.text_display.add_widget(label)
def change_text_color(self, label, color):
# Apply the sentiment color to the label's text
label.color = (color[0], color[1], color[2], 1) # Set RGBA color
def save_file(self, instance):
# Open the save dialog with file filter extension
content = BoxLayout(orientation="vertical")
filechooser = FileChooserIconView(path=os.path.expanduser("~\\Documents"), filters=["*.txt"])
# Add a TextInput for entering a new file name if none is selected
filename_input = TextInput(hint_text="Enter a new filename (without extension)", multiline=False, size_hint_y=None, height=40)
filename_input.disabled = False # Ensure it is enabled by default
content.add_widget(filechooser)
content.add_widget(filename_input)
# Add a Save button to the dialog
save_button = Button(text="Save", size_hint_y=None, height=50)
content.add_widget(save_button)
# Function to handle file selection and filename input visibility
def handle_selection(*args):
if filechooser.selection:
# Enable the save button if a file is selected
save_button.disabled = False
filename_input.disabled = True # Disable filename input when a file is selected
else:
# If no file is selected, enable filename input
save_button.disabled = False
filename_input.disabled = False
filechooser.bind(selection=handle_selection)
# Function to handle the save action when the button is clicked
def save_to_file(instance):
selected_file = filechooser.selection
path = filechooser.path
filename = ""
if selected_file:
# File is selected, use it to save content
filename = selected_file[0]
print(f"Saving to {path}/{filename}") # Debugging line
elif filename_input.text:
# No file selected, use the input for a new filename
filename = filename_input.text
if not filename.endswith('.txt'):
filename += '.txt' # Ensure the file has the .txt extension
if filename:
try:
with open(f"{path}/{filename}", 'w') as file:
file.write(self.text_input.text)
self.text_input.text = "" # Clear text input after saving
self.popup.dismiss() # Close the popup after saving
except Exception as e:
print(f"Error saving file: {e}")
else:
print("No filename entered!")
save_button.bind(on_press=save_to_file)
# Open the popup with the file chooser and save button
self.popup = Popup(title="Save File", content=content, size_hint=(0.9, 0.9))
self.popup.open()
if __name__ == "__main__":
NotepadApp().run()