简体   繁体   中英

Changing cases of text in Kivy

I'm writing a program where I want to change the text of multiple labels to uppercase. But my program seems to change only the last text to uppercase. This is my program. Here, only c is getting converted to uppercase. a and b remain in lowercase. Where am I going wrong?

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

The id properties are unique within a rule. You overrided it two times. What I suggest is giving each label an unique id , and writing a function (in box ) that sets their content to either uppercase or lowercase.


A version with a loop, instead of giving each label a unique 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM