Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter import messagebox
- import os
- import datetime
- def diarywrite():
- """Writes input text to the diary."""
- inputtxt = txt.get("1.0", END).strip() # Get text from the Text widget
- if not inputtxt:
- messagebox.showwarning("Input Required", "Please enter some text to write to the diary.")
- return
- diary_entry = inputtxt
- if date_checkbox_var.get():
- date_formatted = get_date_formatted()
- diary_entry = f"{date_formatted}{diary_entry}\n\n"
- with open("MY-DIARY.txt", "a") as file:
- file.write(diary_entry)
- messagebox.showinfo("Success", "Your entry has been recorded!")
- txt.delete("1.0", END) # Clear the text field
- def diaryopen():
- """Opens the diary in the default text editor."""
- if not os.path.exists("MY-DIARY.txt"):
- with open("MY-DIARY.txt", "w") as file:
- file.write("") # Create an empty file if it doesn't exist
- os.system("notepad MY-DIARY.txt")
- def createnewdiary():
- """Creates a new diary file."""
- new_diary_name = txt.get("1.0", END).strip()
- if not new_diary_name:
- messagebox.showwarning("Input Required", "Please enter a name for the new diary.")
- return
- new_diary_name = new_diary_name.replace(" ", "_").replace(".txt", "") + ".txt"
- if os.path.exists(new_diary_name):
- messagebox.showwarning("File Exists", f"A file named '{new_diary_name}' already exists!")
- return
- with open(new_diary_name, "w") as file:
- file.write("") # Create an empty diary
- os.system(f"notepad {new_diary_name}")
- messagebox.showinfo("Success", f"New diary '{new_diary_name}' has been created!")
- txt.delete("1.0", END)
- def get_date_formatted():
- """Returns the current date and time formatted for diary entries."""
- x = datetime.datetime.now()
- str_time = x.strftime("[%H:%M:%S] ")
- str_day = x.strftime("%A")
- str_date = x.strftime("%d")
- str_month = x.strftime("%B")
- str_year = str(x.year)
- return f"{str_time}{str_day}, {str_month} {str_date}, {str_year}: \n"
- # GUI Setup
- window = Tk()
- window.title("Welcome To The Najeeb Diary!")
- window.geometry("900x500")
- # Large Text Field with Scrollbar
- lbl = Label(window, text="Hello Najeeb! Write Diary Here:-", font=("Times New Roman Bold", 18))
- lbl.grid(column=0, row=0, padx=10, pady=10, sticky="w")
- frame_txt = Frame(window)
- frame_txt.grid(column=0, row=1, columnspan=3, padx=10, pady=10)
- # Create a Text widget
- txt = Text(frame_txt, width=108, height=21, bd=5, wrap=WORD)
- # Create a Scrollbar widget
- scrollbar = Scrollbar(frame_txt, orient=VERTICAL, command=txt.yview)
- scrollbar.pack(side=RIGHT, fill=Y)
- # Attach the Scrollbar to the Text widget
- txt.configure(yscrollcommand=scrollbar.set)
- txt.pack(side=LEFT, fill=BOTH, expand=True)
- # Checkbox for including date
- date_checkbox_var = BooleanVar()
- date_checkbox_var.set(True) # Default to include the date
- date_checkbox = Checkbutton(window, text="Include Date in Entry", variable=date_checkbox_var)
- date_checkbox.grid(column=0, row=2, sticky="w", padx=10)
- # Buttons in one row
- frame_buttons = Frame(window)
- frame_buttons.grid(column=0, row=3, columnspan=3, pady=10)
- btn_write = Button(frame_buttons, text="Write to Diary", bg="blue", fg="white", width=20, command=diarywrite)
- btn_write.pack(side=LEFT, padx=5)
- btn_open = Button(frame_buttons, text="Open Diary", bg="yellow", fg="black", width=20, command=diaryopen)
- btn_open.pack(side=LEFT, padx=5)
- btn_new = Button(frame_buttons, text="Create New Diary", bg="green", fg="white", width=20, command=createnewdiary)
- btn_new.pack(side=LEFT, padx=5)
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement