from tkinter import Tk, Label, Entry, Button
from googletrans import Translator
def translate_text():
text = entry.get()
translator = Translator()
translation = translator.translate(text, dest='en' if is_chinese(text) else 'zh-CN')
result_label.config(text=translation.text)
def is_chinese(text):
for char in text:
if '\u4e00' <= char <= '\u9fff':
return True
return False
root = Tk()
root.title("中英文翻译器")
# 输入标签和输入框
input_label = Label(root, text="请输入要翻译的内容:")
input_label.pack()
entry = Entry(root, width=50)
entry.pack()
# 翻译按钮
translate_button = Button(root, text="翻译", command=translate_text)
translate_button.pack()
# 结果显示标签
result_label = Label(root, text="")
result_label.pack()
root.mainloop()