在 kivy window 中添加更多值并在相同的 Window 动态数组中获取新标签中的结果

adding more Values in kivy window and getting the result in new Label in the same Window , dynamic array

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

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

如何创建这个动态数组?非常感谢

这是我的 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

Builder.load_file('MyMain.kv')
class WindowManager(ScreenManager):
    pass


class MainWindow(Screen):

    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)
        self.counter = 1
        Clock.schedule_once(self.add_stuff)

    def add_stuff(self, *args):
        self.textlist = [TextInput()]
        self.ids.grid.add_widget(Label(text='Input value {}'.format(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 ' + str(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 MyMainApp(App):
    def build(self):
        b1=WindowManager()
        MainWindow()
        return b1


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

这是MyMain.kv

<CustButton@Button>:
    font_size: 40

<WindowManager>:
    MainWindow:

<MainWindow>:
    name: "main"
    # don't forget to add this
    grid: grid.__self__

    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

我看不到基维 Window .. 你能帮我吗?

您的代码中有一些错误,但您需要做的是 运行 这是:

  • 首先,将根小部件 WindowManager: 更改为规则 <WindowManager>:
  • 其次,在小部件完成初始化时添加东西,使用Clock.schedule_once(self.add_stuff)

.py 应该是这样的:

class WindowManager(ScreenManager):
    pass


class MainWindow(Screen):

    def __init__(self, **kwargs):
        super(MainWindow, self).__init__(**kwargs)
        self.counter = 1
        Clock.schedule_once(self.add_stuff)

    def add_stuff(self, *args):
        self.textlist = [TextInput()]
        self.ids.grid.add_widget(Label(text='Input value {}'.format(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 ' + str(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 MyMainApp(App):
    def build(self):
        b1=WindowManager()
        MainWindow()
        return b1


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

回答为什么你看不到任何东西:

如果您想在不调用 Builder.load_file 的情况下自动加载您的 kv 文件,那么您需要确保您的 kv 文件的名称是您的 App class 减去 App 部分的名称。

例如,在您的情况下,您的 kv 文件应命名为 mymain.kv 而不是 main.kv

手动加载kv文件请使用builder

Builder.load_file('main.kv')

请参阅 https://kivy.org/doc/stable/guide/lang.html 以获得更多帮助。

在这个 NoEmbryos 回答之后,您应该可以完成剩下的工作了。 希望对您有所帮助!