简体   繁体   中英

Expected an intended block (Python TK library)

I am trying to make a simple translator. The important part of the code:

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)

My functions (russian & german translation only for now):

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)

My attempt at trying to translate from English to said language by using an if statement:

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)

Sublime text says the error is on line 46 aka

button1 = tk.Button(root, text="Translate", command=rustrans)

I am here cause I wanna find out what should I do in case "expected an intended block" occurs.

EDIT: running WITHOUT the WHOLE if choices["ru"] part of the code (by commenting it) the program runs just fine.

You just forgot to put the indentation at the start of both the lines after the if choices['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)

Then it should work for you

The issue in your code is here:

if choices["ru"]:
button1 = tk.Button(root, text="Translate", command=rustrans)
button1.grid(row=1,column=2)

You should rewrite that to look like:

if choices["ru"]: # indent the code after the colon
    button1 = tk.Button(root, text="Translate", command=rustrans)
    button1.grid(row=1,column=2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM