【问题标题】:Trying to make Kivy button pop-up a text box试图让 Kivy 按钮弹出一个文本框
【发布时间】:2018-10-04 17:24:50
【问题描述】:

没有太大帮助的类似/相关链接:

  1. How to get a text input box to display with Kivy?
  2. https://kivy.org/doc/stable/api-kivy.uix.textinput.html
  3. Getting started with Kivy: getting user input using Kivy

我已经为此工作了几个小时,我发现了与我类似的问题,但实际上没有任何效果。 这就是我想要做的事情:创建一个按钮,按下时会拉出一个文本输入框,然后在短字符串后显示您在按钮上键入的任何内容。 例如。 按钮开始像:“LP:” 您输入文本:“4000” 按钮现在显示:“LP:4000” 我将如何做到这一点?如果这不是完全可能的,我也可以在按下按钮后获得输入。我什至似乎都无法做到这一点。 Kivy 很新,Python 也很新。 按键代码(KV文件):

    <FloatLayout>:
        Button:
           name: 'LP'
           id: LP
           text: "LP: 4000"
           size_hint: 0.14, 0.15
           pos_hint: {"left": 1, "top": 0.8105}

类(Py 文件):

    class LPInput(Widget):
        pass

输入代码(KV 文件):

    <LPInput>:
        size_hint: 0.14, 0.15
        pos_hint: {"left": 1, "top": 0.8105}

        TextInput:
            id: lifepoint
            text: ""

        Label:
            id: currlp #not sure this is doing anything
            text: "LP: "

我已经编写了一些其他的代码片段来尝试通过不同的方法来创建它,但在我沮丧中我保存了我的文件,其中保存了这些,所以这就是我目前所拥有的。

【问题讨论】:

  • 欢迎来到 Stack Overflow。 LPInput 是根小部件吗?

标签: python button kivy textinput


【解决方案1】:

下面的解决方案假设 LPInput 是根小部件。

<LPInput>:
    size_hint: 0.14, 0.15
    pos_hint: {"left": 1, "top": 0.8105}

    TextInput:
        id: lifepoint
        text: ""

    Label:
        id: currlp #not sure this is doing anything
        text: "LP: "

<FloatLayout>:
    Button:
       name: 'LP'
       id: LP
       text: "LP: "
       size_hint: 0.14, 0.15
       pos_hint: {"left": 1, "top": 0.8105}
       on_release:
           self.text = "LP: " + app.root.ids.lifepoint.text

Kv language » Rule context

Kv 语言特有的三个关键字:

app:始终引用您的应用程序的实例。

root:指当前规则中的基础小部件/模板

self:始终引用当前小部件

示例

main.py

​​>
from kivy.lang import Builder
from kivy.base import runTouchApp

runTouchApp(Builder.load_string('''
#:kivy 1.11.0
FloatLayout:

    TextInput:
        id: lifepoint
        text: ""
        size_hint: 0.14, 0.15
        pos_hint: {"left": 1, "top": 0.8105}

    Button:
        name: 'LP'
        id: LP
        text: "LP: "
        size_hint: 0.14, 0.15
        pos_hint: {"right": 1, "top": 0.8105}
        on_release:
            self.text = "LP: " + root.ids.lifepoint.text
'''))

输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多