[英]Kivy: Update a TextInput element with button
I recently started making a Kivy App.我最近开始制作 Kivy 应用程序。
What it should do so far is change the TextInput element from "Password will appear here" to "Cleared."到目前为止,它应该做的是将 TextInput 元素从“密码将出现在此处”更改为“已清除”。 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...如果我是愚蠢的,请随意笑...
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!!!更改为 on_release 给了我错误代码“需要 0 个位置 arguments,但给出了 1”所以我添加了 lambda a: 将参数重新路由为空......它有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.