Page 1 of 1

update_command method for textbox

Posted: Mon Jun 29, 2020 4:33 am
by rassberry pi
Hi all,

I am using the textbox widget under guizero. When the user changes their value in the textbox, it will trigger the update command, but I do not know how to use it.

Can someone please give me a sample?

Kind regards,
rassberry pi

Re: update_command method for textbox

Posted: Mon Jun 29, 2020 5:51 pm
by ghp

Code: Select all

import guizero

# both methods are possible (without or with the key)
#
# def update_callback():
#    print("update_callback")
#
def update_callback( key ):
    print("update_callback", key )

app=guizero.App(title="callback", width=400, height=100)
textbox = guizero.TextBox(app, text="unknown",  command=update_callback)


app.display()

Re: update_command method for textbox

Posted: Mon Jun 29, 2020 11:33 pm
by rassberry pi
Thank you for your help ghp, I really appreciate it.

The code doesn't work for me. It prints out update_callback, but does not print out the values after I write something in the text box.

I am using latest Raspbian version (Buster) on Raspberry Pi.

Re: update_command method for textbox

Posted: Tue Jun 30, 2020 3:34 am
by Paeryn
The callback just tells you what key was added so as you type the callback function is called with each letter.

If you want to know the contents of the textbox at any time just read its value. I added a PushButton that when pressed will print the contents of the textbox to the console. The lambda expression is so I can pass textbox as an argument to the print_text() function rather than relying on textbox being a global variable.

Code: Select all

import guizero

def update_callback( key ):
    print("update_callback", key )

def print_text( text_box ):
    print('Current text: ', text_box.value)

app=guizero.App(title="callback", width=400, height=100)
textbox = guizero.TextBox(app, text="unknown",  command=update_callback)
button = guizero.PushButton(app, text = 'Print contents', command = lambda t = textbox : print_text(t))

app.display()

Re: update_command method for textbox

Posted: Tue Jun 30, 2020 10:21 pm
by rassberry pi
Thank you so much Paeryn, that worked for me!

Kindest regards,
rassberry pi