Kivy Does Not Update The Refreshed Data on The Screen [Video + Full Codes]

23 views
Skip to first unread message

Mecra Yavcin

unread,
Feb 10, 2023, 9:59:56 AM2/10/23
to Kivy users support

Kivy does not update the refreshed data on the screen. If i restart the app, i can see the new data after calculation. I want to see refreshed data on the screen after the calculation is done, without restarting the program again.

When i run the app, datas() function pulls the json file at the first time and also when i on the second calculation screen, before calculation, the Clock.schedule_once(self.datas) pulls the data again as well but sill i can see the refreshed names on the screen.

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)      

    # Under Over 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')
        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                

data.json File

Please create a data.json file and PLEASE CHANGE THE LOCATION!!!!!!!!!!!!!!!!! in def datas(self, *args): with open ("C:\Users\Messi\Desktop\Python\Projects\Football Tips\Kivy\Testing Bugs\Test1\data.json", "r") as dosya:

{"home": ["Manchester City", "Arsenal"]}

Video Of The Problem

https://www.youtube.com/watch?v=mMwryGLQ5SQ

Thanks for your help


Elliot Garbus

unread,
Feb 10, 2023, 11:08:30 AM2/10/23
to kivy-...@googlegroups.com

In the __init__() you are assigning self.data = self.datas(), and then using self.data to display the info.  The var self.data is only getting updated when the app first runs.  You could make the changes as below.  See the highlighted areas.

 

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.metrics import dp
from kivy.clock import Clock
import json

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)

           
# Under Over 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)


   
# Calculation
   
def calculate(self, *args):
        data =
self.datas()

       
for i in 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('data.json') as dosya:
            dataApi = json.load(dosya)
           
print('datas updated')
       
return dataApi


--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/d6243cbc-dcd4-4254-bc4d-1b0710df3b6bn%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages