K
K
Kirill3122020-09-13 15:14:14
Python
Kirill312, 2020-09-13 15:14:14

I wanted to ask about python kivy ... I don’t understand anything at all (Can you help?

Gentlemen... I've only been programming for 5 months recently and I decided to try kivy... Please don't swear at my code... I've looked at a bunch of YouTube videos and a bunch of sites about kivy... But still kivy is difficult for me.. ..
First of all, I don’t understand how to make the code not be executed all at once, but gradually .... Like, I have a problem, I want to make as many buttons as the user sets in the TextInput and then give this value to the next loop, but the code is executed all at once I don't understand what to do at all Help me please... That's what I wanted to do

class ScreenTwo7(Screen):
  def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self.name = 'Sc7'
    AncL1 = AnchorLayout(anchor_y = 'top')
    NumberS1 = GridLayout(cols = 2, spacing = 2, padding = [0, 100, 0, 100] )
    AncL1.add_widget(NumberS1)
    for i in range(1):
      
      self.t2 = TextInput(text = 'Введите кол-во элементов выборки...')
      NumberS1.add_widget(self.t2)
      
      self.b2 = Button(text = 'Вывести поля для выборки')
      self.b2.bind(on_press = self.cicle)
      NumberS1.add_widget(self.b2)

      
    self.add_widget(AncL1)		
  def cicle(self, t):
    for n in  range(int(t.text)):
      NumberS1 = GridLayout(cols = 2, spacing = 2, padding = [0, 100, 0, 100] )
      self.t2 = TextInput(text = '#1')
      NumberS1.add_widget(self.t2)
      self.add_widget(NumberS1)


And there was also a question about how to transfer a value from one class to another ... I would like the user to press the button 7 on one screen, this number would be remembered in some variable and transferred to the next screen thanks for the answers ... If will..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
Umpiro, 2020-09-14
@Umpiro

So that the code is not executed all at once, but depending on the user's actions, you need to use events.
For example, on_text_validate of the TextInput widget.

#!/usr/bin/python
import kivy
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

class RootWidget(GridLayout):
    def __init__(self, **kwargs):
        super(RootWidget, self).__init__(**kwargs)
        self.cols = 2
        self.add_widget(CreateInput(multiline=False))

class CreateInput(TextInput):
    def on_text_validate(self):
        for i in range(int(self.text)):
            self.parent.add_widget(Button(text=str(i)))

class MyApp(App):
    def build(self):
        return RootWidget()

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question