[英]Expected an intended block (Python TK library)
我正在嘗試制作一個簡單的翻譯器。 代碼的重要部分:
tkVar = tk.StringVar(root)
choices = { "de":'German', "ru":'Russian', "en":'English', "hr":'Croatian', "cz":'Czech', "sv":'Swedish', "pl":'Polish' }
tkVar.set('de')
popUpMenu = tk.OptionMenu(mainframe, tkVar, *choices)
tk.Label(mainframe, text="Choose a language").grid(row=1,column=1)
popUpMenu.grid(row=2,column=1)
我的功能(目前只有俄文和德文翻譯):
def rustrans():
word = entry.get()
translator = Translator(service_urls=["translate.google.com"])
translation1 = translator.translate(word, dest="ru")
label1 = tk.Label(root,text=f"Translated in Russian : {translation1.text}", bg="yellow")
label1.grid(row=2,column=0)
def detrans():
word = entry.get()
translator = Translator(service_urls=["translate.google.com"])
translation2=translator.translate(word, dest="de")
label2 = tk.Label(root, text=f"Translated in German : {translation2.text}", bg = "yellow")
label2.grid(row=2,column=0)
我嘗試使用 if 語句將英語翻譯成上述語言:
if choices["de"]:
button = tk.Button(root, text="Translate", command=detrans)
button.grid(row=1,column=2)
if choices["ru"]:
button1 = tk.Button(root, text="Translate", command=rustrans)
button1.grid(row=1,column=2)
崇高的文字說錯誤在第 46 行,又名
button1 = tk.Button(root, text="Translate", command=rustrans)
我在這里是因為我想知道如果發生“預期的阻塞”我應該怎么做。
編輯:如果選擇 ["ru"] 部分代碼(通過注釋),則在沒有整體的情況下運行該程序運行得很好。
您只是忘記在 if 選項 ['ru'] 之后的兩行開頭添加縮進:
if choices["de"]:
button = tk.Button(root, text="Translate", command=detrans)
button.grid(row=1,column=2)
if choices["ru"]:
button1 = tk.Button(root, text="Translate", command=rustrans)
button1.grid(row=1,column=2)
那么它應該適合你
您的代碼中的問題在這里:
if choices["ru"]:
button1 = tk.Button(root, text="Translate", command=rustrans)
button1.grid(row=1,column=2)
您應該將其重寫為:
if choices["ru"]: # indent the code after the colon
button1 = tk.Button(root, text="Translate", command=rustrans)
button1.grid(row=1,column=2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.