【问题标题】:adding two or more Values in kivy and getting the result dynamic array在kivy中添加两个或多个值并获得结果动态数组
【发布时间】:2020-05-24 08:52:03
【问题描述】:

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

我需要一个动态数组,用户可以在第一个TextInput Box中输入第一个值,然后他可以按下按钮“换行”,他得到在新的文本输入框中输入 第二个值 的可能性,然后他可以再次按下按钮“新行”.. 他有可能在输入框中输入第三个值新的文本输入框.. 在任何时候他都可以按“结果” ..在标签中得到这个值的总和

如何制作这个动态数组? 谢谢

【问题讨论】:

    标签: python arrays dynamic sum kivy


    【解决方案1】:

    H 洛斯里克.. 这是代码 main.py

        from kivy.uix.textinput import TextInput
        from kivy.app import App
        from kivy.lang import Builder
        from kivy.uix.screenmanager import ScreenManager, Screen
    
    
        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
    

    这是代码

    【讨论】:

      【解决方案2】:

      有什么问题?制作一个 TextInputs 列表,然后从中获取值。像这样的:

      # create a list with first text input:
      self.textlist = [TextInput()]
      # I don't know which layout you are using, for example BoxLayout with box variable. Add TextInput to box:
      self.box.add_widget(self.textlist[0])
      
      # function to create new inputs, that button 'new line' calls:
      def addnewtextinput(self):
          self.textlist.append(TextInput())
          self.box.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)
          return result
      

      【讨论】:

      • 嗨 Lothric,感谢您的回答,我应该在哪里创建带有第一个文本输入的列表:在 main.kv 或 main.py
      • 在 .py 文件中。您可以在 .kv 文件中创建第一个 TextInput 但它会更复杂,您必须从那里获取它并附加到列表中,您不需要额外的代码。
      【解决方案3】:

      好的,你的情况是这样的:

      .py 文件:

      # import this
      from kivy.properties import ObjectProperty
      from kivy.clock import Clock
      ...
      class MainWindow(Screen):
      
          def __init__(self, **kwargs):
              super(MainWindow, self).__init__(**kwargs)
              # we have to delay running that function, it will run when kv file will be ready to provide widgets by id
              Clock.schedule_once(self.getids)
              self.counter = 1
              self.textlist = [TextInput()]
              self.grid.add_widget(Label(text='Input value ' + counter))
              self.counter += 1
              self.grid.add_widget(self.textlist[0])
      
          # don't forget this
          grid = ObjectProperty(None)
      
          # function to connect layout with the variable
          def getids(self):
              self.grid = self.ids.grid
      
      # function to create new inputs, that button 'new line' calls:
          def addnewtextinput(self):
              self.grid.add_widget(Label(text='Input value ' + self.counter))
              self.counter += 1
              self.textlist.append(TextInput())
              self.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):
                  self.b1 = WindowManager()
                  self.b1.add_widget(MainWindow())
                  return self.b1
      

      .kv 文件:

      <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 
      

      【讨论】:

      • 嗨 Lothric,感谢您的帮助,我得到了这个按摩:NameError: name 'self' is not defined .... 问题更复杂:counter = 1 self.textlist = [TextInput() ] self.ids.grid.add_widget(Label(text='输入值' + counter)) counter += 1 self.ids.grid.add_widget(self.textlist[0])
      • self.textlist = [TextInput()]
      • self.textlist = [TextInput()] NameError: name 'self' is not defined
      • 我已经编辑了代码,试着把它放在init函数中
      • 我有一个新的 Proplem,文件“C:/Users/HP/PycharmProjects/untitled2/main.py”,第 14 行,在 init self.ids.grid .add_widget(Label(text='Input value ' + counter)) 文件“kivy\properties.pyx”,第 863 行,在 kivy.properties.ObservableDict.__getattr__ AttributeError: 'super' object has no attribute 'getattr'
      猜你喜欢
      • 2020-05-17
      • 1970-01-01
      • 2019-11-13
      • 2020-05-26
      • 2015-09-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2012-08-21
      相关资源
      最近更新 更多