Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

python 3.x - Kivy: Update a TextInput element with button

I recently started making a Kivy App.

What it should do so far is change the TextInput element from "Password will appear here" to "Cleared!" when the user presses the "Clear" button.

Unfortunately nothing happens and I am not sure why.

Code:

class Main(FloatLayout):

    def __init__(self, **kwargs):
        super(Main, self).__init__(**kwargs)
        self.cols = 2
        self.rows = 3
        self.add_widget(Label(text=str("Random Password Generator"), size_hint_y=None, pos=(20,600), color=(1, 204/255, 102/255)))
        self.checkBox1 = CheckBox(active=False)
        self.add_widget(self.checkBox1)
        self.dropdown = DropDown()
        for index in range(50):
            btn = Button(text='%d' % index, pos_hint=(200,200), size_hint_y=None, height=44)
            btn.bind(on_release=lambda btn: self.dropdown.select(btn.text))
            self.dropdown.add_widget(btn)
        self.mainbutton = Button(text='Char Count', size_hint=(0.08, 0.05), pos=(427,400))
        self.mainbutton.bind(on_release=self.dropdown.open)
        self.add_widget(self.mainbutton)
        self.dropdown.bind(on_select=lambda instance, x: setattr(self.mainbutton, 'text', "Char Count: " + x))
        self.textBox = TextInput(text="Password will appear here", size_hint=(0.4, 0.05), pos=(427, 520))
        self.add_widget(self.textBox)
        self.clearButton = Button(text="Clear", size_hint=(0.08,0.05), pos=(427,440))
        self.clearButton.bind(on_select=self.clearBox)
        self.add_widget(self.clearButton)
    def clearBox(self):
        self.textBox.text = "Cleared!"
class KeycardApp(App):

    def build(self):
        return Main()

if __name__ == '__main__':
    KeycardApp().run()

Feel free to laugh if I am being stupid...

question from:https://stackoverflow.com/questions/65891582/kivy-update-a-textinput-element-with-button

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Fixed by doing this:

self.clearButton.bind(on_release=lambda a: self.clearBox())

Changing to on_release gave me the error code "takes 0 positional arguments, but 1 was given" so I added lambda a: to reroute the argument to nothing... and it worked!!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...