![](/img/trans.png)
[英]How do I have a label display a string with a text input variable using python in Kivy without the kv. file?
[英]How do I set the text value of a label to a global variable in my .kv file?
我的程序的一部分涉及用戶在其中輸入自己的文本。 我希望標簽更改為輸入的值,該值將存儲在全局變量中。
基本上發生了什么
chosen = StringProperty()
<MainScreen>:
BoxLayout:
id:layout
orientation: "vertical"
Label:
id: lab
text: #What do I put here?
font_size: 25
padding: 10, 0
text_size: root.width, None
size: self.texture_size
size_hint_y:3
我希望文本隨着變量的變化而變化,並且我想控制2個不同類的變量值。
您可以使用app關鍵字來訪問python腳本中的變量
text: app.chosen
如果您接受.py
文件中的輸入,則可以使用ObjectProperty
.kv文件
<MainScreen>:
BoxLayout:
id:layout
lab:lab
orientation: "vertical"
Label:
id: lab
text: 'anything'
font_size: 25
padding: 10, 0
text_size: root.width, None
size: self.texture_size
size_hint_y:3
在.py文件中
lab = ObjectProperty()
txt = input('Dear User, please enter a value : ')
self.lab.text=txt
此代碼需要進入主mainscreen
的事件處理程序功能。 由於您尚未發布完整的代碼,因此無法向您顯示。
希望這會有所幫助。
干杯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.