2017-07-19 84 views
0

我正尝试在.kv文件中使用按钮更新.kv文件中的text_input小部件与当前时间。如何从.py文件将变量转换为.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”存储在Object Property中传递给.kv文件。

任何帮助,将不胜感激。谢谢。

回答

0

我找到了解决方案。我错过了按钮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 
0

你可能想使用time3.text = root.get_time('dt3')代替hint_textCustTextInputreadonly: 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 
+0

谢谢你的提示,其实我彻底地改变了它,而不是文字上hint_text了。注意on_release,但你能解释一下为什么使用它而不是on_press更好?干杯 – Aiden

+0

这只是按钮的行为,发布时(几乎所有其他)的命令,所以它会感到正常的使用你的kivy应用程序:-) –

+0

足够公平,谢谢你的建议。 – Aiden

相关问题