Language Detection in Python using Tkinter
Language detection is the process of identifying the language of a given text. In Python, we can use the langdetect
library to detect the language of a given text.
To create a GUI for language detection using Tkinter, we can follow these steps:
- Install the
langdetect
library using pip:
pip install langdetect
- Import the necessary modules:
from tkinter import *
from langdetect import detect
- Create a Tkinter window:
root = Tk()
root.title("Language Detection")
root.geometry("300x200")
- Create a label and an entry widget for the user to input the text:
label = Label(root, text="Enter text:")
label.pack()
text_entry = Entry(root, width=30)
text_entry.pack()
- Create a function to detect the language of the input text:
def detect_language():
text = text_entry.get()
language = detect(text)
result_label.config(text="Detected language: " + language)
- Create a button to trigger the language detection function:
detect_button = Button(root, text="Detect Language", command=detect_language)
detect_button.pack()
- Create a label to display the detected language:
result_label = Label(root, text="")
result_label.pack()
- Run the Tkinter event loop:
root.mainloop()
Here's the complete code:
from tkinter import *
from langdetect import detect
root = Tk()
root.title("Language Detection")
root.geometry("300x200")
label = Label(root, text="Enter text:")
label.pack()
text_entry = Entry(root, width=30)
text_entry.pack()
def detect_language():
text = text_entry.get()
language = detect(text)
result_label.config(text="Detected language: " + language)
detect_button = Button(root, text="Detect Language", command=detect_language)
detect_button.pack()
result_label = Label(root, text="")
result_label.pack()
root.mainloop()
When the user enters some text and clicks the "Detect Language" button, the program will detect the language of the input text and display it in the "Detected language" label.