added a check for maximim length of entry (1000 characters)
This commit is contained in:
parent
681f9b1163
commit
1f1152ebd1
117
journal.py
117
journal.py
|
|
@ -3,7 +3,7 @@
|
||||||
import os
|
import os
|
||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
from tkinter import ttk, StringVar
|
from tkinter import ttk, StringVar
|
||||||
from tkinter.messagebox import showinfo
|
from tkinter.messagebox import showinfo, showwarning
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from PIL import Image, ImageTk
|
from PIL import Image, ImageTk
|
||||||
|
|
||||||
|
|
@ -13,86 +13,84 @@ MONTH = now.strftime("%m")
|
||||||
DAY = now.strftime("%d")
|
DAY = now.strftime("%d")
|
||||||
journal_filename = "%s-%s-%s-Journal.txt" % (YEAR, MONTH, DAY)
|
journal_filename = "%s-%s-%s-Journal.txt" % (YEAR, MONTH, DAY)
|
||||||
|
|
||||||
#
|
# Determine journal file path
|
||||||
# determine OS to correctly save journal file and to switch back to one-branch development for convenience
|
journal_filename_path = "./%s" % journal_filename
|
||||||
if os.name == 'posix':
|
|
||||||
journal_filename_path = "./%s" % journal_filename
|
|
||||||
|
|
||||||
if os.name == 'nt':
|
# Create the file if it doesn't exist
|
||||||
journal_filename_path = "$Env:AppData/%s" % journal_filename
|
if os.path.exists(journal_filename_path):
|
||||||
|
with open(journal_filename_path, "a", encoding="utf-8"):
|
||||||
if not os.path.exists(journal_filename_path):
|
|
||||||
with open(journal_filename_path, "w"):
|
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
with open(journal_filename_path, "a"):
|
with open(journal_filename_path, "w", encoding="utf-8"):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
journal = tk.Tk()
|
# Initialize main window
|
||||||
journal.attributes('-alpha', 0.5)
|
journal_root = tk.Tk()
|
||||||
|
|
||||||
#
|
# Set fullscreen dimensions and background image
|
||||||
# set window to fullscreen (dependend of the actual screensize) and fill with a background image
|
|
||||||
image_location = 'background.jpg'
|
image_location = 'background.jpg'
|
||||||
width = journal.winfo_screenwidth()
|
width = journal_root.winfo_screenwidth()
|
||||||
height = journal.winfo_screenheight()
|
height = journal_root.winfo_screenheight()
|
||||||
journal.geometry("%dx%d" % (width, height))
|
journal_root.geometry("%dx%d" % (width, height))
|
||||||
journal.title("Ameland Kinderjournal")
|
journal_root.title("Ameland Kinderjournal")
|
||||||
|
|
||||||
img = ImageTk.PhotoImage(Image.open(image_location).resize((width, height), Image.ADAPTIVE))
|
img = ImageTk.PhotoImage(Image.open(image_location).resize((width, height), Image.ADAPTIVE))
|
||||||
label = tk.Label(journal, image=img)
|
label = tk.Label(journal_root, image=img)
|
||||||
|
label.img = img # Keep a reference
|
||||||
#
|
|
||||||
# Keep a reference in case this code is put in a function.
|
|
||||||
label.img = img
|
|
||||||
|
|
||||||
#
|
|
||||||
# Place label in center of parent.
|
|
||||||
label.place(relx=0.5, rely=0.5, anchor='center')
|
label.place(relx=0.5, rely=0.5, anchor='center')
|
||||||
|
|
||||||
#
|
# Store name and message
|
||||||
# store name and message
|
|
||||||
name = tk.StringVar()
|
name = tk.StringVar()
|
||||||
message = tk.StringVar()
|
message = tk.StringVar()
|
||||||
|
|
||||||
#
|
# Create main journal frame
|
||||||
# Main Window
|
journal = ttk.Frame(journal_root)
|
||||||
journal = ttk.Frame(journal)
|
|
||||||
journal.pack(pady=100, side=tk.TOP)
|
journal.pack(pady=100, side=tk.TOP)
|
||||||
|
|
||||||
#
|
# Name input
|
||||||
# name
|
name_label = ttk.Label(journal, text="Dein Name mit Nachname:")
|
||||||
name_label = ttk.Label(journal, text = "Dein Name:")
|
name_label.grid(sticky='w', column=0, row=0, padx=5, pady=5)
|
||||||
name_label.grid(sticky = 'w', column = 0, row = 0, padx = 5, pady = 5)
|
name_entry = ttk.Entry(journal, textvariable=name, width=65)
|
||||||
name_entry = ttk.Entry(journal, textvariable = name, width = 70)
|
name_entry.grid(sticky='e', column=0, row=0, padx=100, pady=5)
|
||||||
name_entry.grid(sticky = 'e', column = 0, row = 0, padx = 90, pady = 5)
|
|
||||||
name_entry.focus()
|
name_entry.focus()
|
||||||
|
|
||||||
#
|
# Textbox height
|
||||||
# textbox
|
|
||||||
# TODO: textbox need a scrollbar when applicable
|
|
||||||
# standard height of the textbox is 40 unless the screen provides more space in which case we will compute
|
|
||||||
# the textbox_heigth reasonably and relatively to the screensize
|
|
||||||
textbox_height: int = 40
|
textbox_height: int = 40
|
||||||
if height > 1080:
|
if height > 1080:
|
||||||
textbox_height = height - 1400
|
textbox_height = height - 1400
|
||||||
|
|
||||||
message_label = tk.Label(journal, text = "Deine Nachricht:")
|
# Message input
|
||||||
message_label.grid(column = 0, row = 1, sticky = 'w', padx = 5)
|
message_label = tk.Label(journal, text="Deine Nachricht:")
|
||||||
message_text = tk.Text(journal, height = textbox_height)
|
message_label.grid(column=0, row=1, sticky='w', padx=5)
|
||||||
message_text.grid(column = 0, row = 2, 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():
|
def send_button_clicked():
|
||||||
with open(journal_filename_path, "a") as journal_of_the_day:
|
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("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(message_text.get(1.0, "end-1c") + "\n")
|
||||||
journal_of_the_day.write("-----------------\n")
|
journal_of_the_day.write("-----------------\n")
|
||||||
message_text.delete('1.0', "end-1c")
|
message_text.delete('1.0', "end-1c")
|
||||||
name.set("")
|
name.set("")
|
||||||
showinfo(title = "Information", message = "Deine Nachricht wurde gespeichert")
|
showinfo(title="Information", message="Deine Nachricht wurde gespeichert")
|
||||||
|
|
||||||
|
# Save and quit action
|
||||||
def save_and_quit_button_clicked():
|
def save_and_quit_button_clicked():
|
||||||
with open(journal_filename_path, "a") as journal_of_the_day:
|
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(name.get() + "\n")
|
||||||
journal_of_the_day.write(message_text.get(1.0, "end-1c") + "\n")
|
journal_of_the_day.write(message_text.get(1.0, "end-1c") + "\n")
|
||||||
journal_of_the_day.write("-----------------\n")
|
journal_of_the_day.write("-----------------\n")
|
||||||
|
|
@ -100,24 +98,23 @@ def save_and_quit_button_clicked():
|
||||||
journal_of_the_day.write("Speichern und Beenden von: " + "<noname>" + " um " + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "\n")
|
journal_of_the_day.write("Speichern und Beenden von: " + "<noname>" + " um " + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "\n")
|
||||||
else:
|
else:
|
||||||
journal_of_the_day.write("Speichern und Beenden von: " + name.get() + " um " + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "\n")
|
journal_of_the_day.write("Speichern und Beenden von: " + name.get() + " um " + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "\n")
|
||||||
journal.quit()
|
journal_root.quit()
|
||||||
|
|
||||||
#
|
|
||||||
# Send Button
|
# Send Button
|
||||||
button_send = ttk.Button(
|
button_send = ttk.Button(
|
||||||
journal,
|
journal,
|
||||||
text = "Abschicken",
|
text="Speichern und Abschicken",
|
||||||
command = lambda: send_button_clicked()
|
command=send_button_clicked
|
||||||
)
|
)
|
||||||
button_send.grid(sticky = 'w', column = 0, row = 3, ipady = 8, padx = 5, pady = 5)
|
button_send.grid(sticky='w', column=0, row=3, ipady=8, padx=5, pady=5)
|
||||||
|
|
||||||
#
|
|
||||||
# Quit Button
|
# Quit Button
|
||||||
button_quit = ttk.Button(
|
button_quit = ttk.Button(
|
||||||
journal,
|
journal,
|
||||||
text = "Speichern und Beenden",
|
text="Beenden",
|
||||||
command = lambda: save_and_quit_button_clicked()
|
command=save_and_quit_button_clicked
|
||||||
)
|
)
|
||||||
button_quit.grid(sticky = 'e', column = 0, row = 3, ipady = 8, padx = 5, pady = 5)
|
button_quit.grid(sticky='e', column=0, row=3, ipady=8, padx=5, pady=5)
|
||||||
|
|
||||||
journal.mainloop()
|
# Start application
|
||||||
|
journal_root.mainloop()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user