我如何获得按键并将其写入变量?

How do i get the key press and write it to a variable?

我正在写一个程序“翻译器”。写了一个编程接口。我想做的是让我得到用户输入的内容,写入变量,翻译和输出。

但是我不知道该怎么做。我看了很多论坛,但没有找到答案。 我希望用户在左侧文本条目 window 中输入他的文本进行翻译,我收到此文本,将其写入变量,翻译并在右侧 window 中显示翻译后的文本。我想这样做是为了使程序自动化,以便翻译是自动的,没有按钮。 `

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

/函数

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

def exchange_button():
    pass

/语言

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

`

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

某些东西必须触发翻译,但不清楚您希望该触发器是什么。对于这个答案,我假设您希望它在用户按下 return 键时发生。

首先,您需要编写一个函数来进行翻译。因为我们要从一个事件中调用它,它需要一个参数来表示触发该函数的事件,即使我们在函数中不需要这个参数:

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)

接下来,通过将 return 键绑定到函数,指示 tkinter 在用户按下 return 键时调用此函数:

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

如果您希望在用户键入时进行翻译,您可以绑定事件 <Any-KeyRelease>,这将在用户每次按下和释放键时触发该功能:

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