Advertisement
Python253

custom_input_box_to_list

May 23rd, 2024 (edited)
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.83 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: custom_input_box_to_list.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script creates a custom input box using Tkinter.
  10.    - Users can enter data in the input box and press Enter to add each entry.
  11.    - Once all data has been entered, users can click the Submit button to finish.
  12.    - It provides an option to save the entered data to a text file named "output_data_entry.txt" in the current working directory.
  13.  
  14. Requirements:
  15.    - Python 3.x
  16.    - Tkinter library
  17.  
  18. Functions:
  19.    - show_custom_input_box():
  20.        Displays the custom input box and returns the user inputs as a list.
  21.  
  22.    - print_user_inputs(inputs):
  23.        Prints the user inputs in a formatted numbered list.
  24.  
  25.    - save_user_inputs(inputs):
  26.        Saves the user inputs to a text file named "output_data_entry.txt" in the current working directory.
  27.  
  28. Usage:
  29.    - Run the script.
  30.    - Enter data in the input box and press Enter for each entry.
  31.    - Click the Submit button when all data has been entered.
  32.  
  33. Example Output:
  34.  
  35.    User inputs:
  36.    1: First input
  37.    2: Second input
  38.    3: Third input
  39.  
  40. Additional Notes:
  41.    - The script provides a simple way to gather multiple inputs from the user using a graphical interface.
  42.    - Ideal for data entry tasks where manual input is required.
  43. """
  44.  
  45. import tkinter as tk
  46.  
  47. class CustomInputBox:
  48.     def __init__(self, root):
  49.         """
  50.        Initialize the CustomInputBox class.
  51.  
  52.        Parameters:
  53.            root (tk.Tk): The root Tkinter window.
  54.        """
  55.         self.root = root
  56.         self.root.title("Data Entry Form")
  57.         self.root.geometry("400x150")
  58.         self.root.resizable(False, False)
  59.         self.root.eval('tk::PlaceWindow . center')
  60.  
  61.         self.label = tk.Label(root, text="Enter Data Below & Press 'Enter' To Add Each Entry.\nClick 'Submit' When All Data Has Been Entered:")
  62.         self.label.pack(pady=10)
  63.  
  64.         self.text_box = tk.Entry(root, width=40)
  65.         self.text_box.pack(pady=5)
  66.         self.text_box.focus_set()
  67.         self.text_box.bind("<Return>", lambda event: self.add_input())
  68.  
  69.         self.button_frame = tk.Frame(root)
  70.         self.button_frame.pack(pady=20)
  71.  
  72.         self.submit_button = tk.Button(self.button_frame, text="Submit", width=10, command=self.submit)
  73.         self.submit_button.grid(row=0, column=0, padx=5)
  74.  
  75.         self.exit_button = tk.Button(self.button_frame, text="Exit", width=10, command=self.exit_program)
  76.         self.exit_button.grid(row=0, column=1, padx=5)
  77.  
  78.         self.inputs = []
  79.  
  80.     def add_input(self):
  81.         """
  82.        Add user input to the inputs list.
  83.        """
  84.         input_text = self.text_box.get()
  85.         if input_text:
  86.             self.inputs.append(input_text)
  87.             self.text_box.delete(0, tk.END)
  88.             self.text_box.focus_set()
  89.  
  90.     def submit(self):
  91.         """
  92.        Finish data entry and close the window.
  93.        """
  94.         if self.text_box.get():
  95.             self.inputs.append(self.text_box.get())
  96.         self.root.destroy()
  97.  
  98.     def exit_program(self):
  99.         """
  100.        Exit the program without saving.
  101.        """
  102.         self.root.destroy()
  103.  
  104. def show_custom_input_box():
  105.     """
  106.    Display the custom input box and return the user inputs as a list.
  107.  
  108.    Returns:
  109.        list: A list containing user inputs.
  110.    """
  111.     root = tk.Tk()
  112.     input_box = CustomInputBox(root)
  113.     root.mainloop()
  114.     return input_box.inputs
  115.  
  116. def print_user_inputs(inputs):
  117.     """
  118.    Print the user inputs in a formatted manner.
  119.  
  120.    Parameters:
  121.        inputs (list): A list containing user inputs.
  122.    """
  123.     if inputs:
  124.         print("User inputs:")
  125.         for i, input_data in enumerate(inputs, start=1):
  126.             print(f"{i}: {input_data}")
  127.     else:
  128.         print("User cancelled the input.\n\nExiting Program... Goodbye!\n")
  129.  
  130. def save_user_inputs(inputs):
  131.     """
  132.    Save the user inputs to a text file named "output_data_entry.txt" in the current working directory.
  133.  
  134.    Parameters:
  135.        inputs (list): A list containing user inputs.
  136.    """
  137.     with open("output_data_entry.txt", "w", encoding="utf-8") as file:
  138.         for i, input_data in enumerate(inputs, start=1):
  139.             file.write(f"{i}: {input_data}\n")
  140.  
  141. if __name__ == "__main__":
  142.     user_inputs = show_custom_input_box()
  143.     print_user_inputs(user_inputs)
  144.     if user_inputs:
  145.         choice = input("\nDo you want to save the data to a file?\n\n1: Yes\n2: No\n\nMake your selection (1 or 2): ")
  146.         if choice == '1':
  147.             save_user_inputs(user_inputs)
  148.             print("\nData saved to 'output_data_entry.txt'.\n\nExiting Program... Goodbye!\n")
  149.         elif choice == '2':
  150.             print("Data not saved.")
  151.         else:
  152.             print("Invalid input. Data not saved.")
  153.  
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement