Question

I want to use the Selected string value from Spinner to the Label text but couldn't do it as can be seen in the following code. Help anyone !!!

class LoginScreen(GridLayout):
   def __init__(self, **kwargs):
      tex=' '
      jj=0
      super(LoginScreen, self).__init__(**kwargs)
      self.cols = 2
      self.add_widget(Label(text='Pressure (barg)',height=11 ))
      self.username = TextInput(multiline=False, height=11)
      self.add_widget(self.username)
      self.add_widget(Label(text='Temperature (deg C)'))
      self.password = TextInput(multiline=False, height=11)
      self.add_widget(self.password)
      spinner = Spinner(text='Select..', values=s, size_hint=(None, None))

  def show_selected_value(spinner, text):
      print('The spinner','have text', text)
      jj=0
      for j in range(0,852,1):
      if b3112012[j][6]==text:
          tex=text
          jj=j+1
          break
      else:
          pass
      print('The index of selected is ', jj)
      print(tex)
      tex=text
      print('Index is:', jj)

      spinner.bind(text=show_selected_value)
      self.add_widget(spinner)
      #print('value' , show_selected_value)
      dlabel=Label(text="%s" % str(spinner.text))
      self.add_widget(dlabel)

The 2nd last line i.e "dlabel=Label(text = "%s" % str(spinner.text)) is not showing the selected spinner text in the dlabel text when the program run. How can I get Spinner index or String value to use anywhere in the code ?

Was it helpful?

Solution

I guess your indentation make it hard to understand your init method. Anyway, the idea is to save the instance of the Label widget you want to set the text. In your case, this is dlabel. The correct code would look like:

class LoginScreen(GridLayout):
   def __init__(self, **kwargs):
      super(LoginScreen, self).__init__(**kwargs)
      self.cols = 2
      self.add_widget(Label(text='Pressure (barg)',height=11 ))
      self.username = TextInput(multiline=False, height=11)
      self.add_widget(self.username)
      self.add_widget(Label(text='Temperature (deg C)'))
      self.password = TextInput(multiline=False, height=11)
      self.add_widget(self.password)
      spinner = Spinner(text='Select..', values=s, size_hint=(None, None))
      self.add_widget(spinner)
      self.dlabel = Label(text="%s" % str(spinner.text))
      self.add_widget(self.dlabel)

      def show_selected_value(spinner, text):
          """
          # dunno what is that for.
          jj=0
          for j in range(0,852,1):
              if b3112012[j][6]==text:
                  tex=text
                  jj=j+1
                  break
          """
          self.dlabel.text = text


      spinner.bind(text=show_selected_value)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top