import os
import tkinter as tk
from tkinter import messagebox

def count_words(text):
    words = text.split()
    return len(words)

def count_characters(text):
    return len(text)

def count_lines(text):
    lines = text.split('\n')
    return len(lines)

def read_text():
    choice = input("Do you want to input text or read from a file? (input/file): ")
    if choice.lower() == 'input':
        text = input("Enter your text: ")
        return text
    elif choice.lower() == 'file':
        filename = input("Enter the filename: ")
        if not os.path.exists(filename):
            messagebox.showerror("Error", "File not found.")
            return None
        try:
            with open(filename, 'r') as file:
                text = file.read()
            return text
        except Exception as e:
            messagebox.showerror("Error", f"Error reading file: {e}")
            return None
    else:
        messagebox.showerror("Error", "Invalid choice.")
        return None

def display_results(text):
    num_words = count_words(text)
    num_characters = count_characters(text)
    num_lines = count_lines(text)

    result_text = f"Number of words: {num_words}\nNumber of characters (including spaces): {num_characters}\nNumber of lines: {num_lines}"
    messagebox.showinfo("Results", result_text)

def process_text():
    text = read_text()
    if text is not None:
        display_results(text)

def main():
    root = tk.Tk()
    root.title("Text Counter")

    label = tk.Label(root, text="Welcome to Text Counter!")
    label.pack()

    button = tk.Button(root, text="Process Text", command=process_text)
    button.pack()

    root.mainloop()

if __name__ == "__main__":
    main()