2016-02-13 104 views
0

当从文件添加项目到kivy列表视图listitemlabel标签的间距似乎重叠新的添加,我不确定从哪个部分。当开始时列表为空并添加了新添加项时,间距不会在标签之间重叠,但如果我的_reset_spopulate出现问题,则不知道如何解决。Kivy listview添加重叠的项目

from kivy.app import App 
from kivy.uix.button import Button 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.textinput import TextInput 
from kivy.adapters.simplelistadapter import SimpleListAdapter 
from kivy.uix.listview import ListView, ListItemButton, ListItemLabel 

class Controller(GridLayout): 
    data = ['0','1','2'] 
    test_list=[] 
    data0 = ['hello','2','3','2'] 
    data1 = ['world','e','r','young'] 
    data2 = ['1'] 
    all_list = [] 
    all_list.append(data0) 
    all_list.append(data1) 
    all_list.append(data2) 
    info_data = [] 
    list_x = 0 

    def __init__(self, **kwargs): 
     super(Controller,self).__init__(**kwargs) 
     self.x = 0 

     self.my_list.adapter.bind(on_selection_change = self.selection_change) 
     self.my_list.adapter.cls.deselected_color = (1,1,1,1) 
     self.my_list.adapter.cls.selected_color = (1,0,1,1) 

    def repopulate(self): 
     if(hasattr(self.my_info, '_reset_spopulate')): 
      self.my_info._reset_spopulate() 

    def on_load_files(self): 
     pass 

    def on_enter(self): 
     self.all_list[self.list_x].append(self.my_in.text) 
     self.my_info.adapter.data.append(self.my_in.text) 
     self.repopulate() 

    def selection_change(self,adapter,*args): 
     if len(adapter.selection)!=0: 
      self.list_x=int(adapter.selection[0].text[0]) 
      #for x in xrange(0,len(self.all_list[self.list_x])): 
      self.my_info.adapter.data = self.all_list[self.list_x] 
       #self.my_info.adapter.data.append(self.all_list[self.list_x][x]) 
      self.repopulate() 

    def load_test(self): 
     with open('./data0.txt','r+') as d0: 
      for line in d0.readlines(): 
       try: 
        self.data0.append(line) 
       except ValueError: 
        pass 
      for x in xrange(0,len(self.data0)): 
       self.data0 = [i.strip('\r\n') for i in self.data0] 

    def load_files(self): 
     with open('./data0.txt','r+') as d0: 
      for line in d0.readlines(): 
       try: 
        self.test_list.append(line) 
       except ValueError: 
        pass 
      for x in xrange(0,len(self.test_list)): 
       self.test_list=[i.strip('\n') for i in self.test_list] 

     self.data0 = self.test_list 
     self.all_list.append(self.data0) 

     with open('./data1.txt','r+') as d0: 
      for line in d0.readlines(): 
       try: 
        self.test_list.append(line) 
       except ValueError: 
        pass 
      for x in xrange(0,len(self.test_list)): 
       self.test_list=[i.strip('\n') for i in self.test_list] 

     self.data1 = self.test_list 
     self.all_list.append(self.data1) 

     with open('./data2.txt','r+') as d2: 
      for line in d2.readlines(): 
       try: 
        self.test_list.append(line) 
       except ValueError: 
        pass 
      for x in xrange(0,len(self.test_list)): 
       self.test_list=[i.strip('\n') for i in self.test_list] 

     self.data2 = self.test_list 
     self.all_list.append(self.data2) 

    def write_files(self): 
     with open('./data0.txt','w+') as d0: 
      d0.seek(0) 
      for x in xrange(0,len(self.data0)): 
       d0.write(self.data0[x]+'\n') 
      d0.seek(0) 
      for x in xrange(0,len(self.data0)): 
       print d0.readline().strip('\n') 

     with open('./data1.txt','w+') as d0: 
      d0.seek(0) 
      for x in xrange(0,len(self.data1)): 
       d0.write(self.data1[x]+'\n') 
      d0.seek(0) 
      for x in xrange(0,len(self.data1)): 
       print d0.readline().strip('\n') 

     with open('./data2.txt','w+') as d2: 
      d2.seek(0) 
      for x in xrange(0,len(self.data2)): 
       d2.write(self.data2[x]+'\n') 
      d2.seek(0) 
      for x in xrange(0,len(self.data2)): 
       print d2.readline().strip('\n') 

    def on_write_files(self): 
     pass#self.write_files() 


class NameApp(App): 
    def build(self): 
     controller = Controller() 
     controller.load_test() 
     return controller 



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

这里是name.kv

#:kivy 1.4.0 
#:import label kivy.uix.label 
#:import sla kivy.adapters.simplelistadapter 
#:import la kivy.adapters.listadapter 
#:import lib kivy.uix.listview 

<Label>: 
    text_size: self.size 
    halign: 'left' 
    valign: 'middle' 

<Controller>: 
    my_in:my_in_id 
    my_info:my_info_id 
    my_list:my_list_id 
    cols:1 
    FloatLayout: 
     Button: 
      text:'on_load_files()' 
      size_hint:None,None 
      size:100,30 
      pos:100,100 
      on_press:root.on_load_files() 
     ListView: 
      id:my_list_id 
      size_hint:.2,.6 
      size:00,400 
      pos_hint:{'x':.2,'y':.2} 
      pos:200,200 
      adapter: 
       la.ListAdapter(
       data=root.data, 
       cls=lib.ListItemButton, 
       selection_mode='single', 
       allow_empty_selection=True 
       ) 
     ListView: 
      id:my_info_id 
      size_hint: .4,.6 
      pos:400,200 
      adapter: 
       la.ListAdapter(
       data=root.info_data, 
       cls=lib.ListItemLabel, 
       selection_mode='single', 
       allow_empty_selection=True) 

     Button: 
      size_hint:None,None 
      size:100,30 
      pos:0,0 
      text:'enter/clear' 
      on_press:root.on_write_files() 
     TextInput: 
      size_hint:.8,None 
      size:400,30 
      pos:100,0 
      background_color:0.2,0.2,0.2,1 
      foreground_color:1,1,1,1 
      id:my_in_id 
      multiline:False 
      text:'enter new item' 
      on_text_validate:root.on_enter() 

回答

1

使用args_converter并设置项目的高度在列表中没有它切断标签或线来解决这个

方式
args_converter = lambda row_index, x: {'text': '{0:^20}'.format(x), 'size_hint_y': None, 'height': 55},