Home > Back-end >  How do i get the key press and write it to a variable?
How do i get the key press and write it to a variable?

Time:12-14

I am writing a program "translator". Wrote a programming interface. I want to make it so that I get what the user enters, write to a variable, translate and output.

However I don’t understand how to do it. I have looked at many forums but have not found an answer. I want the user to enter his text for translation in the left text entry window, I receive this text, write it to a variable, translate and display the translated text in the right window. I want to do this in order to automate the program, so that the translation is automatic, without buttons. `

from languages import lang
from function import *
from tkinter import *
from tkinter import ttk
import keyboard
from tkinter import messagebox
import googletrans
from googletrans import Translator

root = Tk()

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)

style = ttk.Style()
style.configure('TCombobox', pady=15 )

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("<FocusIn>", defocus)

exchange_button = PhotoImage(file='transfer.png')
img_label = Label(image=exchange_button)
exchange_button = exchange_button.subsample(18,18)
exchange_button1 = Button(root, image=exchange_button,background='#2ee59d',borderwidth=0, command=exchange_button)
exchange_button1.place(relx=0.49,y=50)

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

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

text1 = Text(first_frame, bg = "White")
text1.place(x=0,y=0,width= 250, height=200)

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 = "White")
text2.place(x=0,y=0,width= 250, height=200)

root.mainloop()

/function

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

def exchange_button():
    pass

/languages

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

` enter image description here

CodePudding user response:

You can do this:

variable = input("Enter something: ")
print(variable)

variable holds whatever user typed in.

CodePudding user response:

I want to do this in order to automate the program, so that the translation is automatic, without buttons.

Something has to trigger the translation, but it's not clear what you want that trigger to be. For this answer, I'll assume you want it to happen when the user presses the return key.

First, you need to write a function that does the translation. Since we're going to call this from an event, it needs to take an argument representing the event that triggered the function even though we don't need this argument in the function:

def do_translation(event):
    source_lang = language_selection1.get()
    target_lang = language_selection2.get()
    text = text1.get("1.0", "end").strip()

    translated_text = <your code to do the translation>

    text2.delete("1.0", "end")
    text2.insert("end", translated_text)

Next, instruct tkinter to call this function when the user presses the return key by binding the return key to the function:

text1.bind("<Return>", do_translation)

If you want the translation to happen as the user types, you can bind on the event <Any-KeyRelease> which will cause the function every time the user presses and releases a key:

text1.bind("<Any-KeyRelease>", do_translation)
  • Related