168 lines
5.8 KiB
Python
Executable File
168 lines
5.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import tkinter as tk
|
|
from tkinter import ttk, StringVar
|
|
from tkinter.messagebox import showinfo, showwarning
|
|
from datetime import datetime
|
|
from PIL import Image, ImageTk
|
|
|
|
now = datetime.now()
|
|
YEAR = now.strftime("%Y")
|
|
MONTH = now.strftime("%m")
|
|
DAY = now.strftime("%d")
|
|
journal_filename = "%s-%s-%s-Journal.txt" % (YEAR, MONTH, DAY)
|
|
|
|
# Determine journal file path
|
|
journal_filename_path = "./%s" % journal_filename
|
|
|
|
# Create the file if it doesn't exist
|
|
if os.path.exists(journal_filename_path):
|
|
with open(journal_filename_path, "a", encoding="utf-8"):
|
|
pass
|
|
else:
|
|
with open(journal_filename_path, "w", encoding="utf-8"):
|
|
pass
|
|
|
|
# Initialize main window
|
|
journal_root = tk.Tk()
|
|
|
|
# Set fullscreen dimensions and background image
|
|
image_location = 'background.jpg'
|
|
width = journal_root.winfo_screenwidth()
|
|
height = journal_root.winfo_screenheight()
|
|
journal_root.geometry("%dx%d" % (width, height))
|
|
journal_root.title("Ameland Kinderjournal")
|
|
|
|
img = ImageTk.PhotoImage(Image.open(image_location).resize((width, height), Image.ADAPTIVE))
|
|
label = tk.Label(journal_root, image=img)
|
|
label.img = img # Keep a reference
|
|
label.place(relx=0.5, rely=0.5, anchor='center')
|
|
|
|
# Store name and message
|
|
name = tk.StringVar()
|
|
message = tk.StringVar()
|
|
|
|
# Create main journal frame
|
|
journal = ttk.Frame(journal_root)
|
|
journal.pack(pady=100, side=tk.TOP)
|
|
|
|
# Name input
|
|
name_label = ttk.Label(journal, text="Dein Name mit Nachname:")
|
|
name_label.grid(sticky='w', column=0, row=0, padx=5, pady=5)
|
|
name_entry = ttk.Entry(journal, textvariable=name, width=65)
|
|
name_entry.grid(sticky='e', column=0, row=0, padx=100, pady=5)
|
|
name_entry.focus()
|
|
|
|
# Textbox height
|
|
textbox_height: int = 40
|
|
if height > 1080:
|
|
textbox_height = height - 1400
|
|
|
|
# Message input
|
|
message_label = tk.Label(journal, text="Deine Nachricht:")
|
|
message_label.grid(column=0, row=1, sticky='w', padx=5)
|
|
message_text = tk.Text(journal, height=textbox_height)
|
|
message_text.grid(column=0, row=2, padx=5)
|
|
|
|
# Function to check message length
|
|
def is_message_valid():
|
|
current_message = message_text.get("1.0", "end-1c")
|
|
if len(current_message) > 1000:
|
|
showwarning("Warnung", "Deine Nachricht ist zu lang, bitte beschränke Dich auf 1000 Zeichen")
|
|
message_text.focus_set()
|
|
return False
|
|
return True
|
|
|
|
# Send button action
|
|
def send_button_clicked():
|
|
if not is_message_valid():
|
|
return
|
|
with open(journal_filename_path, "a", encoding="utf-8") as journal_of_the_day:
|
|
journal_of_the_day.write("Eintrag von: " + name.get() + " um " + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "\n")
|
|
journal_of_the_day.write(message_text.get(1.0, "end-1c") + "\n")
|
|
journal_of_the_day.write("-----------------\n")
|
|
message_text.delete('1.0', "end-1c")
|
|
name.set("")
|
|
showinfo(title="Information", message="Deine Nachricht wurde gespeichert")
|
|
|
|
# Save and quit action
|
|
def save_and_quit_button_clicked():
|
|
if not is_message_valid():
|
|
return
|
|
with open(journal_filename_path, "a", encoding="utf-8") as journal_of_the_day:
|
|
journal_of_the_day.write(name.get() + "\n")
|
|
journal_of_the_day.write(message_text.get(1.0, "end-1c") + "\n")
|
|
journal_of_the_day.write("-----------------\n")
|
|
if not name.get():
|
|
journal_of_the_day.write("Speichern und Beenden von: " + "<noname>" + " um " + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "\n")
|
|
else:
|
|
journal_of_the_day.write("Speichern und Beenden von: " + name.get() + " um " + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "\n")
|
|
journal_root.quit()
|
|
|
|
# Send Button
|
|
button_send = ttk.Button(
|
|
journal,
|
|
text="Speichern und Abschicken",
|
|
command=send_button_clicked
|
|
)
|
|
button_send.grid(sticky='w', column=0, row=3, ipady=8, padx=5, pady=5)
|
|
|
|
# Quit Button
|
|
button_quit = ttk.Button(
|
|
journal,
|
|
text="Beenden",
|
|
command=save_and_quit_button_clicked
|
|
)
|
|
button_quit.grid(sticky='e', column=0, row=3, ipady=8, padx=5, pady=5)
|
|
|
|
# Start application
|
|
journal_root.mainloop()
|
|
|
|
|
|
# TODO
|
|
# Ja, in Python wird ein **Array von Wörtern** in der Regel als eine **Liste von Strings** repräsentiert. Die Funktion `finde_wort_in_text_effizient` ist so geschrieben, dass sie eine solche Liste als Eingabe akzeptiert.
|
|
#
|
|
# ### Beispiel mit einer Liste von Wörtern
|
|
#
|
|
# Hier ist ein Beispiel, das zeigt, wie du die Funktion mit einer ganz normalen Python-Liste (`list`) verwenden kannst, die Strings enthält.
|
|
#
|
|
# ```python
|
|
# def finde_wort_in_text_effizient(woerter_liste, text):
|
|
# """
|
|
# Überprüft effizient, ob ein Wort aus einer Liste in einem Text vorkommt.
|
|
# Args:
|
|
# woerter_liste: Eine Liste von Wörtern.
|
|
# text: Der Text, in dem gesucht werden soll.
|
|
# Returns:
|
|
# False, wenn ein Wort gefunden wird.
|
|
# True, wenn kein Wort gefunden wird.
|
|
# """
|
|
# woerter_set = set(woerter_liste)
|
|
# text_woerter = set(text.split())
|
|
#
|
|
# if not woerter_set.isdisjoint(text_woerter):
|
|
# return False
|
|
#
|
|
# return True
|
|
#
|
|
# # Ein "Array von Wörtern" in Python ist eine Liste
|
|
# mein_array_von_woertern = ["Entwickler", "Programmierung", "Code"]
|
|
#
|
|
# # Ein Text, in dem wir suchen
|
|
# mein_text = "Ich bin ein Python-Entwickler und liebe Programmierung."
|
|
#
|
|
# # Wir übergeben die Liste direkt an die Funktion
|
|
# ergebnis = finde_wort_in_text_effizient(mein_array_von_woertern, mein_text)
|
|
#
|
|
# print(f"Gesuchte Wörter: {mein_array_von_woertern}")
|
|
# print(f"Text: '{mein_text}'")
|
|
# print(f"Treffer gefunden? {ergebnis}")
|
|
#
|
|
# # Das Ergebnis ist False, da "Entwickler" und "Programmierung" gefunden wurden
|
|
# ```
|
|
#
|
|
# Wie du siehst, funktioniert die Funktion einwandfrei mit einer **Liste von Strings**, da dies die standardmäßige und effizienteste Methode in Python ist, um eine Ansammlung von Wörtern zu speichern und zu verarbeiten.
|
|
#
|
|
# Übrigens: Wenn du [Aktivitäten in Gemini-Apps](https://myactivity.google.com/product/gemini) aktivierst, kannst du die Funktionen aller Apps verwenden.0
|