'How to change the picture to the buttons in Tkinter when selecting from Combobox

enter image description hereI am finishing writing the program code for the Translator application. I have a text-to-speech button, but since voice acting is only available in Russian and English, I want to change the icon to (no_voice_acting) and make the button inactive when the user selects other languages, for example German

import googletrans
from languages import lang, lang_to_translate
from tkinter import *
from tkinter import ttk
from tkinter.font import Font
import pyperclip as clipboard
import pyttsx3

after_id = None
language_abbreviation = []

def speaking_voice_text1():
    global choice_language1
    choice_language1 = language_selection1.get()
    text = text1.get('1.0', END)
    speech_synthesis = pyttsx3.init('sapi5')
    voices = speech_synthesis.getProperty('voices')
    if choice_language1 == 'Russian':
        speech_synthesis.setProperty('voice', voices[0].id)
        speech_synthesis.setProperty('volume', 0.5)
        speech_synthesis.say(f'{text}')
        speech_synthesis.runAndWait()
        speech_synthesis.stop()
    elif choice_language1 == 'English':
        speech_synthesis.setProperty('voice', voices[1].id)
        speech_synthesis.setProperty('volume', 0.8)
        speech_synthesis.say(f'{text}')
        speech_synthesis.runAndWait()
        speech_synthesis.stop()

def speaking_voice_text2():
    global choice_language2
    choice_language2 = language_selection2.get()
    text = text2.get('1.0', END)
    speech_synthesis = pyttsx3.init('sapi5')
    voices = speech_synthesis.getProperty('voices')

    if choice_language2 == 'Russian':
        speech_synthesis.setProperty('voice', voices[0].id)
        speech_synthesis.setProperty('volume', 0.5)
        speech_synthesis.say(f'{text}')
        speech_synthesis.runAndWait()
        speech_synthesis.stop()
    elif choice_language2 == 'English':
        speech_synthesis.setProperty('voice', voices[1].id)
        speech_synthesis.setProperty('volume', 0.8)
        speech_synthesis.say(f'{text}')
        speech_synthesis.runAndWait()
        speech_synthesis.stop()

def copy_text(string):
    text = text2.get('1.0', END).strip()
    clipboard.copy(text)

def clear_text():
    text1.delete('1.0', END)
    text2.delete('1.0', END)

def defocus(event):
    event.widget.master.focus_set()

def get_choice1(event):
    global choice_language1
    choice_language1 = language_selection1.get()
    print(choice_language1)

def get_choice2(event):
    global choice_language2
    global text1
    choice_language2 = language_selection2.get()
    if not text1:
        schedule_translation()

def ex_button():
    c1 = language_selection1.get()
    c2 = language_selection2.get()
    label1.configure(text=c1)
    label2.configure(text=c2)
    language_selection1.set(c2)
    language_selection2.set(c1)
    get_choice1('event')
    get_choice2('event')

    text = text2.get('1.0', END).strip()
    text2.delete('1.0', END)
    text1.delete('1.0', END)
    text1.insert(1.0, text)

    schedule_translation()

def do_translation():
    global language_abbreviation
    global choice_language2

    try:
        dest = lang.index(choice_language2)
        language_abbreviation = lang_to_translate[dest]

        text = text1.get('1.0', END).strip()

        translator = googletrans.Translator()
        translated_text = translator.translate(text, dest=language_abbreviation)

        text2.delete('1.0', END)
        text2.insert(END, translated_text.text)
    except:
        pass

def schedule_translation(event=None):
    global after_id
    if after_id is not None:
        root.after_cancel(after_id)
    after_id = root.after(500, do_translation)

root = Tk()

font = Font(family="Comfortaa", size=10)
root.option_add("*TCombobox*Listbox*Font", font)

app_width = 800
app_height = 500
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width / 2) - (app_width / 2)
y = (screen_height / 2) - (app_height / 2)

root.title('Переводчик')
root['bg'] = '#1D1B26'
root.geometry(f'{app_width}x{app_height}+{int(x)}+{int(y)}')
root.resizable(width=False, height=False)

