簡體   English   中英

我如何將變量從.py文件轉換為.kv文件Text_Input小部件

[英]How do i get a variable from .py file into .kv file Text_Input widget

我試圖在我的.kv文件中有一個按鈕,用當前時間更新.kv文件中的text_input小部件。

我將當前時間存儲在.py文件中的函數變量中。

def get_time(self, event):
    dt3 = str(datetime.datetime.now().strftime("%H-%M"))
    return dt3

.kv文件的一部分:

CustButton:
    text: "Click for current Time"
    on_press: time3=root.get_time('dt3')

CustTextInput:
    id: time3
    hint_text: 
    font_size: 25

我沒有收到任何錯誤,但是當我單擊時什么也沒有發生。 我不確定我是否需要將'dt3'存儲在對象屬性中以傳遞給.kv文件。

任何幫助,將不勝感激。 謝謝。

我找到了解決方案。 我在按鈕on_press命令中缺少對hint_text字段的引用。 參見下面的工作代碼:

CustButton:
    text: "Click for current Time"
    on_press: time3.hint_text=root.get_time('dt3')

CustTextInput:
    id: time3
    hint_text: ""
    font_size: 25

您可能要使用time3.text = root.get_time('dt3')而不是hint_text並具有readonly: True CustTextInput readonly: True 另外最好使用on_release而不是on_press

CustButton:
    text: "Click for current Time"
    on_release: time3.text=root.get_time('dt3')

CustTextInput:
    id: time3
    text: ""
    readonly: True
    font_size: 25

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM