self.ids.clayout.ids提供空字典,即使它包含小部件

时间:2016-06-16 13:37:45

标签: python dictionary kivy

在此代码中,self.ids会返回包含id: clayout子元素的字典。在__init__()中,我向BoxLayout(id:clayout)添加了五个按钮。但当我得到self.ids.clayout.ids时,它给了我空字典。我找不到的小错误。请提前帮助和感谢。

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.lang import Builder
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.checkbox import CheckBox

import xlrd
import openpyxl
Mess = {
            'mess1': 'RR - SI',
            'mess2': 'VTH - SI',
            'mess3': 'SK - SI',
            'mess4': 'CV - SI',
            'mess5': 'CR - NI',
            'mess6': 'AD - NI',
            'mess7': 'MD - NI',
            'mess8': 'MDJ - NI',
            'mess9': 'FC - HM'  
}
Builder.load_string('''
<ChoiceLayout>
    orientation:'vertical'
    Label:
        text: "Mess Registration for Jul-Nov 2016"
        size_hint: (None,None)
        font_size:'48dp'
        size_hint_y:1
        pos_hint: {'center_x': .5}
    BoxLayout:
        id: blayout
        orientation:'horizontal'
        size_hint_y:2
        Label:
            text: 'NAME : {}'.format('R GOKULAN')
            id: name
            size_hint: (1,None)
            font_size:'36dp'
            pos_hint: {'left': 1}
        Label:
            text: 'ROLL NUMBER : {}'.format('CS15B033')  #Put some variable for Roll number here as rnum
            id: rollno
            size_hint: (1.25,None)
            font_size:'36dp'
            pos_hint: {'right': 1}
    BoxLayout:
        orientation:'vertical'
        size_hint_y: 1
        id: clayout
    Button:
        id: submit
        text: 'Submit'
        size_hint: (None,None)
        height: "36px"
        width: '50px'
        pos_hint: {'center_x': .5}
        on_press: root.register_mess()

    ''')



class ChoiceLayout(BoxLayout):
    choices = []
    def __init__(self,*args,**kwargs):
        super(ChoiceLayout, self).__init__(*args, **kwargs)
        # self.drop_down1 = CustomDropDown()
#------------------------------------------------------------------------
        dropdown1 = DropDown()
        for mess,mname in Mess.iteritems():
            btn = Button(text=mname , size_hint_y=None, height=30)
            btn.bind(on_release=lambda btn: dropdown1.select(btn.text))
            dropdown1.add_widget(btn)
        mainbutton1 = Button(text='Choice 1', size_hint=(1, 1), id='btn1')
        mainbutton1.bind(on_release=dropdown1.open)
        dropdown1.bind(on_select=lambda instance, x: setattr(mainbutton1, 'text', x))
#------------------------------------------------------------------------
        dropdown2 = DropDown()
        for mess,mname in Mess.iteritems():
            btn = Button(text=mname , size_hint_y=None, height=30)
            btn.bind(on_release=lambda btn: dropdown2.select(btn.text))
            dropdown2.add_widget(btn)
        mainbutton2 = Button(text='Choice 2', size_hint=(1, 1), id='btn2')
        mainbutton2.bind(on_release=dropdown2.open)
        dropdown2.bind(on_select=lambda instance, x: setattr(mainbutton2, 'text', x))
#------------------------------------------------------------------------
        dropdown3 = DropDown()
        for mess,mname in Mess.iteritems():
            btn = Button(text=mname , size_hint_y=None, height=30)
            btn.bind(on_release=lambda btn: dropdown3.select(btn.text))
            dropdown3.add_widget(btn)
        mainbutton3 = Button(text='Choice 3', size_hint=(1, 1), id='btn3')
        mainbutton3.bind(on_release=dropdown3.open)
        dropdown3.bind(on_select=lambda instance, x: setattr(mainbutton3, 'text', x))
#-----------------------------------------------------------------------
        dropdown4 = DropDown()
        for mess,mname in Mess.iteritems():
            btn = Button(text=mname , size_hint_y=None, height=30)
            btn.bind(on_release=lambda btn: dropdown4.select(btn.text))
            dropdown4.add_widget(btn)
        mainbutton4 = Button(text='Choice 4', size_hint=(1, 1), id='btn4')
        mainbutton4.bind(on_release=dropdown4.open)
        dropdown4.bind(on_select=lambda instance, x: setattr(mainbutton4, 'text', x))
#-----------------------------------------------------------------------
        dropdown5 = DropDown()
        for mess,mname in Mess.iteritems():
            btn = Button(text=mname , size_hint_y=None, height=30)
            btn.bind(on_release=lambda btn: dropdown5.select(btn.text))
            dropdown5.add_widget(btn)
        mainbutton5 = Button(text='Choice 5', size_hint=(1, 1),id='btn5')
        mainbutton5.bind(on_release=dropdown5.open)
        dropdown5.bind(on_select= lambda instance, x: setattr(mainbutton5, 'text', x))
        self.ids.clayout.add_widget(mainbutton1)
        self.ids.clayout.add_widget(mainbutton2)
        self.ids.clayout.add_widget(mainbutton3)
        self.ids.clayout.add_widget(mainbutton4)
        self.ids.clayout.add_widget(mainbutton5)

    def register_mess(self):
        workbook = openpyxl.load_workbook('database.xlsx')
        sheet1 = workbook.get_sheet_by_name('Sheet2')
        i=2
        # while(sheet1['A'+str(i)] != None):
        #if str(sheet1['A'+str(i)]) == 'cs15b033':
        # sheet1['B'+str(i)] = Mess.keys()[Mess.values().index(self.ids.btn1.text)]
        # sheet1['C'+str(i)] = Mess.keys()[Mess.values().index(self.ids.btn2.text)]
        # sheet1['D'+str(i)] = Mess.keys()[Mess.values().index(self.ids.btn3.text)]
        # sheet1['E'+str(i)] = Mess.keys()[Mess.values().index(self.ids.btn4.text)]
        # sheet1['F'+str(i)] = Mess.keys()[Mess.values().index(self.ids.btn5.text)]
        print self.ids.clayout.ids
        i += 1
        workbook.save('database.xlsx')
        print 'success'


class RegisterApp(App):
    def build(self):
        return ChoiceLayout()


if __name__ == '__main__':
    RegisterApp().run()

1 个答案:

答案 0 :(得分:0)

ids字典仅由kv lang解析器填充。您必须自己跟踪动态创建的小部件。例如,您可以将小部件存储在ListProperty中。