0

Good evening forum. Let me get help from you. I need to output the key from the dictionary to the terminal, I work through GUI Tkinter and tk.StringVar. For example, there is a pair of 'apple' and 'pineapple', and because of the spelling in the term 'apple' I knocked out 'pineapple'. Help please. Code below! Sincerely, Vladimir!

from tkinter import *
import tkinter as tk

window = tk.Tk()

window.title('Boom')
window.geometry('300x200+50+75')
window.resizable(True, True)

def info():
    print(info)
fruit = {'яблоко':'apple', "ананас":'pineapple'}

mystring = tk.StringVar(window)
def getvalue():
    print(mystring.get())
entry = tk.Entry(window, textvariable=mystring, bg='white', fg='black', width=200).pack()
button = tk.Button(window, 
    text='Translate',
    bg='blue',
    fg='black', width=15, height=3, command=getvalue).pack()

my_string1 = tk.StringVar(window)
def info():
    print(my_string1())
entry = tk.Entry(window, textvariable=my_string1, bg='white', fg='black', width=200).pack()

window.mainloop()
3
  • Do you need it to translate from English to Russian and from Russian to English? You could have two dictionaries, one for each. Commented Sep 24, 2021 at 15:36
  • Yes, this is for translation. How do you see my code if you use your strategy? What do you need to change in the code? Thank you very much for your feedback! Commented Sep 24, 2021 at 15:52
  • to make code more readable you could put all functions directly after imports - and before window = tk.Tk(). Don't mix normal lines with functions. Commented Sep 24, 2021 at 17:57

1 Answer 1

1

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().

Sign up to request clarification or add additional context in comments.

5 Comments

quick note: you could use only one dictionary prefilled, the other could be generated in runtime: fruit_e_to_r = {english: russian for russian, english in fruit_r_to_e.items()}
Thank you very much for such a contribution to my development. Very grateful. And how do I make the translated word automatically appear in the second line of "entry" after I click 'Translate'?
I think you would use my_string1.set(). I will update my answer code to do this. Also, @Matiiss, I will use this in my code as well. Thanks for the suggestion!
I will wait for a more updated version of my code. Thank you very much for your help. Goodbye!
Excuse me, can you reset the version from .set () here so that I can start developing a version for mass use and creating a visual page for this program?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.