0

I want to create a button in tkinter that prints text in different Entry depends if it clicked. The text to be displayed must be the same, but not at the same time.

I have this code to try to get the result:

from tkinter import *

root = Tk()
event = StringVar()


def callback(event):
    print("focus in event raised, open keyboard")
    return event

frame = Frame(root, width=100, height=100)
frame.pack()

addressInput = Entry(frame, font = "Verdana 20 ", justify="center")
addressInput.bind("<FocusIn>", callback)
addressInput.pack()
addressInput.focus_set()

otroInput = Entry(frame, font = "Verdana 20 ", justify="center")
otroInput.bind("<FocusIn>", callback)
otroInput.pack()


def inserta(text):
    if event == ".!frame.!entry":
        addressInput.insert(200,text)
    if event == ".!frame.!entry2":
        otroInput.insert(200,text)


inserta=Button(frame, text="Insert number 2 ", command=inserta("2"))
inserta.pack()

root.mainloop()

So when I click inserta(Buttom) in addressInput(entry) you can see the number "2". If you select otroInput(Entry) and then click the Button, you see number "2" in otroInput(Entry).

If you have any suggestions, I would be very grateful, please.

Thank you very much!

JRiggles
  • 4,847
  • 1
  • 12
  • 27
Une
  • 3
  • 3
  • Just so I understand what you're asking: You want to set the text of the `Entry` widget that currently has focus when the user presses a `Button`? Is that correct? – JRiggles Mar 21 '23 at 19:13
  • Yes! That's correct – Une Mar 21 '23 at 19:16
  • I suggest you read [How to pass arguments to a Button command in Tkinter?](https://stackoverflow.com/questions/6920302/how-to-pass-arguments-to-a-button-command-in-tkinter) – Bryan Oakley Mar 21 '23 at 19:16
  • I tried to pass arguments to a Button command, but the text is only set in one Entry – Une Mar 21 '23 at 19:19
  • @Une Are you trying to set the text for both `Entry` widgets at once, or just the one that has focus? I should also point out that either `Entry` will lose focus once you click the button, so you'd have to keep track of which widget had focus just before the button was pressed. – JRiggles Mar 21 '23 at 19:22

1 Answers1

1

If you want the text to be inserted into the widget with keyboard focus, you can use focus_get() to get the widget with the focus. You can then check to see if it's an entry widget, and insert text into it if it is.

def inserta():
    focus = root.focus_get()
    if focus and focus.winfo_class() == "Entry":
        focus.insert("insert", "a")
...
inserta=Button(frame, text="Insert number 2 ", command=inserta)

If you want to be able to pass in what is to be inserted, you can use lambda to create an anonymous function:

def do_insert(char):
    focus = root.focus_get()
    if focus and focus.winfo_class() == "Entry":
        focus.insert("insert", char)
...
insert2=Button(frame, text="Insert number 2 ", command=lambda: do_insert("2")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • @Bryan Oakley. How about walrus? Would it nice like this? def inserta(): if [focus := root.focus_get()]and focus.winfo_class() == "Entry": focus.insert("insert", "a") – toyota Supra Mar 22 '23 at 01:04