我被我的代码卡住了,你能帮忙吗?在我将 on_press 添加到我的 kv 文件并描述函数 update_label 后,出现错误:
AttributeError: 'Button' object has no attribute 'update_label'.
即使我用简单的 print() 函数更改了我的函数的描述,它也不起作用。提前致谢!
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.stacklayout import StackLayout
from kivy.uix.textinput import TextInput
Builder.load_file('spain2.kv')
class Box2(BoxLayout):
pass
class Box1(StackLayout):
pass
class Ex42App(App):
def update_label(self):
if self.l1.text == 'amar':
self.l2.text = "Translate: to love"
else:
self.l2.text = "Infinitive not founded"
def build(self):
return Box2()
if __name__=='__main__':
Ex42App().run()
西班牙2.kv:
<Box2>:
orientation: 'vertical'
Box1:
orientation: 'lr-tb'
size_hint: 1, .1
Label:
id: l1
text: 'Insert Infinitive'
size_hint: None, None
size: 150, 30
TextInput:
size_hint: None, None
size: 300, 30
Button:
text: 'Find'
size_hint: None, None
size: 150, 30
on_press: self.update_label
Label:
id: l2
text: 'some data'
size_hint: None, .9
相关分类