我想使用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=Label(text = "%s" % str(spinner.text))
在程序运行时未显示dlabel文本中选定的微调文本。如何在代码中的任何位置使用Spinner索引或String值?
答案 0 :(得分:2)
我想你的缩进会让你很难理解你的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)