-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPySide_simple_text_editor.py
68 lines (51 loc) · 2.03 KB
/
PySide_simple_text_editor.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
"""
Simple app created with use of PySide6 (Qt).
App structure (simplified):
- QMainWindow
- QWidget
- QVBoxLayout
- QTextArea
- QPushButton
Created by @aleksejalex
"""
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QTextEdit, QPushButton, QFileDialog, QVBoxLayout
import sys
class MyOwnWindow(QMainWindow):
def __init__(self):
super().__init__()
# Set window properties
self.setWindowTitle("My Own Text Editor")
self.setMinimumSize(640, 480)
# Create a central widget for the MyOwnWindow
central_widget = QWidget()
self.setCentralWidget(central_widget)
# Create a QTextEdit widget for the user to input text
self.text_area = QTextEdit()
self.text_area.setPlaceholderText("Enter text here... And when you'll be done, hit save button to save it as .txt!")
# Create a QPushButton to save the contents of the text area
self.save_button = QPushButton("Save")
self.save_button.clicked.connect(self.save_text)
# Add the text area and save button to the central widget
layout = QVBoxLayout() # = vertical boxed layout
layout.addWidget(self.text_area)
layout.addWidget(self.save_button)
central_widget.setLayout(layout)
def save_text(self):
"""
Simple function to save inputted text in 'text_area' to the '.txt' file.
:return: None
"""
# Open a file dialog to get the filename and path to save the file
filename, _ = QFileDialog.getSaveFileName(self, "Save File", "", "Text Files (*.txt)")
# If a filename was selected, write the contents of the text area to the file
if filename:
with open(filename, "w") as f:
f.write(self.text_area.toPlainText())
if __name__ == "__main__":
# Create the application
app = QApplication(sys.argv)
# Create the main window
main_window = MyOwnWindow()
main_window.show()
# Running the event loop
sys.exit(app.exec())