gpt4 book ai didi

android - kivy - python - Spinner 输出文本/字符串如何使用

转载 作者:太空狗 更新时间:2023-10-29 13:24:16 24 4
gpt4 key购买 nike

我想将 Spinner 中的 Selected 字符串值用于 Label 文本,但不能这样做,如以下代码所示。帮助任何人!!!

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)

倒数第二行即 "dlabel=Lab​​el(text = "%s"% str(spinner.text)) 在程序运行时未在 dlabel 文本中显示选定的微调器文本。如何获取 Spinner 索引或字符串值以在代码中的任何位置使用?

最佳答案

我猜你的缩进让人很难理解你的 init 方法。无论如何,想法是保存要设置文本的 Label 小部件的实例。在您的情况下,这是 dlabel。正确的代码如下所示:

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)

关于android - kivy - python - Spinner 输出文本/字符串如何使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23598173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com