language_selection1 = ttk.Combobox(root, values=lang, font='Comfortaa 10')
language_selection1.current(1)
language_selection1.place(relx=0.16, y=50)
language_selection1.bind('<<ComboboxSelected>>', get_choice1)
language_selection1.bind('<FocusIn>', defocus)

label1 = Label(root)

exchange_button = PhotoImage(file='transfer.png')
img_label = Label(image=exchange_button)
exchange_button = exchange_button.subsample(2, 2)
exchange_button1 = Button(root, image=exchange_button,
                          highlightthickness=0, bd=0, bg='#1D1B26',
                          activebackground='#1D1B26', command =ex_button)
exchange_button1.place(relx=0.49, y=48)

language_selection2 = ttk.Combobox(root, values=lang, font='Comfortaa 10')
language_selection2.set('Выберите язык')
language_selection2.place(relx=0.66, y=50)
language_selection2.bind('<<ComboboxSelected>>', get_choice2)
language_selection2.bind('<FocusIn>', defocus)

first_frame = Frame(root, bg='Black')
first_frame.place(x=41, y=100, width=250, height=200)

text1 = Text(first_frame, bg='#FFC0CB', wrap=WORD, font='Comfortaa 12', cursor='arrow')
text1.bind('<Any-KeyRelease>', schedule_translation)
text1.pack(side="left", fill="both", expand=True)

clear_button = PhotoImage(file='clear_button.png')
clear_button_label = Label(image=clear_button)
clear_button = clear_button.subsample(25, 25)
clear_button1 = Button(text1, image=clear_button,
                       highlightthickness=0, bd=0, bg='#FFC0CB',
                       activebackground='#FFC0CB', command=clear_text)
clear_button1.pack(side='top', anchor='e', pady=5, padx=5)

speak_button_text1 = PhotoImage(file='speak_text1.png')
speak_button_text1_label = Label(image=speak_button_text1)
speak_button_text1 = speak_button_text1.subsample(2, 2)
speak_button_text1_1 = Button(text1, image=speak_button_text1,
                       highlightthickness=0, bd=0, bg='#FFC0CB',
                       activebackground='#FFC0CB', command=speaking_voice_text1)
speak_button_text1_1.pack(side='bottom', anchor='w',pady=5, padx=5)

label2 = Label(root)

second_frame = Frame(root, bg='Black')
second_frame.place(x=528, y=100, width= 250, height=200) #441

text2 = Text(second_frame, bg='#FFC0CB', wrap=WORD, font='Comfortaa 12', cursor='arrow')
text2.pack(side="left", fill="both", expand=True)

copying_text = PhotoImage(file='copying_text.png')
copying_text_label = Label(image=copying_text)
copying_text = copying_text.subsample(18, 18)
copying_text1 = Button(text2, image=copying_text,
                       highlightthickness=0, bd=0, bg='#FFC0CB',
                       activebackground='#FFC0CB', command=copy_text)
copying_text1.pack(side='top', anchor='e')

speak_button_text2 = PhotoImage(file='speak_text2.png')
speak_button_text2_label = Label(image=speak_button_text2)
speak_button_text2 = speak_button_text2.subsample(2, 2)
speak_button_text2_1 = Button(text2, image=speak_button_text2,
                       highlightthickness=0, bd=0, bg='#FFC0CB',
                       activebackground='#FFC0CB', command=speaking_voice_text2)
speak_button_text2_1.pack(side='bottom', anchor='w', pady=5, padx=5)

root.mainloop()

I will also attach languages for translation from the file "languages.py "

lang = ['Belarusian', 'English', 'German', 'Italian', 'Japanese', 'Kazakh', 'Kyrgyz', 'Norwegian', 'Polish', 'Russian', 'Spanish', 'Swedish', 'Turkish', 'Ukrainian', ' ']

lang_to_translate =['be', 'en', 'de', 'it', 'ja', 'kk', 'ky', 'no', 'pl', 'ru', 'es', 'sv', 'tr', 'uk', ' ']



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source