2016-02-13 62 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},