在 Kivy 中更改文本大小写
Changing cases of text in Kivy
我正在编写一个程序,我想将多个标签的文本更改为大写。但是我的程序似乎只将最后一个文本更改为大写。这是我的程序。在这里,只有 c 被转换为大写。 a 和 b 保持小写。我哪里错了?
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.togglebutton import ToggleButton
from kivy.lang import Builder
Builder.load_string('''
<box>:
ToggleButton:
text: 'Caps Lock'
on_state:
if self.state == 'down': lol.text = lol.text.upper()
elif self.state == 'normal': lol.text = lol.text.lower()
Label:
id: lol
text: 'a'
Label:
id: lol
text: 'b'
Label:
id: lol
text: 'c'
''')
class box(BoxLayout):
pass
class main(App):
def build(self):
return box()
if __name__ == "__main__":
main().run()
id
属性在规则中是唯一的。你覆盖了它两次。我的建议是给每个标签一个唯一的 id
,并编写一个函数(在 box
中)将其内容设置为大写或小写。
一个带有循环的版本,而不是给每个标签一个唯一的 id
:
Builder.load_string('''
<Box>:
toggle: toggle
ToggleButton:
id: toggle
text: 'Caps Lock'
on_state: root.change_labels()
Label:
text: 'a'
Label:
text: 'b'
Label:
text: 'c'
''')
class Box(BoxLayout):
toggle = ObjectProperty()
def change_labels(self):
for child in self.children[:3]:
if self.toggle.state == 'down':
child.text = child.text.upper()
else:
child.text = child.text.lower()
我正在编写一个程序,我想将多个标签的文本更改为大写。但是我的程序似乎只将最后一个文本更改为大写。这是我的程序。在这里,只有 c 被转换为大写。 a 和 b 保持小写。我哪里错了?
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.togglebutton import ToggleButton
from kivy.lang import Builder
Builder.load_string('''
<box>:
ToggleButton:
text: 'Caps Lock'
on_state:
if self.state == 'down': lol.text = lol.text.upper()
elif self.state == 'normal': lol.text = lol.text.lower()
Label:
id: lol
text: 'a'
Label:
id: lol
text: 'b'
Label:
id: lol
text: 'c'
''')
class box(BoxLayout):
pass
class main(App):
def build(self):
return box()
if __name__ == "__main__":
main().run()
id
属性在规则中是唯一的。你覆盖了它两次。我的建议是给每个标签一个唯一的 id
,并编写一个函数(在 box
中)将其内容设置为大写或小写。
一个带有循环的版本,而不是给每个标签一个唯一的 id
:
Builder.load_string('''
<Box>:
toggle: toggle
ToggleButton:
id: toggle
text: 'Caps Lock'
on_state: root.change_labels()
Label:
text: 'a'
Label:
text: 'b'
Label:
text: 'c'
''')
class Box(BoxLayout):
toggle = ObjectProperty()
def change_labels(self):
for child in self.children[:3]:
if self.toggle.state == 'down':
child.text = child.text.upper()
else:
child.text = child.text.lower()