在kivy窗口中添加更多值,并在同一Window的动态数组中的new Label中获得结果

问题描述 投票:0回答:1

我是Kivy的新手,需要您的帮助。我有一个小问题:

我需要一个动态数组,用户可以在其中在第一个TextInput字段中输入第一个值,然后他可以按“新建行”按钮,他有机会在新的TextInput字段中输入第二个值可以再次按下“ New Line”按钮,使他可以选择在新的TextInput字段中输入第三个值。使用“结果”,他可以随时在标签中调用这些值的总和。

如何创建此动态数组?非常感谢]]

这是我的main.py文件

from kivy.clock import Clock
from kivy.uix.textinput import TextInput
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty


class MainWindow(Screen):

    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)
        self.counter = 1
        self.textlist = [TextInput()]
        self.ids.grid.add_widget(Label(text='Input value ' + self.counter))
        self.counter += 1
        self.ids.grid.add_widget(self.textlist[0])

    # function to create new inputs, that button 'new line' calls:
    def addnewtextinput(self):
        self.ids.grid.add_widget(Label(text='Input value ' + self.counter))
        self.counter += 1
        self.textlist.append(TextInput())
        self.ids.grid.add_widget(self.textlist[-1])

    # function to get a result:
    def getresult(self):
        result = 0
        for i in self.textlist:
        # you may convert it to float if you need, like float(i.text)
            result += int(i.text)
        self.ids.label_id.text = str(result)


class WindowManager(ScreenManager):
    pass


class MyMainApp(App):
    def build(self):
        b1=WindowManager()
        MainWindow()
        return b1


if __name__ == "__main__":
    MyMainApp().run() 

这是我的main.kv

<CustButton@Button>:
    font_size: 40

<WindowManager>:
    MainWindow:

<MainWindow>:
    name: "main"

    GridLayout:
        cols:1

        # you will control that GridLayout from .py so here it's empty
        GridLayout:
            # set the id to be able to control it from .py file
            id: grid
            cols: 2

        CustButton:
            text: "new line"
            on_press: root.addnewtextinput()

        CustButton:
            text: "result"
            font_size: "30sp"
            on_press: root.getresult()

        TextInput:
            id:label_id
            font_size: 40
            multiline: True

我看不到猕猴桃的窗户..你能帮我吗?

我是Kivy的新手,需要您的帮助。我有一个小问题:我需要一个动态数组,用户可以在其中在第一个TextInput字段中输入第一个值,然后可以按“新行”按钮,...

python arrays dynamic kivy screen
1个回答
0
投票
有很多错误的东西,但是您需要执行以下操作:首先将根窗口小部件WindowManager:更改为规则<WindowManager>:小部件使用Clock.schedule_once(self.add_stuff)完成初始化后,添加第二个东西。.py应该是这样的:
© www.soinside.com 2019 - 2024. All rights reserved.