如何通过python函数更新文本标签

时间:2019-11-13 21:47:06

标签: python python-3.x kivy kivy-language

我使用按钮来调用函数(root.write_username())

  GridLayout
        cols: 1

        Label:
            text: "Login"

        TextInput:
            id: username
            hint_text: root.usernamehint
            multiline: False

        Button:
            text: "Gooo"
            on_release: root.write_username()

功能块(def write_username(self):)在文本文件中写入用户名。 之后,功能部件应使用输入的用户名更新SecondWindow中的标签。

class LoginScreen(Screen):
    username = ObjectProperty(None)
    usernamehint = StringProperty(str(read_username()))


#####Weiterleitung SecondScreen, wenn ein Benutzername eingegeben wurde wird dieser gespeichert#####

    def write_username(self):
        if str(self.username.text) is "":
            self.manager.current = "second"
            pass
        else:
            f = open("username.text", "w")
            f.write(str(self.username.text))
            f.close()
          #######befor go to second the label in second should be updated but how?#####
            self.manager.current = "second"
<SecondWindow>:
    name: "second"
    username_label: usernamelabel

    GridLayout:
        cols: 1

        ####this label should be updated####
        Label:
            id : usernamelabel
            text: root.username
            color: 1,0,1,1

我知道有一些关于使用python更新kivy标签的帖子,但是我没有得到答案。 感谢您的帮助!

如果您需要我的完整代码

##########################Pyton#############################
import random
import operator
import time
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty

kivy.require("1.11.1")


def read_username():
    f = open("username.text", "r")
    username = str(f.read())
    f.close()
    return username

    pass


def choicehello():
    spruch = ["Gib jedem Tag die Chance, der schönste deines Lebens zu werden. – Mark Twain",
              "Wenn du die Absicht hast, dich zu erneuern, tu es jeden Tag. – Konfuzius",
              ]
    hello = random.choice(spruch)

    return hello

    pass


#####Eingabe Username falls Unbekannt########

class LoginScreen(Screen):
    username = ObjectProperty(None)
    usernamehint = StringProperty(str(read_username()))


#####Weiterleitung SecondScreen, wenn ein Benutzername eingegeben wurde wird dieser gespeichert#####

    def write_username(self):
        if str(self.username.text) is "":
            self.manager.current = "second"
            pass
        else:
            f = open("username.text", "w")
            f.write(str(self.username.text))
            f.close()

            self.manager.current = "second"




####Begrüßung des Users, Level Auswahl, Einstellungen, Highscores#####

class SecondWindow(Screen):
    username = StringProperty(str(read_username()))
    userhello = StringProperty(str(choicehello()))

class Game1Window(Screen):
    pass


class WindowManager(ScreenManager):
    pass


kv = Builder.load_file("mathe.kv")


class MatheApp(App):
    def build(self):
        return kv


if __name__ == "__main__":
    MatheApp().run()


########################KV########################################

WindowManager:
    LoginScreen:
    SecondWindow:
    Game1Window:

<LoginScreen>:
    name: "login"
    username: username


    GridLayout
        cols: 1

        Label:
            text: "Login"

        TextInput:
            id: username
            hint_text: root.usernamehint
            multiline: False

        Button:
            text: "Gooo"
            on_release: root.write_username()





<SecondWindow>:
    name: "second"


    GridLayout:
        cols: 1

        Label:
            text: root.userhello
            color: 1,0,1,1

        Label:
            text: "Wähle ein Level"

        Label:
            id : usernamelabel
            text: root.username
            color: 1,0,1,1

        ScrollView:

            GridLayout:
                cols: 1
                padding: 10
                spacing: 10



                Button:
                    text: "Test"
                    on_release:
                        app.root.current = "game1"

                Button:
                    text: "Test"

                Button:
                    text: "Test"

        Button:
            text: "Go Back"
            on_release:
                app.root.current = "login"



<Game1Window>:
    name: "game1"

    GridLayout:
        cols: 1

        Button:
            text: "back"
            on_release:
                app.root.current = "second"


1 个答案:

答案 0 :(得分:0)

据我所知,您正在寻找从Python代码更新Label文本的方法。为此,您需要使用ObjectProperty

kv代码(摘要)

GridLayout:
    cols: 1
    username:username
    Label:
        id : username
        text: 'hello'
        color: 1,0,1,1

python代码(摘要)

class function(self):
    username = ObjectProperty(None)
    if logic1:
        self.username.text = 'text for logic 1'
    else:
        self.username.text = 'text for logic 2'

希望这能够回答您的查询 :)