rassberry pi
Posts: 12
Joined: Mon Jun 29, 2020 1:22 am

update_command method for textbox

Mon Jun 29, 2020 4:33 am

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

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: update_command method for textbox

Mon Jun 29, 2020 5:51 pm

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()

rassberry pi
Posts: 12
Joined: Mon Jun 29, 2020 1:22 am

Re: update_command method for textbox

Mon Jun 29, 2020 11:33 pm

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.

User avatar
Paeryn
Posts: 2966
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: update_command method for textbox

Tue Jun 30, 2020 3:34 am

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()
She who travels light — forgot something.

rassberry pi
Posts: 12
Joined: Mon Jun 29, 2020 1:22 am

Re: update_command method for textbox

Tue Jun 30, 2020 10:21 pm

Thank you so much Paeryn, that worked for me!

Kindest regards,
rassberry pi

Return to “Python”