-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest1.py
More file actions
115 lines (86 loc) · 4.03 KB
/
test1.py
File metadata and controls
115 lines (86 loc) · 4.03 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
'''Dette er hovedfila til editoren'''
import sys
from PyQt5.QtWidgets import QApplication, QTextEdit, QMainWindow
from PyQt5.QtWidgets import QFileDialog, QToolBar, QAction, QSplitter
# Skiller ut split screen til separat klasse
class CustomSplitter(QSplitter):
'''En klasse for å instansiere individuelle tekstvinduer'''
def __init__(self):
super().__init__()
self.active_widget = None
def set_active_widget(self, widget):
'''Trenger denne for å sette fokus på rett widget'''
self.active_widget = widget
# Setter opp en editor-klasse
class TextEditor(QMainWindow):
'''Selve teksteditoren'''
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
'''Konstruktormetoden'''
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# TEXT EDIT AREAS #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Create a QTextEdit for the text editing area
self.split = CustomSplitter()
self.text_edit_1 = QTextEdit()
self.text_edit_2 = QTextEdit()
self.setCentralWidget(self.split)
self.split.addWidget(self.text_edit_1)
self.split.addWidget(self.text_edit_2)
self.split.set_active_widget(self.text_edit_1)
self.text_edit_1.focus_event = lambda event: self.split.set_active_widget(self.text_edit_1)
self.text_edit_2.focus_event = lambda event: self.split.set_active_widget(self.text_edit_2)
# Endrer tekstområdet til mørkere design
stylesheet_text_area = "background-color: #333; color: white; border: 2px solid #555;"
self.text_edit_1.setStyleSheet(stylesheet_text_area)
self.text_edit_2.setStyleSheet(stylesheet_text_area)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# TOOLBAR #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Lager en toolbar
toolbar = QToolBar("Toolbar?")
self.addToolBar(toolbar)
# Lager open og save knapper til "fil
open_action = QAction("Open", self)
save_action = QAction("Save", self)
open_action.triggered.connect(self.open_file)
save_action.triggered.connect(self.save_file)
# Lager "fil"-knapp til toolbar
file_menu = self.menuBar().addMenu("File")
file_menu.addAction(open_action)
file_menu.addAction(save_action)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# ******* #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Create a central widget to hold the layout
self.setCentralWidget(self.split)
# Initialize the application window
self.setWindowTitle('Hobbyprosjekt 🙂')
self.setGeometry(100, 100, 800, 600)
def open_file(self):
'''For å åpne tekstfiler'''
# Implement the open file function
file_path = QFileDialog.getOpenFileName(self, 'Open File', '',
'Text Files (*.txt);;All Files (*)')
if file_path:
# Read the file and display its content in the QTextEdit
with open(file_path, 'r', encoding="utf-8") as file:
self.text_edit_1.setPlainText(file.read())
def save_file(self):
'''For å lagre tekstfiler'''
file_path, _ = QFileDialog.getSaveFileName(self, 'Save File',
'', 'Text Files (*.txt);;All Files (*)')
if file_path:
with open(file_path, 'w', encoding="utf-8") as file:
file.write(self.text_edit_1.toPlainText())
def focus_event(self):
'''Tror ikke denne gjør noe nyttig lengre'''
print("Gained focus!")
if __name__ == '__main__':
app = QApplication(sys.argv)
# app.setCursorFlashTime(500)
editor = TextEditor()
editor.show()
sys.exit(app.exec_())