You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python调用Google Translate API时出现AttributeError问题求助

解决Google Translator库的AttributeError异常

问题根源

你遇到的AttributeError: 'NoneType' object has no attribute 'group'是因为官方googletrans库长期未维护,与谷歌翻译的最新API接口不兼容,导致无法正确获取用于验证的TKK令牌。另外你的代码还存在语言参数不匹配问题(下拉框使用语言名称,而翻译接口需要语言代码)。

解决方案

1. 安装修复后的第三方维护版本

官方googletrans已停止更新,第三方维护的4.0.0-rc1版本修复了令牌获取问题:

  • 卸载旧版本:
    pip uninstall -y googletrans
    
  • 安装修复版本:
    pip install googletrans==4.0.0-rc1
    

2. 修正代码中的语言参数匹配问题

原代码下拉框使用语言名称(如ENGLISH),但翻译接口需要传入语言代码(如en),需添加名称到代码的映射,同时补充异常处理和输入校验:

修改后的完整代码:

from tkinter import *
from tkinter import ttk, messagebox
import googletrans
from googletrans import Translator

root = Tk()
root.title("Google Translator")
root.geometry("1080x400")
root.resizable(False, False)
root.configure(background="white")

# 新增:语言名称到代码的映射,统一转为大写匹配下拉框
lang_name_to_code = {v.upper(): k for k, v in googletrans.LANGUAGES.items()}

def label_change():
    c = combo1.get()
    c1 = combo2.get()
    label1.configure(text=c)
    # 处理默认的SELECT LANGUAGE提示
    if c1 != "SELECT LANGUAGE":
        label2.configure(text=c1)
    root.after(1000, label_change)

def translate_now():
    text_= text1.get(1.0, END).strip()
    # 空文本校验
    if not text_:
        messagebox.showwarning("提示", "请输入要翻译的文本")
        return
    # 目标语言选择校验
    if combo2.get() == "SELECT LANGUAGE":
        messagebox.showwarning("提示", "请选择目标语言")
        return
    
    t1 = Translator(service_urls=['translate.google.com'])
    try:
        # 通过映射获取语言代码
        src_lang = lang_name_to_code[combo1.get()]
        dest_lang = lang_name_to_code[combo2.get()]
        trans_text = t1.translate(text_, src=src_lang, dest=dest_lang)
        text2.delete(1.0, END)
        text2.insert(END, trans_text.text)
    except Exception as e:
        messagebox.showerror("错误", f"翻译失败:{str(e)}")

language = googletrans.LANGUAGES
languageV = [v.upper() for v in language.values()]  # 统一转为大写

#combobox1
combo1 = ttk.Combobox(root, values=languageV, font="Roboto 14", state="r")
combo1.place(x=110, y=20)
combo1.set("ENGLISH")

label1 = Label(root, text="ENGLISH", font="seoge 30 bold", bg="white", width=18, bd=5, relief=GROOVE)
label1.place(x=10, y=50)

#combobox2
combo2 = ttk.Combobox(root, values=languageV, font="Roboto 14", state="r")
combo2.place(x=730, y=20)
combo2.set("SELECT LANGUAGE")

label2 = Label(root, text="SELECT LANGUAGE", font="seoge 30 bold", bg="white", width=18, bd=5, relief=GROOVE)
label2.place(x=620, y=50)

#frame1
f = Frame(root, bg="Black", bd=5)
f.place(x=10, y=118, width=440, height=210)
text1 = Text(f, font="Roboto 20", bg="white", relief=GROOVE, wrap=WORD)
text1.place(x=0, y=0, width=430, height=200)

scrollbar1 = Scrollbar(f)
scrollbar1.pack(side="right", fill='y')

scrollbar1.configure(command=text1.yview)
text1.configure(yscrollcommand=scrollbar1.set)

#frame2
f1 = Frame(root, bg="Black", bd=5)
f1.place(x=620, y=118, width=440, height=210)
text2 = Text(f1, font="Roboto 20", bg="white", relief=GROOVE, wrap=WORD)
text2.place(x=0, y=0, width=430, height=200)

scrollbar2 = Scrollbar(f1)
scrollbar2.pack(side="right", fill='y')

scrollbar2.configure(command=text2.yview)
text2.configure(yscrollcommand=scrollbar2.set)

#translate button
translate = Button(root, text="Translate", font=("Roboto", 15), activebackground="white", cursor="hand2", bd=1,
                   width=10, height=2, bg="black", fg="white", command=translate_now)
translate.place(x=476, y=250)

label_change()
root.mainloop()

3. 额外优化说明

  • 新增空文本和未选语言的校验,避免无效请求触发异常
  • 统一语言名称大小写,消除映射匹配的大小写问题
  • 添加全局异常捕获,友好展示错误信息

内容的提问来源于stack exchange,提问作者Anonymous

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 10:25:45