'Launch by button from the keyboard

I need the text to be entered using a widget Entry and launched on the enter button.

        entry = tk.Text(root, width=30, height=1)
        entry.bind("<Return>")
        entry.pack()
        entry.focus()

Here is an example, but I don’t know how to insert it into the program.

import tkinter as tk

from tkinter import *

root = tk.Tk()`

c = tk.Canvas(root)
c.pack(expand=1, fill=tk.BOTH)

words = 'London is capital of Great Britain.'
words = words.split()

def new_word(i):
  if i == len(words):
    i = 0

  word = words[i]
  middle = (len(word)+1)//2
  c.itemconfigure(t1, text=word[:middle-1]+' ')
  c.itemconfigure(t2, text=word[middle-1:middle])
  c.itemconfigure(t3, text=word[middle:])

  root.after(100, lambda: new_word(i+1))


t1 = c.create_text(200,100,text='', anchor='e', font=("Courier", 25))
t2 = c.create_text(200,100,text='', anchor='e', font=("Courier", 25), fill='red')
t3 = c.create_text(200,100,text='', anchor='w', font=("Courier", 25))
new_word(0)

root.geometry('400x200+200+200')
root.mainloop()

Thanks in advance.



Solution 1:[1]

Create an Entry widget and bind it with the "Return" key, calling a function that will store the text from the Entry box as a list of words in a list, and this list will be then used by the new_word() function.

import tkinter as tk

root = tk.Tk()

c = tk.Canvas(root)
c.pack(expand=1, fill=tk.BOTH)

entryWords = tk.StringVar()             # a StringVar() for string type variable of tk
words= []                               # will store the list of words entered
def storeWords(event):                  # a function to call when "Enter" key is pressed
  global words                          # the variable will be used by other functions too, so global
  words = entryWords.get().split()      #Get the text from Entry, and create the list
  new_word(0)                           # call the function here instead of calling below
  
entry = tk.Entry(root, textvariable=entryWords, width=30)       #set the StringVar as its textvariable 
entry.bind("<Return>", storeWords)      #bind the "Enter" key with the Entry box and call the function "storeWords" when it is pressed
c.create_window(200, 50, window=entry)  #place this Entry widget in the canvas
entry.focus()                           
  
def new_word(i):
  if i == len(words):
    i = 0
    
  word = words[i]
  middle = (len(word)+1)//2
  c.itemconfigure(t1, text=word[:middle-1]+' ')
  c.itemconfigure(t2, text=word[middle-1:middle])
  c.itemconfigure(t3, text=word[middle:])

  root.after(100, lambda: new_word(i+1))

t1 = c.create_text(200,100,text='', anchor='e', font=("Courier", 25))
t2 = c.create_text(200,100,text='', anchor='e', font=("Courier", 25), fill='red')
t3 = c.create_text(200,100,text='', anchor='w', font=("Courier", 25))

root.geometry('400x200+200+200')
root.mainloop()

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Kartikeya