1

I have a tkinter GUI which includes an Entry Widget (tk.Entry). At the moment this displays a numeric value, which can be incremented up/down by a couple of tkinter Buttons. In addition to incrementing the value displayed in the Entry Widget, whenever the Button is clicked it executes a command which updates the appropriate setting on a physical instrument with the new value from the Entry Widget.

I am wondering if it is possible at all to also have the option that if the user types a number into the Entry Widget to overwrite what is already there, that this can execute the same command as clicking the up/down buttons? For example, if the value is set to 10, and the user enters 100, then the command is executed to update the real instrument to 100.

I tried to add the code command=mycommandname to the Entry Widget (e.g. input_wavelength = tk.Entry(pwr_mtr_ctrl_frame, relief=tk.GROOVE, font=( "Ariel", 11), justify='center', validate='key', vcmd=pwr_vcmd, command=mycommandname) but get the error "unknown option "-command""

I guess this means that the Entry Widget does not have the option to execute a function, like a Button widget does? Are there ways to implement this somehow?

McKendrigo
  • 39
  • 1
  • 5
  • Hmmmm include the code so we can understand whats going on. Basically use `IntVar()` and `trace()` method or you can use binding with the widget – Delrius Euphoria Oct 14 '20 at 14:33
  • If the value is ten, and the user deletes the zero, and one, and then enters 2, do you want the command to be called three times or just once? If just once, do you want it to be triggered when they press return, or when they click on some other item in the UI/ – Bryan Oakley Oct 14 '20 at 14:35

2 Answers2

2
def print_something(*args):
    print('hello world')

root = TK()
entry1 = Entry(root)
entry1.bind("<Return>", print_something) 
# you can use other keys and replace it with "<Return>". EX: "f"
# by default, this function will pass an unknown argument to your function.
# thus, you have to give your function a parameter; in this case, we will use *args

root.mainloop()
Heang Sok
  • 144
  • 5
0

According to what you described you would be better "served" by using a Spinbox.

This being said you need to bind a function to the <Return> event captured by the Entry if you want to execute something when the user enters something:

Here is how to do it:

input_wavelength = tk.Entry(pwr_mtr_ctrl_frame, relief=tk.GROOVE, font=( "Ariel", 11), justify='center', validate='key', vcmd=pwr_vcmd)
input_wavelength.bind("<Return>",mycommandname)

If mycommandname is also used as a command you need to declare it like this:

def mycommandname(event=None):
    ...
Jean-Marc Volle
  • 3,113
  • 1
  • 16
  • 20