Search code examples
pythontkintertkinter-entry

Entrybox input copied into another entrybox and still be able to edit input independently


Im trying to gather the input from one entrybox and then putting it into another entrybox. I can do this using a Stringvar but i want to be able to edit the second entry without changing the other.

from tkinter import *

my_window = Tk()

var_ip = StringVar()
var_mask = StringVar()
var_gateway = StringVar()


def user_input_ip():
    input_ip = entry_ip.get()
    display_ip = input_ip
    var_ip.set(display_ip)


entry_ip = Entry(my_window, font="Helvetica 15", textvariable=var_ip)
entry_ip.place(x=35, y=100, width=158, height=30)
entry_ip.bind("<Tab>", focus_next_widget)
entry_ip.bind("<Return>", enter_key)


def user_input_mask():
    input_mask = entry_mask.get()
    display_mask = input_mask
    var_mask.set(display_mask)


entry_mask = Entry(my_window, font="Helvetica 15", textvariable=var_mask)
entry_mask.place(x=35, y=165, width=158, height=30)
entry_mask.bind("<Tab>", focus_next_widget)
entry_mask.insert(END, "255.255.255.255")


def user_input_gateway():
    input_gateway = entry_gateway.get()
    display_gateway = input_gateway
    var_gateway.set(display_gateway)


entry_gateway = Entry(my_window, font="Helvetica 15", textvariable=var_gateway)
entry_gateway.place(x=35, y=230, width=158, height=30)
entry_gateway.bind("<Tab>", focus_next_widget)

my_window.mainloop()

Solution

  • You can bind <KeyRelease> event on the first entry and update second entry in the callback:

    def on_entry_input(event):
        text = event.widget.get()
        # direct update second_entry
        second_entry.delete(0, 'end')
        second_entry.insert('end', text)
        # or via separate variable associated to second entry
        # second_entry_var.set(text)
    
    
    first_entry = Entry(...)
    first_entry.bind('<KeyRelease>', on_entry_input)
    
    second_entry = Entry(...)