-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotepadeinfach.py
More file actions
132 lines (98 loc) · 4.16 KB
/
notepadeinfach.py
File metadata and controls
132 lines (98 loc) · 4.16 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 4 12:05:58 2025
"""
import tkinter as tk
from tkinter import messagebox, filedialog
textchanged = False
dateiname = None
def on_text_change(event):
global textchanged
if textfeld.edit_modified(): # Prüft, ob wirklich eine Textänderung stattfand
textchanged = True
textfeld.edit_modified(False)
def neues_dokument():
global textchanged, textfeld, dateiname
if textchanged and ja_nein_box("Speichern", "Möchten Sie die Änderungen speichern?"):
speichern()
textfeld.delete("1.0", tk.END)
textchanged = False
textfeld.edit_modified(False)
dateiname = None
def oeffne_datei():
global textchanged, textfeld, dateiname
if textchanged and ja_nein_box("Speichern", "Möchten Sie die Änderungen speichern?"):
speichern()
dateiname = tk.filedialog.askopenfilename(filetypes=[("Textdateien", "*.txt"),
("Python-Dateien", "*.py *.pyw"),
("Alle Dateien", "*.*")])
if dateiname:
with open(dateiname, "r", encoding="UTF-8") as datei:
inhalt = datei.read()
textfeld.delete("1.0", tk.END)
textfeld.insert("1.0", inhalt)
root.title(f"Notepad-- {dateiname}")
# Diese beiden Zeilen NACH dem Einfügen des Textes
textfeld.edit_modified(False)
textchanged = False
def speichern():
if dateiname:
with open(dateiname, "w", encoding="UTF-8") as datei:
datei.write(textfeld.get("1.0", tk.END))
global textchanged
textchanged = False
textfeld.edit_modified(False)
else:
speichern_unter()
def speichern_unter():
global dateiname, textchanged
dateiname = tk.filedialog.asksaveasfilename(filetypes=[("Textdateien", "*.txt"),
("Alle Dateien", "*.*")],
defaultextension=".txt")
if dateiname:
with open(dateiname, "w", encoding="UTF-8") as datei:
datei.write(textfeld.get("1.0", tk.END))
global textchanged
textchanged = False
textfeld.edit_modified(False)
def programm_beenden():
if textchanged and ja_nein_box("Speichern", "Möchten Sie die Änderungen speichern?"):
speichern()
root.quit()
root.destroy()
def ja_nein_box(titel, frage):
return messagebox.askyesno(titel, frage)
def show_about():
messagebox.showinfo("Über", "Notepad--\n\nKurs 13.1\n(c) 2025")
def main():
global root, textfeld
# Hauptfenster
root = tk.Tk()
root.title("Notepad--")
root.geometry("600x400")
root.protocol("WM_DELETE_WINDOW", programm_beenden)
# Textfeld (mehrzeilige Eingabe) erstellen:
textfeld = tk.Text(root, wrap="word")
textfeld.pack(expand=True, fill="both")
textfeld.bind("<<Modified>>", on_text_change)
# Menüleiste erstellen:
# Datei-Menü:
menueleiste = tk.Menu(root) # Die Menüleiste wird erstellt
datei_menue = tk.Menu(menueleiste, tearoff=0) # Datei-Menü wird der Leiste hinzugefügt
datei_menue.add_command(label="Neu", command=neues_dokument) # Element wird hinzugefügt
datei_menue.add_command(label="Öffnen", command=oeffne_datei)
datei_menue.add_command(label="Speichern", command=speichern)
datei_menue.add_command(label="Speichern unter", command=speichern_unter)
datei_menue.add_separator() # Trennlinie
datei_menue.add_command(label="Beenden", command=programm_beenden)
menueleiste.add_cascade(label="Datei", menu=datei_menue) # Untermenü für "Datei" wird erstellt
# Hilfe-Menü:
hilfe_menue = tk.Menu(menueleiste, tearoff=0) # Hilfe-Menü wird der Menüleiste hinzugefügt
hilfe_menue.add_command(label="Über", command=show_about) # "Über" wird dem Hilfe-Unterordner hinzugefügt
menueleiste.add_cascade(label="Hilfe", menu=hilfe_menue) # Untermenü "Hilfe" wird dem Hilfe-Menü hinzugefügt
root.config(menu=menueleiste) # Menüleiste hinzufügen
# Hauptloop starten
root.mainloop()
if __name__ == "__main__":
main()