[英]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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.