【问题标题】:How to change text of a label in the kivy language with python如何使用python更改kivy语言中标签的文本
【发布时间】:2014-12-26 16:20:44
【问题描述】:

我想知道如何使用 Python 更改在 Kivy 语言中制作的标签文本。 就像我如何将来自 python 的用户输入作为 kivy 中的标签文本一样。 (顺便说一句,我在实际程序中的格式正确,但我把它粘贴到stackoverflow上搞砸了) 假设我想让代码中的标签文本成为python中生成的随机数,我该怎么做?

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.graphics import Color, Rectangle
from kivy.properties import ObjectProperty

# Create both screens. Please note the root.manager.current: this is how
# you can control the ScreenManager from kv. Each screen has by default a
# property manager that gives you the instance of the ScreenManager used.


Builder.load_string("""
<ButImage@ButtonBehavior+AsyncImage>
<TutImage@ButtonBehavior+AsyncImage>
<MenuScreen>:
GridLayout:
    cols: 4
    row_force_default: True
    col_default_width: 175
    row_default_height: 150
    padding: 15
    spacing: 15
    canvas.before:
        BorderImage:
            # BorderImage behaves like the CSS BorderImage
            border: 10, 10, 10, 10
            source: '/Users/clayhigh/Desktop/kivy/aot.png'
            pos: self.pos
            size: self.size
    Button:
        text: 'Goto settings'
        background_color: 1,0,0,0.5
        on_press: root.manager.current = 'settings'
    ButImage:
        on_press: root.manager.current = 'UBW'
        id: but
        size_hint: .5, .5
        opacity: 1 if self.state == 'normal' else .5
        allow_stretch: True
        keep_ratio: False
        source: 'http://s3.amazonaws.com/rapgenius/1361742626_beautiful-ocean-beautiful-pictures-27115524-1440-900.jpg'
        Label:
            center: but.center
            text: "UBW"
            color: 0.78,0.145,0.016,2
    ButImage:
        id: lh
        size_hint: .5, .5
        opacity: 1 if self.state == 'normal' else .5
        allow_stretch: True
        keep_ratio: False
        source: 'http://s3.amazonaws.com/rapgenius/1361742626_beautiful-ocean-beautiful-pictures-27115524-1440-900.jpg'
        Label:
            center: lh.center
            text: "LH 2"
            color: 0,0,0,1
    ButImage:
        id: ttl
        size_hint: .5, .5
        opacity: 1 if self.state == 'normal' else .5
        allow_stretch: True
        keep_ratio: False
        source: 'http://s3.amazonaws.com/rapgenius/1361742626_beautiful-ocean-beautiful-pictures-27115524-1440-900.jpg'
        Label:
            center: ttl.center
            text: "TwTl"
            color: 0,0,0,1
    ButImage:
        id: gris
        size_hint: .5, .5
        opacity: 1 if self.state == 'normal' else .5
        allow_stretch: True
        keep_ratio: False
        source: 'http://s3.amazonaws.com/rapgenius/1361742626_beautiful-ocean-beautiful-pictures-27115524-1440-900.jpg'
        Label:
            center: gris.center
            text: "Gris"
            color: 0,0,0,1
    ButImage:
        id: shig
        size_hint: .5, .5
        opacity: 1 if self.state == 'normal' else .5
        allow_stretch: True
        keep_ratio: False
        source: 'http://s3.amazonaws.com/rapgenius/1361742626_beautiful-ocean-beautiful-pictures-27115524-1440-900.jpg'
        Label:
            center: shig.center
            text: "Shig"
            color: 0,0,0,1
    Button:
        text: 'Test3'
        background_color: 1,0,0,0.5
    Button:
        text: 'Test4'
        background_color: 1,0,0,0.5
    Button:
        text: 'Quit'
        background_color: 1,0,0,0.5
        on_press: App.on_stop

<SettingsScreen>:
GridLayout:
    row_force_default: True
    row_default_height: 100
    cols: 2
    canvas.before:
        BorderImage:
            # BorderImage behaves like the CSS BorderImage
            border: 10, 10, 10, 10
            source: '/Users/clayhigh/Desktop/kivy/ato.jpeg'
            pos: self.pos
            size: self.size
    Button:
        text: 'Button'
        color: 0,0,.5
        background_color: 1,0,0,1
    Button:
        text: 'Back to menu'
        background_color: 1,0,0,1
        on_press: root.manager.current = 'menu'
<UBW>:
GridLayout:
    row_force_default: True
    row_default_height: 100
    cols: 2
    canvas.before:
        Color:
            rgb: .5, .5, .5
        Rectangle:
            pos: self.pos
            size: self.size
        Color:
            rgb: 1, 1, 1
        BorderImage:
            # BorderImage behaves like the CSS BorderImage
            border: 10, 10, 10, 10
            source: '/Users/clayhigh/Desktop/kivy/fsn.jpg'
            pos: self.pos
            size: self.size
    Button:
        text: 'Back to menu'
        color: 0,0,.5
        on_press: root.manager.current = 'menu'
        background_color: 1,0,0,1
    Label:
        id: AName
        text: "F S/N: UBW"
        font_size: '24sp'
""")

# Declare both screens


class MenuScreen(Screen):
pass


class SettingsScreen(Screen):
pass


class UBW(Screen):
pass

# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))
sm.add_widget(UBW(name='UBW'))


class TestApp(App):

def build(self):
    return sm

if __name__ == '__main__':
TestApp().run()

【问题讨论】:

    标签: python python-3.x kivy


    【解决方案1】:

    标签的文本可以是 kivy 属性,以后可以更改,因为它是 kivy 属性,所以它会在任何地方自动更新。这是您的 .py 示例

    from kivy.app import App
    from kivy.uix.widget import Widget
    from kivy.properties import StringProperty
    import random
    
    class YourWidget(Widget):
        random_number = StringProperty()
    
        def __init__(self, **kwargs):
            super(YourWidget, self).__init__(**kwargs)
            self.random_number = str(random.randint(1, 100))
    
        def change_text(self):
            self.random_number = str(random.randint(1, 100))
    
    class YourApp(App):
        def build(self):
            return YourWidget()
    
    if __name__ == '__main__':
        YourApp().run()
    

    还有你的 .kv

    <YourWidget>:
        BoxLayout:
            size: root.size
            Button:
                id: button1
                text: "Change text"
                on_release: root.change_text()
            Label:
                id: label1
                text: root.random_number
    

    当你点击按钮时,它会调用change_text()函数,它将标签的文本随机更改为1到100之间的随机整数。

    【讨论】:

    • 感谢您的回复,但是否可以在 kivy 语言之外调用该函数?假设我希望将标签的文本更改为列表中的字符串,我可以这样做而无需按下按钮调用该函数吗?谢谢
    • 是的,您可以像往常一样在YourClassself.change_text() 中的任何位置。您不必调用任何函数,您唯一需要做的就是将一个新字符串分配给(在这种情况下)random_number
    • 几年后,我确实用你的回答解决了我的问题,谢谢你们俩
    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    相关资源
    最近更新 更多