Kivy语言动态复选框列表

时间:2015-08-30 20:41:15

标签: android python checkbox kivy

我想在Kivy语言中动态添加复选框。我知道如何在python中实现这一点,但不知道如何在Kivy语言中这样做。 下面列表中的每个文件都应该有一个复选框:

 from kivy.uix.filechooser import FileSystemLocal
 file_system = FileSystemLocal()
 file_list=file_system.listdir(App.get_running_app().user_data_dir+'\\')   # this returns a list of files in dir
 file_list=[x for x in file_list if x[-4:]=='.csv']

我怎样才能循环这个Kivy?我假设应该在右侧循环,因为那是python代码。但我不知道怎么样?任何先发者都可以提供帮助。

修改

class MergeScreen(Screen):
    result_label=''
    check_boxes={}
    def __init__(self,**kwargs):
        self.name='MergeScreen'
        super(MergeScreen, self).__init__(**kwargs)
        b=BoxLayout(orientation='vertical')
        file_system = FileSystemLocal()
        file_list=file_system.listdir(App.get_running_app().user_data_dir+'\\')   # this returns a list of files in dir
        file_list=[x for x in file_list if x[-4:]=='.csv']
        for file_1 in file_list:
            g=GridLayout(cols=2)
            c=CheckBox()
            l=Label(bold= True,font_size=20,text=file_1)
            self.check_boxes[c]=file_1
            g.add_widget(l);g.add_widget(c)
            b.add_widget(g)
        g=GridLayout(cols=2)
        l=Label(bold= True,font_size=20,text='Result Name')
        t=TextInput(font_size=25)
        self.result_label=t
        g.add_widget(l)
        g.add_widget(t)
        b.add_widget(g)
        g_options=GridLayout(cols=2)
        g_options.add_widget(Button(text="Okay",on_press=self.create_result))
        g_options.add_widget(Button(text="Back",on_press=self.return_back))
        b.add_widget(g_options)
        self.add_widget(b)        
    def return_back(self,btn):
        self.parent.current = 'ExperimentSelectScreen'    
    def create_result(self,btn):
        file_list=[]
        root_dir= App.get_running_app().user_data_dir+'\\'
        for key, value in self.check_boxes.iteritems():
            if key.active==True:
                file_list.append(root_dir+'\\'+value)
        result_name=self.result_label.text+'.csv'
        result_dir=root_dir+'results'
        if os.path.exists(result_dir)==False:os.makedirs(result_dir)
        result_path=result_dir+'\\'+result_name
        fout=open(result_path,"w+")
        # # first file:
        for line in open(file_list[0]):
            fout.write(line)
        # # now the rest:    
        for num in file_list[0:]:
            f = open(num)
            f.next() # skip the header
            for line in f:
                fout.write(line)
            f.close() # not really needed
        fout.close()
        file_create=Popup(title='Confirmation',content=Label(text="Merged File has stored at\n"+result_path)\
                ,size_hint=(.75,.75),auto_dismiss=True)
        file_create.open()
        Clock.schedule_interval(file_create.dismiss, 3) 

问题是我如何在创建文件时添加复选框,因为init()只运行一次。我每次都重新启动应用程序。

1 个答案:

答案 0 :(得分:1)

您无法使用kv语言动态添加小部件,除非在例如self.add_widget(Something())中写入on_press之类的内容。事件绑定,例如 echo " <div class='formTitle' style='position: relative; text-align: center; margin-top: 20px'>Medals</div> <table class='profileTable' id='medalTable' style='border-top-width: 0px'> "; $mID = $_GET['mID']; $result = $mysqli->query("SELECT * FROM ".$dbprefix."clanawards_members WHERE maa_member_id = '$mID'"); while($row = $result->fetch_assoc()) { //RETRIEVE ALL COLUMN NAMES then run a foreach to run each one through the next query foreach($row as $key => $val) { // $result = $mysqli->query("SELECT * FROM ".$dbprefix."clanawards WHERE EXISTS name = '$key'"); // while($row = $result->fetch_assoc()) { // $awardName = $row['name']; // $description = $row['description']; echo " <tr><td class='main' align='center'>$key<br>$awardName</td></tr> "; //} } } echo "</table>"; 。我不认为它适合你正在做的事情,这听起来像你应该在Python方面做的任务。