如何使用python更改kivy语言中标签的文本

如何使用python更改kivy语言中标签的文本,python,python-3.x,kivy,Python,Python 3.x,Kivy,我想知道如何使用Python更改Kivy语言中的标签文本。 比如,我如何让python中的用户输入成为kivy中标签的文本。 (顺便说一句,我在实际程序中的格式是正确的,但我把它粘贴到stackoverflow时搞砸了) 比如说,如果我想让代码中的标签文本成为python中生成的随机数,我该怎么做呢 from kivy.app import App from kivy.lang import Builder from kivy.uix.screenmanager import ScreenMan

我想知道如何使用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()
从kivy.app导入应用
从kivy.lang导入生成器
从kivy.uix.screenmanager导入screenmanager,屏幕
从kivy.graphics导入颜色,矩形
从kivy.properties导入ObjectProperty
#创建两个屏幕。请注意root.manager.current:这就是
#您可以从kv控制ScreenManager。默认情况下,每个屏幕都有一个
#属性管理器,为您提供所用ScreenManager的实例。
生成器。加载\u字符串(“”)
:
网格布局:
科尔斯:4
行\强制\默认值:True
颜色默认宽度:175
行\默认\高度:150
填充:15
间距:15
在以下情况之前:
边框图像:
#BorderImage的行为类似于CSS BorderImage
边界:10,10,10,10
来源:“/Users/clayhigh/Desktop/kivy/aot.png”
pos:self.pos
大小:self.size
按钮:
文本:“转到设置”
背景颜色:1,0,0,0.5
按:root.manager.current='settings'
但是图像:
按:root.manager.current='UBW'
id:但是
大小提示:.5,.5
不透明度:如果self.state=='normal'else,则为1。5
允许拉伸:真
保留比率:False
资料来源:'http://s3.amazonaws.com/rapgenius/1361742626_beautiful-ocean-beautiful-pictures-27115524-1440-900.jpg'
标签:
中锋:但是,中锋
案文:“UBW”
颜色:0.78,0.145,0.016,2
但是图像:
id:lh
大小提示:.5,.5
不透明度:如果self.state=='normal'else,则为1。5
允许拉伸:真
保留比率:False
资料来源:'http://s3.amazonaws.com/rapgenius/1361742626_beautiful-ocean-beautiful-pictures-27115524-1440-900.jpg'
标签:
中:左中
正文:“LH 2”
颜色:0,0,0,1
但是图像:
id:ttl
大小提示:.5,.5
不透明度:如果self.state=='normal'else,则为1。5
允许拉伸:真
保留比率:False
资料来源:'http://s3.amazonaws.com/rapgenius/1361742626_beautiful-ocean-beautiful-pictures-27115524-1440-900.jpg'
标签:
中心:ttl.center
案文:“TwTl”
颜色:0,0,0,1
但是图像:
身份证号码:格里斯
大小提示:.5,.5
不透明度:如果self.state=='normal'else,则为1。5
允许拉伸:真
保留比率:False
资料来源:'http://s3.amazonaws.com/rapgenius/1361742626_beautiful-ocean-beautiful-pictures-27115524-1440-900.jpg'
标签:
中心:格里斯中心
文本:“Gris”
颜色:0,0,0,1
但是图像:
id:shig
大小提示:.5,.5
不透明度:如果self.state=='normal'else,则为1。5
允许拉伸:真
保留比率:False
资料来源:'http://s3.amazonaws.com/rapgenius/1361742626_beautiful-ocean-beautiful-pictures-27115524-1440-900.jpg'
标签:
中心:志贺中心
正文:“志”
颜色:0,0,0,1
按钮:
文本:“Test3”
背景颜色:1,0,0,0.5
按钮:
文本:“Test4”
背景颜色:1,0,0,0.5
按钮:
文本:“退出”
背景颜色:1,0,0,0.5
on_按:应用程序on_停止
:
网格布局:
行\强制\默认值:True
行\默认\高度:100
科尔斯:2
在以下情况之前:
边框图像:
#BorderImage的行为类似于CSS BorderImage
边界:10,10,10,10
来源:“/Users/clayhigh/Desktop/kivy/ato.jpeg”
pos:self.pos
大小:self.size
按钮:
文本:“按钮”
颜色:0,0,5
背景颜色:1,0,0,1
按钮:
文本:“返回菜单”
背景颜色:1,0,0,1
按:root.manager.current='菜单'
:
网格布局:
行\强制\默认值:True
行\默认\高度:100
科尔斯:2
在以下情况之前:
颜色:
rgb:.5、.5、.5
矩形:
pos:self.pos
大小:self.size
颜色:
rgb:1,1,1
边框图像:
#BorderImage的行为类似于CSS BorderImage
边界:10,10,10,10
来源:“/Users/clayhigh/Desktop/kivy/fsn.jpg”
pos:self.pos
大小:self.size
按钮:
文本:“返回菜单”
颜色:0,0,5
按:root.manager.current='菜单'
背景颜色:1,0,0,1
标签:
id:AName
正文:“F序列号:UBW”
字体大小:“24sp”
""")
#声明两个屏幕
类菜单屏幕(屏幕):
通过
类别设置屏幕(屏幕):
通过
UBW类(屏幕):
通过
#创建屏幕管理器
sm=屏幕管理器()
sm.add_小部件(菜单浏览(name='menu'))
sm.add_小部件(设置屏幕(name='settings'))
sm.add_小部件(UBW(name='UBW'))
类TestApp(应用程序):
def生成(自):
返回sm
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
TestApp().run()

标签的文本可以是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()
还有你的

<YourWidget>:
    BoxLayout:
        size: root.size
        Button:
            id: button1
            text: "Change text"
            on_release: root.change_text()
        Label:
            id: label1
            text: root.random_number
:
盒子布局:
大小:root.size
按钮:
id:按钮1
文本:“更改文本”
发布时:root.change\u text()
标签:
id:label1
文本:root.random\u编号

当您单击该按钮时,它将调用
change\u text()
函数,该函数将随机将标签文本更改为1到100之间的随机整数。

感谢您的响应,不过可以在外部调用该函数吗