Send multiple selected Spinner values forward as a list into a python script

674 views
Skip to first unread message

Mundi

unread,
Apr 19, 2017, 8:59:16 AM4/19/17
to Kivy users support

Hi, I am relatively new to Python and Kivy. I am completely stuck trying to grasp how to take the spinner values that I select, and send them forward as a list into an algorithm I have written in Python.

On my <MainScreen> I have 4 spinners. I would like to be able to select the desired value, and once all values are selected, I want to have them in a list, so I can input that list into a calculation. I cannot figure out how to get them all into a list. From my research, I believe I am going to need to use the bind function but I have had no luck with this. Someone please help 😊


My .py and .kv file is attached.


Thankyou




Main-question.py
main-question.kv

Fari Tigar

unread,
Apr 19, 2017, 10:21:27 AM4/19/17
to Kivy users support
You need to understand the concepts of Kivy properties, which implement the Observable pattern. That means, you define a variable, every time the value of the variable changes an method is called along with parameters. If you define a variable foo the method will be def on_foo(self):
in this method you can react to changes. If you like another method to react to a change of the variable foo, you use the bind method.

Another part of properties, is that is links kv with Python. Keep in mind at the end of the day a kv file needs to be compiled somehow to python code. In kv when you intend a class it gets the child of the class, to be able to reference it by name you should have an id for every class you like to reference.


In your case you could have something like that:
The WeatherSelection class is now known to the mainscreen using an ObjectProperty
<MainScreen>:                                   # This is the Main screen class , it has a float layout with my spinner (defined after in the SamBoxLayout), and a button to cacluate
    name: "main"
    weather_selection:weather_selection
   
FloatLayout: # allows us to have two widgets on one screen
        WeatherSelection
           
id:weather_selection
       
Button:
           
color: 0,1,0,1
            font_size: 15
            size_hint: 0.15,0.15
            text: "Calculate"
            on_release: root.print_info();app.root.current= "other"
            pos_hint: {"right":1, "bottom":1}


in the py file you can now reference from the main screen to the weatherselection class like that:


import kivy
import numpy as np

kivy.require("1.9.1")                       # My version of Kivy

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.widget import Widget


class Manager(ScreenManager):
   
pass


class MainScreen(Screen):

   
def __init__(self, **kwargs):
       
super(MainScreen, self).__init__(**kwargs)

   
def print_info(self):
       
value= self.weather_selection.ids.spinner_id_1.text
        value_list
= [self.weather_selection.ids.spinner_id_1.text,
               
self.weather_selection.ids.spinner_id_2.text,
               
self.weather_selection.ids.spinner_id_3.text,
               
self.weather_selection.ids.spinner_id_4.text]

       
print(value_list)



class AnotherScreen(Screen):
   
pass


class WeatherSelection(BoxLayout):

   
# For Spinner
    def spinner_clicked(self, value):

       
print("spinner 1 {}:"
              "spinner 2 {}"
              "spinner 3 {}"
              "spinner 4 {}".format(self.ids.spinner_id_1.text,
                                   
self.ids.spinner_id_2.text,
                                   
self.ids.spinner_id_3.text,
                                   
self.ids.spinner_id_4.text))

#Live Weather#
# liveWeather=[value1, value2, value3, value4]

### INSERT CODE THAT WILL USE 'liveWeather[]'

presentation=Builder.load_file("main-question.kv")

class MyApp(App):

   
def build(self):
       
return presentation

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

dara mullins

unread,
Apr 19, 2017, 11:32:04 AM4/19/17
to kivy-...@googlegroups.com

Thanks a million Fari Tigar, I actually did read all of those topics mentioned at some point in my research but could not connect the dots. You have done a very nice job at marrying those features together in an understandable manner for me. I am back on the Kivy train, Thanks very much !

--
You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/fxKBrabJ1RE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages