Python Forum
[Kivy] Need to render updated recycler view
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Kivy] Need to render updated recycler view
#11
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.
Reply
#12
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.
Reply
#13
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:
Error:
Traceback (most recent call last): File "/home/bunny/Desktop/App/new3.py", line 257, in <module> main_root().run() File "/usr/lib/python3/dist-packages/kivy/app.py", line 826, in run runTouchApp() File "/usr/lib/python3/dist-packages/kivy/base.py", line 502, in runTouchApp EventLoop.window.mainloop() File "/usr/lib/python3/dist-packages/kivy/core/window/window_sdl2.py", line 727, in mainloop self._mainloop() File "/usr/lib/python3/dist-packages/kivy/core/window/window_sdl2.py", line 460, in _mainloop EventLoop.idle() File "/usr/lib/python3/dist-packages/kivy/base.py", line 340, in idle self.dispatch_input() File "/usr/lib/python3/dist-packages/kivy/base.py", line 325, in dispatch_input post_dispatch_input(*pop(0)) File "/usr/lib/python3/dist-packages/kivy/base.py", line 291, in post_dispatch_input wid.dispatch('on_touch_up', me) File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch File "/usr/lib/python3/dist-packages/kivy/uix/scrollview.py", line 848, in on_touch_up if self.dispatch('on_scroll_stop', touch): File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch File "/usr/lib/python3/dist-packages/kivy/uix/scrollview.py", line 887, in on_scroll_stop self.simulate_touch_down(touch) File "/usr/lib/python3/dist-packages/kivy/uix/scrollview.py", line 607, in simulate_touch_down ret = super(ScrollView, self).on_touch_down(touch) File "/usr/lib/python3/dist-packages/kivy/uix/widget.py", line 460, in on_touch_down if child.dispatch('on_touch_down', touch): File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch File "/usr/lib/python3/dist-packages/kivy/uix/widget.py", line 460, in on_touch_down if child.dispatch('on_touch_down', touch): File "kivy/_event.pyx", line 707, in kivy._event.EventDispatcher.dispatch File "/usr/lib/python3/dist-packages/kivy/uix/behaviors/button.py", line 151, in on_touch_down self.dispatch('on_press') File "kivy/_event.pyx", line 703, in kivy._event.EventDispatcher.dispatch File "kivy/_event.pyx", line 1214, in kivy._event.EventObservers.dispatch File "kivy/_event.pyx", line 1138, in kivy._event.EventObservers._dispatch File "/home/bunny/Desktop/App/new3.py", line 227, in <lambda> question = Button(text= question, size=(480, 40), size_hint=(1, None), on_press= lambda question: self.question_being_read(subject, question)) File "/home/bunny/Desktop/App/new3.py", line 168, in question_being_read root.ids.multi_button.on_press = lambda : self.clock_caller(subject, row, start=True) File "kivy/properties.pyx", line 838, in kivy.properties.ObservableDict.__getattr__ File "kivy/properties.pyx", line 832, in kivy.properties.ObservableDict._weak_return File "kivy/weakproxy.pyx", line 40, in kivy.weakproxy.WeakProxy.__class__.__get__ File "kivy/weakproxy.pyx", line 26, in kivy.weakproxy.WeakProxy.__ref__ ReferenceError: weakly-referenced object no longer exists
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...
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020