Certainly! Here's an example of a word counter program in Python that includes a graphical user interface (GUI) using the tkinter library:
```python
import tkinter as tk
def count_words():
text = text_entry.get("1.0", tk.END)
words = text.split()
count = len(words)
count_label.configure(text=f"Word Count: {count}")
# Create the main window
window = tk.Tk()
window.title("Word Counter")
# Create a text entry widget
text_entry = tk.Text(window, height=10, width=50)
text_entry.pack()
# Create a button to count words
count_button = tk.Button(window, text="Count Words", command=count_words)
count_button.pack()
# Create a label to display the word count
count_label = tk.Label(window, text="Word Count: 0")
count_label.pack()
# Start the main event loop
window.mainloop()
```
This code uses the tkinter library to create a GUI window with a text entry widget, a button to count words, and a label to display the word count. When the user clicks the "Count Words" button, the `count_words` function is called. It retrieves the text from the text entry widget, splits it into words, counts the number of words, and updates the count label accordingly.
To run this code, you'll need to have Python installed along with the tkinter library. Save the code in a file with a `.py` extension (e.g., `word_counter.py`), and run it using a Python interpreter.
Please note that this is a basic example and may not handle all edge cases or advanced functionality. You can further enhance it based on your specific requirements.
Post a Comment