Here is the code for translating from both Russian to English and vice versa:
import tkinter as tk
window = tk.Tk()
window.title('Boom')
window.geometry('300x200+50+75')
window.resizable(True, True)
# Create two dictionaries: one for Russian to English, the other for English to Russian
fruit_r_to_e = {'яблоко':'apple', "ананас":'pineapple'}
fruit_e_to_r = {english: russian for russian, english in fruit_r_to_e.items()}
mystring = tk.StringVar(window)
my_string1 = tk.StringVar(window)
def getvalue():
global mystring, my_string1, fruit_e_to_r, fruit_r_to_e
string = mystring.get()
# If the string is in English, translate it to Russian
if string in fruit_e_to_r:
print(fruit_e_to_r[string])
# If the string is in Russian, translate it to English
elif string in fruit_r_to_e:
print(fruit_r_to_e[string])
else:
print("This word is not in the dictionary.")
def info():
global my_string1
print(my_string1())
entry = tk.Entry(window, textvariable=mystring, bg='white', fg='black', width=200)
entry.pack()
button = tk.Button(window,
text='Translate',
bg='blue',
fg='black',
width=15,
height=3,
command=getvalue
)
button.pack()
entry = tk.Entry(window, textvariable=my_string1, bg='white', fg='black', width=200)
entry.pack()
window.mainloop()
I created two dictionaries, one for each translation direction. When getvalue() is called, it checks if the word is in the English dictionary: if string in fruit_e_to_r:, and translates it to Russian if it is. If it isn't, it checks if it is in the Russian dictionary: elif string in fruit_r_to_e:, and translates it to English if it is. If it is not in either dictionary: else:, it shows a message saying that the word is not in either dictionary.
I made a few other changes to the code as well. I removed the from tkinter import *, because you already have tkinter imported in the next line: import tkinter as tk, and because wildcard imports (using from <package> import *) are discouraged.
I also noticed that when you create the widgets, you call .pack() when you assign the widgets to variables, for example you used entry = tk.Entry(...).pack(). This is a problem, because the pack() method returns None, so you won't be able to use the widgets later in the program. You need to call entry = tk.Entry(...), and then call entry.pack() on the next line. This way, the entry variable actually contains an instance of tkinter.Entry, instead of None.
You'll notice that I also used the global statement in the functions. This is not required (unless you're assigning variables), but it's generally a good idea. global tells the function that it's using variables that were defined outside the function. So, global mystring, fruit_e_to_r, fruit_r_to_e tells getvalue() that mystring, fruit_e_to_r, and fruit_r_to_e are all variables that were created outside of getvalue().
window = tk.Tk(). Don't mix normal lines with functions.