2

I am working on the project using kivy gui in python but it's performance is very slow,so is it possible to convert that kivy code in cython for getting better performance as in resposne time of an application. If it is possible so how I can do it. if no so what I will do for getting immediately response from the application.

The Source Code is:

from kivymd.app import MDApp
from kivymd.uix.card import MDCard
from kivymd.uix.selectioncontrol import MDCheckbox
from kivymd.uix.textfield import MDTextField
from kivymd.uix.label import MDLabel
from kivymd.uix.boxlayout import MDBoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.metrics import dp
from kivymd.uix.fitimage import FitImage
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.behaviors import RoundedRectangularElevationBehavior
class my_mdcard(MDCard,RoundedRectangularElevationBehavior):
    pass

class Item_Menu(MDBoxLayout):
    list_of_information={"checkbox":[],"labels":[],"quantity":[]}
    selected_items= {"checkbox": [], "labels": [], "quantity": []}
    def __init__(self,**kwargs):
        super(Item_Menu,self).__init__(**kwargs)
        self.size_hint=(1,0.9)
        self.pos_hint={"top":1}
        self.orientation="vertical"
        scrollbarwin=ScrollView()
        content_box=BoxLayout(orientation='vertical',padding=dp(8),spacing=dp(8),size_hint=(1,None))
        content_box.bind(minimum_height=content_box.setter('height'))
        for i in range(0,50):
            Template_card= my_mdcard(
                size_hint_y=None,
                size_hint_x=.960,
                height = dp(100),
                padding = dp(4),
                pos_hint={'center_y': .5, 'center_x': .490},
                radius = [20,],
                elevation = 4,

            )
            checkbox=MDCheckbox(
                size_hint=(None, None),
                size= (dp(48),dp(48)),
                pos_hint={'center_y': .5}
            )
            checkbox.bind(active=lambda instance,value:self.Selected_checkbox(instance,value))
            self.list_of_information["checkbox"].append(checkbox)
            image_box=MDBoxLayout(adaptive_size=True)
            image=FitImage(
                source="D:/Study/Python/Kivy/images/2.jpg",
                size_hint= (None, None),
                height=dp(80),
                width=dp(130),
                radius=[12,],
                pos_hint={'center_y':0.5}

            )
            image_box.add_widget(image)
            text_box=MDBoxLayout(orientation="vertical",adaptive_height=True,pos_hint={'center_y':0.5},padding=[12,0,0,0])
            item_name=MDLabel(text=f"item{i+1}",font_style="H5",size_hint=(1,None),bold=True,theme_text_color="Primary")
            item_name.bind(texture_size=item_name.setter('size'))
            self.list_of_information["labels"].append(item_name)
            price=MDLabel(text=u"Price: \u20B910/per",font_style="Subtitle1",size_hint=(1,None),bold=True,theme_text_color="Hint")
            price.bind(texture_size=price.setter('size'))
            quantitybox=MDBoxLayout(orientation='vertical',adaptive_height=True,size_hint_x=0.2,pos_hint = {'center_y': .5,'center_x':0.5})
            quantityfield=MDTextField(
                hint_text= "Quantity",
                mode= "rectangle",
                size_hint=(None,None),
                width=dp(80),
                height= dp(40),
                padding=[0,0,15,0]
            )
            self.list_of_information["quantity"].append(quantityfield)
            quantitybox.add_widget(quantityfield)
            Template_card.add_widget(checkbox)
            Template_card.add_widget(image_box)
            Template_card.add_widget(text_box)
            text_box.add_widget(item_name)
            text_box.add_widget(price)
            Template_card.add_widget(quantitybox)
            content_box.add_widget(Template_card)
        scrollbarwin.add_widget(content_box)
        self.add_widget(scrollbarwin)    
class MyApp(MDApp):

    def build(self):
        self.theme_cls.theme_style="Dark"
        return Item_Menu()


MyApp().run()

In this code the for loop taking a lot of time to execute its code when the range is 50 only. if the range is 1000 or greater than it. so it would take a lot of time to execute its code for 1000 itreation. so how i can convert this for loop into in cython for loop so just it takes minimum time as possible as it can. and it run fast. so please tell me that what is the solution for it. Thank You!!

  • Did you try with `RecycleView` ? – ApuCoder May 11 '22 at 07:25
  • 2
    Cython only speed up code that would be slow due to the CPython interpreter. If kivy is implemented in C, then it will not be faster. Beside this, Cython mainly help when you can use low-level native types which appear not to be really possible here. That being said the main problem is why you want to iterate 50_000 times on a code creating few widget? GUI operations are generally slow. I barely see any use-case where the user want having like 150_000 widgets printed on its screen! – Jérôme Richard May 12 '22 at 22:11

0 Answers0