如何通过python函数更新文本标签
Posted
技术标签:
【中文标题】如何通过python函数更新文本标签【英文标题】:How to update a text label by python function 【发布时间】:2020-03-09 18:47:15 【问题描述】:我用一个Button来调用一个函数(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()
Funktion (def write_username(self):) 在文本文件中写入用户名。 之后,Funktion 应该使用输入的用户名更新 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"
【问题讨论】:
错误信息是什么? 到目前为止它可以工作,但我没有找到在读取 kv 后更新 Label 上的文本的解决方案。 例如,我想在 python 中进行计算,并将新数据传递给以 kV 为单位的文本标签并显示在屏幕上。 【参考方案1】:据我所知,您希望从 Python 代码中更新 Label 的文本。为此,您需要使用 ObjectProperty
kv码(sn-p)
GridLayout:
cols: 1
username:username
Label:
id : username
text: 'hello'
color: 1,0,1,1
python 代码 (sn-p)
class function(self):
username = ObjectProperty(None)
if logic1:
self.username.text = 'text for logic 1'
else:
self.username.text = 'text for logic 2'
希望这能够回答您的问题 :)
【讨论】:
以上是关于如何通过python函数更新文本标签的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 Selenium 和 Python 从 html 标签跨度获取文本