0

I have a simple program with a spinner widget to select a direction. I need to pass that direction as a variable that can be used in other classes. In the code below, I just want to print the direction in my main app class

#main.py

from kivy.uix.widget import Widget
from kivy.app import App


class MenuKV(Widget):
    spun = False
    value = str          
    
    def on_spinner_select(self, value):
      # print (val)
        self.value = value  
        self.spun = True
    

class MenuScreen(App):
    def build(self):
        
        if MenuKV.spun == True:
            print ("your Going "+ MenuKV.value)
   
   
   
MenuScreen().run()

#kv file #menuscreen.kv

MenuKV:     
   
<MenuKV>:

    BoxLayout:
        orientation: 'horizontal'
        pos_hint: {"x":0, "top":1} 
        size_hint: 1, None
        height: "44dp"
        width: "200dp"
        spacing: "10dp"
        padding: "10dp"
        pos: 0, root.height - 60
        

            
        Spinner:
     
            id: direction
            text: 'North'
            values: 'North', 'South', 'East', 'West'
            on_text:
                value = direction.text
                root.on_spinner_select(value)


        
        Button:    
        
            text: "future dev"  

1 Answer 1

0

Firstly, your code as it is doesn't seem to work as the spinner is not even displayed when you start the app. I assume this was not an intention as to actually print something, your code expects that a value has been selected on the spinner.

Secondly, why are you trying to print the selected value in the App class and not directly from the function that generated it?

If I modify the code in your .py file to actually display the widgets, then to make the direction printed to terminal each time you select a value, all you need to do is this:

from kivy.uix.widget import Widget
from kivy.app import App


class MenuKV(Widget):
#     spun = False # not needed, commented out
#     value = str  # not needed, commented out        
    
    def on_spinner_select(self, value):
      # print (val)
        self.value = value  
#        self.spun = True # not needed, commented out
        print(f"You're going {value}") # prints value to terminal
    

class MenuScreen(App):
    def build(self):
        return MenuKV() # added to actually start and display widget
    
#         if MenuKV.spun == True: # not needed, commented out
#             print ("your Going "+ MenuKV.value) # not needed, commented out
   
MenuScreen().run()

If you really need to share variables across classes, one way to do it is to initiate variables outside the classes and let individual classes read/write them declaring them to be global inside your class functions.

For example doing the below, you could access the selected_value from any other class and/or function:

# initiate a variable
selected_value = ''

class MenuKV(Widget):
    def on_spinner_select(self, value):
        self.value = value
        # call the global variable
        global selected_value 
        # overwrite the global variable
        selected_value = value
    

class MenuScreen(App):
    def build(self):
        return MenuKV() 
   
MenuScreen().run()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.