0

I have an example code and it works nice. But in this code Scrollview and Gridlayout are created in KV File. I have a scroll bug problem so i want to create them in pure python.

There are 350 boxlayouts and they are created in Scrollview and each boxlayout has 5 widgets (buttons label etc) So this causes some bugs in Scrollview when clearing screen and recreating results. There are no problem for 80 boxlayout, but they are created dynamically and some times there are 300+ boxlayouts in Scrollview.

I thought if i can create Scrollview and Gridlayout, i can fix this issue. So i need your help.

How can i do that. Test code:

PY File:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.metrics import dp
from kivy.uix.behaviors import ButtonBehavior
from kivy.clock import Clock, mainthread
import json
import threading


class Test(BoxLayout):

    def __init__(self, **kwargs):
        super(Test, self).__init__(**kwargs)
 
        self.data = self.datas()

    # Homepage Screen
    def homepage(self, screenmanager):        
        
        screenmanager.current = 'homepage_screen'
        Clock.schedule_once(self.clear_widgets)

    # Clear Widgets
    def clear_widgets(self, *args):

        for child in [child for child in self.ids.gridsonuc.children]:
            self.ids.gridsonuc.remove_widget(child)      

    #Second screen
    def second(self,screenmanager):
        
        screenmanager.current = 'second_screen'

        Clock.schedule_once(self.clear_widgets)
        Clock.schedule_once(self.datas) # Before calculation, each time app pulls data again, but Kivy Does Not Update The Refreshed Data in The Screen!    
        Clock.schedule_once(self.calculate)

        # or, if i can use threading system as well but this time i must add @mainthread above def calculate(self, *args): to make code work.
        # in both scenario, Kivy Does Not Update The Refreshed Data in The Screen While APP is Running.

        # mythread1 = threading.Thread(target=self.clear_widgets) 
        # mythread1.start()
        # mythread2 = threading.Thread(target=self.datas) 
        # mythread2.start()
        # mythread3 = threading.Thread(target=self.calculate) 
        # mythread3.start()

    # Calculation
    #@mainthread
    def calculate(self, *args):        

        
        for i in self.data['home']:           

            box = BoxLayout(size_hint_y = None, height = dp(50))
            hometeams = Label(text = f'{[i]}', font_name = 'Roboto', font_size = dp(15), size_hint = (0.225, 1), halign='center', bold = True )
            box.add_widget(hometeams)
            self.ids.gridsonuc.add_widget(box)

    def datas(self, *args):           
        
        # PLEASE CHANGE THE LOCATION!!!!!!!!!!!!!!!!!
        with open ("C:\\Users\\Messi\\Desktop\\Python\\Projects\\Football Tips\\Kivy\\Testing Bugs\\Test1\\data.json", "r") as dosya:
        
            dataApi = json.load(dosya)      
            print('datas updated')
            self.data = dataApi  # update the self.data
        return dataApi     
  
class TestApp(App):
    def build(self): 
        return Test()

if __name__ == '__main__':
    TestApp().run()

KV File:

#:import NoTransition kivy.uix.screenmanager.NoTransition
<Test>:
    ScreenManager:
        transition: NoTransition()
        id: sm
        size: root.width, root.height
        Screen:
            name: 'homepage_screen'            
            BoxLayout:
                size_hint: 1, 0.10
                Button:
                    text: 'Calculate'
                    id: underOver_button_homepage
                    on_press: root.second(sm)     
                    background_color: 0, 0, 0, 0                                  
        Screen:
            name: 'second_screen'
            BoxLayout:
                spacing: '20dp'
                orientation: 'vertical'    
                BoxLayout:
                    size_hint: 1, 0.80
                    ScrollView:
                        scroll_type: ['bars', 'content']
                        bar_margin: '5dp'
                        bar_color: 1, 0.4, 0.769, 1 
                        bar_width: '5dp'
                        bar_inactive_color: 1, 0.4, 0.769, 1
                        GridLayout:                            
                            id: gridsonuc
                            cols: 1
                            spacing: '50dp'
                            size_hint_y: None
                            height: self.minimum_height        
                BoxLayout:
                    size_hint: 1, 0.10
                    Button:
                        text: 'Home'
                        id: home_button_underOver
                        on_press: root.homepage(sm)
                        background_color: 0, 0, 0, 0

For example:

I don't want this structure in KV file

BoxLayout:
    size_hint: 1, 0.80
    ScrollView:
        scroll_type: ['bars', 'content']
        bar_margin: '5dp'
        bar_color: 1, 0.4, 0.769, 1 
        bar_width: '5dp'
        bar_inactive_color: 1, 0.4, 0.769, 1
        GridLayout:                            
            id: gridsonuc
            cols: 1
            spacing: '50dp'
            size_hint_y: None
            height: self.minimum_height

My Goal:

BoxLayout:
    size_hint: 1, 0.80
    Newscroll: # New Scrollview Class and have also Gridlayout in it

GridLayout and Scrollview must have same settings and i want to use like below. I think scroll_type: ['bars', 'content'], bar_margin: '5dp', bar_color: 1, 0.4, 0.769, 1, bar_width: '5dp', bar_inactive_color: 1, 0.4, 0.769, 1, cols: 1, spacing: '50dp', size_hint_y: None, height: self.minimum_height should code in Python

And a new clear def() for cleaning full scrollview class, when clear and create Scrollview in Pure Python, there are created dynamically so I can fix my Scroll bug issue.

Thanks for your help. I hope one day I can be at a level where I can help other people in Kivy.

Claudio
  • 7,474
  • 3
  • 18
  • 48

1 Answers1

0

Kivy: How can I recode Scrollview and Gridlayout in pure Python?

The shortest answer to your question is:

You can't.


See Pure python gui library? here on stackoverflow to read:

Notion of "pure python gui library" is wrong because ultimately you will be using system level calls and widgets, may be thru ctypes but that doesn't change the fact that if you start implementing your idea you will eventually end with using wxPython or some other module.

along with the statement in the accepted answer:

The path of least effort and best results would be to learn what it takes to deploy an app using some of the existing GUI libraries.

Kivy is a Python module you can use to create GUIs (Graphical User Interfaces) and comes with the possibility to create a Scrollview and a Gridlayout objects/widgets for by it created window with an user interface.

Your goal is to use "pure Python" to replace the objects/widgets, but it is not possible to use "pure Python" for this task, because Python requires usage of a module for GUIs.

Most native GUI module that comes with the standard Python installation and is nearest to the notion of "pure Python" is tkinter, but it needs GTK to be already installed on the system, else it won't be available and you need to learn how to use it properly like you need it when using Kivy.

In other words it will be a good idea to find out what actually causes the problems you experience while using Kivy and solve them instead of learning how to use another GUI library to recreate your application.

And if your idea is to create own widgets within Kivy you have to learn how to write own extensions to Kivy which can take you much more effort compared to finding out the reason for why you face problems while using it.

Claudio
  • 7,474
  • 3
  • 18
  • 48