[Kivy] Need to render updated recycler view - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Kivy] Need to render updated recycler view (/thread-14275.html) Pages:
1
2
|
RE: Need to render updated recycler view - test - Nov-25-2018 I tried making the whole thing in python.. here is my attempt. import kivy kivy.require('1.0.8') import openpyxl from kivy.app import App from kivy.uix.button import Button from kivy.uix.scrollview import ScrollView from kivy.uix.gridlayout import GridLayout from kivy.uix.boxlayout import BoxLayout class AllApp(App): layout = BoxLayout(orientation='horizontal') layout_questions = BoxLayout(orientation='vertical') def get_questions(self, subject): print(subject) wb = openpyxl.load_workbook('sWL.xlsx') sheet = wb[subject] data = [] for row in range (2, sheet.max_row+1): question = sheet.cell(row=row, column=1).value data.append(question) return self.add_questions(data) def add_questions(self, data): self.layout_questions.clear_widgets() for question in data: question = Button(text= question, size=(480, 40), size_hint=(None, None)) self.layout_questions.add_widget(question) def build(self): layout_subjects = BoxLayout(orientation='vertical') subjects = ['Anatomy', 'Biochemistry', 'Physiology', 'Pharmacology', 'Pathology', 'Microbiology', 'FMT', 'Ophthalmology', 'ENT', 'PSM', 'OG', 'Surgery', 'Internal_Medicine', 'Paediatrics', 'Anaesthesia', 'Radiology', 'Dermatology', 'Orthopaedics', 'Psychiatry'] subjects_d = {} for subject in subjects: subjects_d[subject] = subject print(subjects_d) for k, v in subjects_d.items(): k = Button(text = v, size=(480, 40), size_hint=(None, None), on_press=lambda x: self.get_questions(k.text)) layout_subjects.add_widget(k) self.layout.add_widget(layout_subjects) self.layout.add_widget(self.layout_questions) root = ScrollView(size_hint=(1, 1), do_scroll_x=False) root.add_widget(self.layout) return root if __name__ == '__main__': AllApp().run()It works except that no matter which subject button i press, only questions from Psychiatry are added on the right, and i am not able to scroll despite me making ScrollView as the root. There still exists a problem with the on_press call. If i am not using lambda function, it crashes on a click and if i do, it calls only the last subject in the list. How do i fix this? Do i manually have to add each button? But that is not the pythonic way to do it...there must be a simpler way no? I was making a mistake with the use of lambda function. i was calling it as: lambda x: self.get_questions(subject)i fixed it by called it as: lambda subject: self.get_questions(subject)however, the scrolling still doesnt work. Kindly advice. RE: Need to render updated recycler view - test - Nov-25-2018 I changed the widget hierarchy a little bit hoping that will make it scroll...still no good.. here's the code: import kivy kivy.require('1.0.8') import openpyxl from kivy.app import App from kivy.uix.button import Button from kivy.uix.scrollview import ScrollView from kivy.uix.gridlayout import GridLayout from kivy.uix.boxlayout import BoxLayout class AllApp(App): layout = BoxLayout(orientation='horizontal') layout_questions = BoxLayout(orientation='vertical') def get_questions(self, subject): print(subject) wb = openpyxl.load_workbook('sWL.xlsx') sheet = wb[subject] data = [] for row in range (2, sheet.max_row+1): question = sheet.cell(row=row, column=1).value data.append(question) return self.add_questions(data) def add_questions(self, data): self.layout_questions.clear_widgets() for question in data: question = Button(text= question, size=(480, 40), size_hint=(None, None)) self.layout_questions.add_widget(question) def build(self): layout_subjects = BoxLayout(orientation='vertical') subjects = ['Anatomy', 'Biochemistry', 'Physiology', 'Pharmacology', 'Pathology', 'Microbiology', 'FMT', 'Ophthalmology', 'ENT', 'PSM', 'OG', 'Surgery', 'Internal_Medicine', 'Paediatrics', 'Anaesthesia', 'Radiology', 'Dermatology', 'Orthopaedics', 'Psychiatry'] subjects_d = {} for subject in subjects: subjects_d[subject] = subject print(subjects_d) for k, v in subjects_d.items(): k = Button(text = v, size=(480, 40), size_hint=(None, None)) k.bind(on_press= lambda k: self.get_questions(k.text)) layout_subjects.add_widget(k) root = BoxLayout() scroll_subject = ScrollView() scroll_question = ScrollView() root.add_widget(self.layout) self.layout.add_widget(scroll_subject) scroll_subject.add_widget(layout_subjects) self.layout.add_widget(scroll_question) scroll_question.add_widget(self.layout_questions) return root if __name__ == '__main__': AllApp().run() Fixed the scrolling problem... import kivy kivy.require('1.0.8') import openpyxl from kivy.app import App from kivy.uix.button import Button from kivy.uix.scrollview import ScrollView from kivy.uix.gridlayout import GridLayout from kivy.uix.boxlayout import BoxLayout from kivy.app import runTouchApp class AllApp(App): layout = BoxLayout(orientation='horizontal') layout_questions = GridLayout(cols=1, size_hint=(None, None), width=400, height=5500) def get_questions(self, subject): print(subject) wb = openpyxl.load_workbook('sWL.xlsx') sheet = wb[subject] data = [] for row in range (2, sheet.max_row+1): question = sheet.cell(row=row, column=1).value data.append(question) return self.add_questions(data) def add_questions(self, data): self.layout_questions.clear_widgets() for question in data: question = Button(text= question, size=(480, 40), size_hint=(1, None)) self.layout_questions.add_widget(question) def build(self): layout_subjects = GridLayout(cols=1, size_hint=(None, None), width=400, height=1000) subjects = ['Anatomy', 'Biochemistry', 'Physiology', 'Pharmacology', 'Pathology', 'Microbiology', 'FMT', 'Ophthalmology', 'ENT', 'PSM', 'OG', 'Surgery', 'Internal_Medicine', 'Paediatrics', 'Anaesthesia', 'Radiology', 'Dermatology', 'Orthopaedics', 'Psychiatry'] subjects_d = {} for subject in subjects: subjects_d[subject] = subject print(subjects_d) for k, v in subjects_d.items(): k = Button(text = v, size=(480, 40), size_hint=(1, None)) k.bind(on_press= lambda k: self.get_questions(k.text)) layout_subjects.add_widget(k) root = BoxLayout() scroll_subject = ScrollView(size_hint=(1, 1)) scroll_question = ScrollView(size_hint=(1, 1)) root.add_widget(self.layout) self.layout.add_widget(scroll_subject) scroll_subject.add_widget(layout_subjects) self.layout.add_widget(scroll_question) scroll_question.add_widget(self.layout_questions) # runTouchApp(scroll_subject) # runTouchApp(scroll_question) return root if __name__ == '__main__': AllApp().run()Apologies if I am being annoying. RE: Need to render updated recycler view - test - Nov-26-2018 Trouble again... all that code in python looked very cluttered, so i cleaned up the code rewriting most of the organization in kv. Now the program starts up and runs the first time and then i get this error: I removed all the Factory imports and made them static. But the basic layout consists of a carousel which i did not make static. So when i press a button, then go back to a previous slide and press another button, it crashes..kindly advice... |