在两个文本输入中添加四个值,并在标签kivy中获得结果

2024-09-28 16:58:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试构建一个可以计算两个Textinput中四个值之和的应用程序。我有以下代码: 有一个文本输入,用户可以在此文本输入中输入一个数字,然后用户可以按“回车”,然后给出第二个文本输入,在第二个文本输入中用户可以输入另一个值, 如果用户按下“空格”,他会得到结果,即第一个值和第二个值的总和

我的代码: main.py

from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.textinput import TextInput
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock



Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (400, 200)


class WindowManager(ScreenManager):
    pass




class BestWindow(Screen):

    def __init__(self, **kwargs):
        super(BestWindow, self).__init__(**kwargs)
        Window.bind(on_key_down=self._on_keyboard_down)
        self.counter = 1
        Clock.schedule_once(self.add_stuff)

    def add_stuff(self, *args):
        self.textlist = [TextInput()]
        self.textinput = TextInput(focus=True)
        self.ids.grid.add_widget(Label(text='Input value {}'.format(self.counter)))
        self.counter += 1
        self.ids.grid.add_widget(self.textlist[0])


    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])

    def getresult(self):

        result = 0
        for i in self.textlist:
            result += int(i.text)
        self.ids.label_id.text = str(result)



    def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
        if self.textinput.focus and keyboard== 13:  # 32 - Cpace key presssed Ascii
            self.addnewtextinput()
        if self.textinput.focus and keyboard== 32:  # 32 - Cpace key presssed Ascii
            self.getresult()




kv = Builder.load_file("main.kv")
class TestApp(App):
    def build(self):
        b1 = WindowManager()
        return b1



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

main.kv文件:

<CustButton@Button>:
    font_size: 40

<WindowManager>:
    BestWindow:


<BestWindow>:

    name: "erst"
    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




        GridLayout:
            spacing: 10
            cols:2

            Button:
                id : test3
                focus: False
                text: "Result!"
                on_press: app.root.current = "therd"

            Label:
                id:label_id
                multiline: True
            BoxLayout:
                id: result_layout
                orientation: "horizontal"

现在我要做以下几件事: 1。光标必须在第一个文本输入中2。当我按“Enter”时,光标必须位于新的文本输入中。 3。在每个文本输入中输入多个值,并在值之间使用Komma, e、 g.第一个文本输入:1,3,2第二个文本输入:3,5,3,5结果:4 8 5 5 我怎么做? 多谢各位


Tags: text用户from文本importselfaddid