Advertisement
Marski1

Recycleview2

Aug 22nd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.92 KB | None | 0 0
  1. Python file:
  2. from kivy.app import App
  3. from kivy.clock import Clock
  4. from kivy.lang import Builder
  5. from kivy.uix.behaviors import FocusBehavior
  6. from kivy.uix.boxlayout import BoxLayout
  7. from kivy.uix.carousel import Carousel
  8. from kivy.uix.image import Image
  9. from kivy.uix.label import Label
  10. from kivy.uix.popup import Popup
  11. from kivy.uix.screenmanager import ScreenManager, Screen
  12. from kivy.properties import ObjectProperty, StringProperty, ListProperty, BooleanProperty
  13.  
  14. import os
  15.  
  16.  
  17. from kivy.uix.recyclegridlayout import RecycleGridLayout
  18. from kivy.uix.recycleboxlayout import RecycleBoxLayout
  19. from kivy.uix.recycleview.layout import LayoutSelectionBehavior
  20. from kivy.uix.recycleview.views import RecycleDataViewBehavior
  21. from kivy.uix.behaviors import FocusBehavior
  22. from kivy.uix.recycleview import RecycleView
  23.  
  24. import gspread
  25. from oauth2client.service_account import ServiceAccountCredentials
  26. from openpyxl import load_workbook
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. class SecondScreen(Screen):
  34.     def __init__(self, **kwargs):
  35.         super(SecondScreen, self).__init__(**kwargs)
  36.  
  37.         Clock.schedule_once(self.post_init,0.1)
  38.  
  39.     def post_init(self, *args):
  40.  
  41.         path = r"xxx"
  42.         wb = load_workbook(path)
  43.         ws = wb.active
  44.         product_list = []
  45.  
  46.         for cell in ws['B']:
  47.             product_list.append(cell.value)
  48.  
  49.         self.ids.rv.data = [{'text': str(i)} for i in product_list]
  50.  
  51.         wb.close()
  52.         wb.save(filename=path)
  53.  
  54.     def add_to_excel(self):
  55.         path = r"xxx"
  56.         wb = load_workbook(path)
  57.         ws = wb.active
  58.  
  59.         ws.cell(column=2, row=ws.max_row + 1, value=self.add_item.text)
  60.         wb.close()
  61.         wb.save(filename=path)
  62.  
  63.  
  64.  
  65.     def refresh_rv_data(self):
  66.         SecondScreen.post_init(self)
  67.  
  68.  
  69. class testApp(App):
  70.     def build(self):
  71.         return SecondScreen()
  72.  
  73.    def update_index(self, data_index, index):
  74.         print('update data index: {}: {}'.format(data_index, index))
  75.         self.data[data_index]['index'] = index
  76.  
  77.     def delete(self, data_index):
  78.         print("delete {}".format(data_index))
  79.         self._remove(data_index)
  80.  
  81.     def _remove(self, data_index):
  82.         self.data.pop(data_index)
  83.         self.data = [{
  84.             'data_index': i,
  85.             'index': d['index'],
  86.             'height': d['height'],
  87.             'text': d['text']
  88.             }
  89.             for i, d in enumerate(self.data)
  90.         ]
  91.  
  92. testApp().run()
  93.  
  94. Kivy file:
  95.  
  96. #:import C kivy.utils.get_color_from_hex
  97.  
  98.  
  99. <SwipeButton@Carousel>:
  100.     text: ''
  101.     size_hint_y: None
  102.     height: 48
  103.     ignore_perpendicular_swipes: True
  104.     data_index: 0
  105.     min_move: 20 / self.width
  106.  
  107.     on__offset: app.update_index(root.data_index, self.index)
  108.  
  109.     canvas.before:
  110.         Color:
  111.             rgba: C('FFFFFF33')
  112.  
  113.         Rectangle:
  114.             pos: self.pos
  115.             size: self.size
  116.  
  117.         Line:
  118.             rectangle: self.pos + self.size
  119.  
  120.     Button:
  121.         text: 'delete ({}:{})'.format(root.text, root.data_index)
  122.         on_press: app.delete(root.data_index)
  123.  
  124.     Label:
  125.         text: root.text
  126.  
  127.     Button:
  128.         text: 'archive'
  129.         on_press: app.archive(root.data_index)
  130.  
  131.  
  132. #########
  133. <SecondScreen>:
  134.     add_item:add_item
  135.  
  136.     BoxLayout:
  137.         orientation:'vertical'
  138.         RecycleView:
  139.             id: rv
  140.             viewclass: 'SwipeButton'
  141.             RecycleGridLayout:
  142.                 cols:1
  143.                 key_selection: 'selectable'
  144.                 default_size: None, dp(40)
  145.                 default_size_hint: 1, None
  146.                 size_hint_y: None
  147.                 height: self.minimum_height
  148.                 multiselect: True
  149.                 touch_multiselect: True
  150.         BoxLayout:
  151.             TextInput:
  152.                 id: add_item
  153.  
  154.             Button:
  155.                 text:'Add item'
  156.                 on_release:
  157.                     root.add_to_excel()
  158.                     root.refresh_rv_data()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement