Kivy:使用按钮更新 TextInput 元素

Kivy: Update a TextInput element with button

我最近开始制作 Kivy 应用程序。

到目前为止它应该做的是将 TextInput 元素从“密码将出现在这里”更改为“已清除!”当用户按下“清除”按钮时。

不幸的是什么也没发生,我不知道为什么。

代码:

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

如果我说的很蠢,请大家见笑...

通过这样做修复:

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

更改为 on_release 给了我错误代码“采用 0 个位置参数,但给出了 1 个”,所以我添加了 lambda a: 以将参数重新路由为空...它起作用了!!!