Advertisement
Najeebsk

MY-DIARY.pyw

Nov 21st, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.66 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter import messagebox
  3. import os
  4. import datetime
  5.  
  6.  
  7. def diarywrite():
  8.     """Writes input text to the diary."""
  9.     inputtxt = txt.get("1.0", END).strip()  # Get text from the Text widget
  10.     if not inputtxt:
  11.         messagebox.showwarning("Input Required", "Please enter some text to write to the diary.")
  12.         return
  13.  
  14.     diary_entry = inputtxt
  15.     if date_checkbox_var.get():
  16.         date_formatted = get_date_formatted()
  17.         diary_entry = f"{date_formatted}{diary_entry}\n\n"
  18.  
  19.     with open("MY-DIARY.txt", "a") as file:
  20.         file.write(diary_entry)
  21.     messagebox.showinfo("Success", "Your entry has been recorded!")
  22.     txt.delete("1.0", END)  # Clear the text field
  23.  
  24.  
  25. def diaryopen():
  26.     """Opens the diary in the default text editor."""
  27.     if not os.path.exists("MY-DIARY.txt"):
  28.         with open("MY-DIARY.txt", "w") as file:
  29.             file.write("")  # Create an empty file if it doesn't exist
  30.     os.system("notepad MY-DIARY.txt")
  31.  
  32.  
  33. def createnewdiary():
  34.     """Creates a new diary file."""
  35.     new_diary_name = txt.get("1.0", END).strip()
  36.     if not new_diary_name:
  37.         messagebox.showwarning("Input Required", "Please enter a name for the new diary.")
  38.         return
  39.  
  40.     new_diary_name = new_diary_name.replace(" ", "_").replace(".txt", "") + ".txt"
  41.     if os.path.exists(new_diary_name):
  42.         messagebox.showwarning("File Exists", f"A file named '{new_diary_name}' already exists!")
  43.         return
  44.  
  45.     with open(new_diary_name, "w") as file:
  46.         file.write("")  # Create an empty diary
  47.     os.system(f"notepad {new_diary_name}")
  48.     messagebox.showinfo("Success", f"New diary '{new_diary_name}' has been created!")
  49.     txt.delete("1.0", END)
  50.  
  51.  
  52. def get_date_formatted():
  53.     """Returns the current date and time formatted for diary entries."""
  54.     x = datetime.datetime.now()
  55.     str_time = x.strftime("[%H:%M:%S] ")
  56.     str_day = x.strftime("%A")
  57.     str_date = x.strftime("%d")
  58.     str_month = x.strftime("%B")
  59.     str_year = str(x.year)
  60.     return f"{str_time}{str_day}, {str_month} {str_date}, {str_year}: \n"
  61.  
  62.  
  63. # GUI Setup
  64. window = Tk()
  65. window.title("Welcome To The Najeeb Diary!")
  66. window.geometry("900x500")
  67.  
  68. # Large Text Field with Scrollbar
  69. lbl = Label(window, text="Hello Najeeb! Write Diary Here:-", font=("Times New Roman Bold", 18))
  70. lbl.grid(column=0, row=0, padx=10, pady=10, sticky="w")
  71.  
  72. frame_txt = Frame(window)
  73. frame_txt.grid(column=0, row=1, columnspan=3, padx=10, pady=10)
  74.  
  75. # Create a Text widget
  76. txt = Text(frame_txt, width=108, height=21, bd=5, wrap=WORD)
  77.  
  78. # Create a Scrollbar widget
  79. scrollbar = Scrollbar(frame_txt, orient=VERTICAL, command=txt.yview)
  80. scrollbar.pack(side=RIGHT, fill=Y)
  81.  
  82. # Attach the Scrollbar to the Text widget
  83. txt.configure(yscrollcommand=scrollbar.set)
  84. txt.pack(side=LEFT, fill=BOTH, expand=True)
  85.  
  86. # Checkbox for including date
  87. date_checkbox_var = BooleanVar()
  88. date_checkbox_var.set(True)  # Default to include the date
  89. date_checkbox = Checkbutton(window, text="Include Date in Entry", variable=date_checkbox_var)
  90. date_checkbox.grid(column=0, row=2, sticky="w", padx=10)
  91.  
  92. # Buttons in one row
  93. frame_buttons = Frame(window)
  94. frame_buttons.grid(column=0, row=3, columnspan=3, pady=10)
  95.  
  96. btn_write = Button(frame_buttons, text="Write to Diary", bg="blue", fg="white", width=20, command=diarywrite)
  97. btn_write.pack(side=LEFT, padx=5)
  98.  
  99. btn_open = Button(frame_buttons, text="Open Diary", bg="yellow", fg="black", width=20, command=diaryopen)
  100. btn_open.pack(side=LEFT, padx=5)
  101.  
  102. btn_new = Button(frame_buttons, text="Create New Diary", bg="green", fg="white", width=20, command=createnewdiary)
  103. btn_new.pack(side=LEFT, padx=5)
  104.  
  105. window.mainloop()
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement