Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: custom_input_box_to_list.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - This script creates a custom input box using Tkinter.
- - Users can enter data in the input box and press Enter to add each entry.
- - Once all data has been entered, users can click the Submit button to finish.
- - It provides an option to save the entered data to a text file named "output_data_entry.txt" in the current working directory.
- Requirements:
- - Python 3.x
- - Tkinter library
- Functions:
- - show_custom_input_box():
- Displays the custom input box and returns the user inputs as a list.
- - print_user_inputs(inputs):
- Prints the user inputs in a formatted numbered list.
- - save_user_inputs(inputs):
- Saves the user inputs to a text file named "output_data_entry.txt" in the current working directory.
- Usage:
- - Run the script.
- - Enter data in the input box and press Enter for each entry.
- - Click the Submit button when all data has been entered.
- Example Output:
- User inputs:
- 1: First input
- 2: Second input
- 3: Third input
- Additional Notes:
- - The script provides a simple way to gather multiple inputs from the user using a graphical interface.
- - Ideal for data entry tasks where manual input is required.
- """
- import tkinter as tk
- class CustomInputBox:
- def __init__(self, root):
- """
- Initialize the CustomInputBox class.
- Parameters:
- root (tk.Tk): The root Tkinter window.
- """
- self.root = root
- self.root.title("Data Entry Form")
- self.root.geometry("400x150")
- self.root.resizable(False, False)
- self.root.eval('tk::PlaceWindow . center')
- self.label = tk.Label(root, text="Enter Data Below & Press 'Enter' To Add Each Entry.\nClick 'Submit' When All Data Has Been Entered:")
- self.label.pack(pady=10)
- self.text_box = tk.Entry(root, width=40)
- self.text_box.pack(pady=5)
- self.text_box.focus_set()
- self.text_box.bind("<Return>", lambda event: self.add_input())
- self.button_frame = tk.Frame(root)
- self.button_frame.pack(pady=20)
- self.submit_button = tk.Button(self.button_frame, text="Submit", width=10, command=self.submit)
- self.submit_button.grid(row=0, column=0, padx=5)
- self.exit_button = tk.Button(self.button_frame, text="Exit", width=10, command=self.exit_program)
- self.exit_button.grid(row=0, column=1, padx=5)
- self.inputs = []
- def add_input(self):
- """
- Add user input to the inputs list.
- """
- input_text = self.text_box.get()
- if input_text:
- self.inputs.append(input_text)
- self.text_box.delete(0, tk.END)
- self.text_box.focus_set()
- def submit(self):
- """
- Finish data entry and close the window.
- """
- if self.text_box.get():
- self.inputs.append(self.text_box.get())
- self.root.destroy()
- def exit_program(self):
- """
- Exit the program without saving.
- """
- self.root.destroy()
- def show_custom_input_box():
- """
- Display the custom input box and return the user inputs as a list.
- Returns:
- list: A list containing user inputs.
- """
- root = tk.Tk()
- input_box = CustomInputBox(root)
- root.mainloop()
- return input_box.inputs
- def print_user_inputs(inputs):
- """
- Print the user inputs in a formatted manner.
- Parameters:
- inputs (list): A list containing user inputs.
- """
- if inputs:
- print("User inputs:")
- for i, input_data in enumerate(inputs, start=1):
- print(f"{i}: {input_data}")
- else:
- print("User cancelled the input.\n\nExiting Program... Goodbye!\n")
- def save_user_inputs(inputs):
- """
- Save the user inputs to a text file named "output_data_entry.txt" in the current working directory.
- Parameters:
- inputs (list): A list containing user inputs.
- """
- with open("output_data_entry.txt", "w", encoding="utf-8") as file:
- for i, input_data in enumerate(inputs, start=1):
- file.write(f"{i}: {input_data}\n")
- if __name__ == "__main__":
- user_inputs = show_custom_input_box()
- print_user_inputs(user_inputs)
- if user_inputs:
- choice = input("\nDo you want to save the data to a file?\n\n1: Yes\n2: No\n\nMake your selection (1 or 2): ")
- if choice == '1':
- save_user_inputs(user_inputs)
- print("\nData saved to 'output_data_entry.txt'.\n\nExiting Program... Goodbye!\n")
- elif choice == '2':
- print("Data not saved.")
- else:
- print("Invalid input. Data not saved.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement