Reputation: 444
I'm new to Kivy. I'm working on this code and I'm getting confused about what the bind function does.
Basically, the code below generates a text input and prints out the user's input.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
class LoginScreen(Widget):
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.username = TextInput(size = (300, 30), pos = (300, 30), multiline = False)
# self.username.bind(on_text_validate = self.on_enter) ### first line
self.username.bind(text= self.on_text) ### second line
self.add_widget(self.username)
def on_enter(instance, value, secondvalue):
print(secondvalue)
def on_text(instance, value, secondvalue):
print(secondvalue)
class ABCApp(App):
def build(self):
return LoginScreen()
if __name__ == "__main__":
ABCApp().run()
Here's what I'm confused about. Why is it that only by printing out the secondvalue
will I get the user's actual input? What is the bind
function doing here? I looked at the documentation but couldn't find anything.
Also if I switch the commenting about such that the first line is commented out and the second line commented in, such that
self.username.bind(on_text_validate = self.on_enter) ### first line
# self.username.bind(text= self.on_text) ### second line
I am now referencing the function on_enter
upon entering my text and pressing down the enter button. However, then I get the error message:
TypeError: on_enter() missing 1 required positional argument: 'secondvalue'
If I change the function on_enter
to accept 2 arguments,
def on_enter(instance, secondvalue):
print(secondvalue)
This now prints <kivy.uix.textinput.TextInput object at 0x0000000003A432B8>
, but doesn't recover the text.
I'm confused about what Kivy is doing at their backend and I can't find any answers in their documentations. Why is it that on_enter
accepts 2 arguments while on_text
3?
Upvotes: 0
Views: 1369
Reputation: 2231
Bind connects an event with a function.
In your case, the first event is the on_text_validate
of the TextInput
widget (the event that is emitted when you press Enter while on its text field), and the second is text
(when the text of the field is changed).
These events trigger their dedicated functions using different arguments.
Both of them send as first argument the widget that produce them (the TextInput
instance).
The text
also sends the changed text.
To get the text of the on_text_validate
event, you can get the TextInput
text
property like this:
print(instance.text)
Upvotes: 2