how to access main loop of kivy

551 views
Skip to first unread message

Gaurav Bhoi

unread,
May 17, 2021, 1:17:52 PM5/17/21
to Kivy users support
Hello guys, while asking this question I m assuming main loop is same loop like other loops nothing fancy. I want to add some piece of code to run it forever. How shall I do it . I m not adding more loops, I will make my app slow. please help thank you

Elliot Garbus

unread,
May 17, 2021, 2:13:17 PM5/17/21
to kivy-...@googlegroups.com

I would suggest that you use Clock.schedule_interval() to run this code, and run it at the longest interval possible.

 

If you can be more descriptive about what you want to do, I might have a different suggestion…  threading, multiprocessing..

--
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/964251d0-0826-436b-b01e-33c4e0d5e764n%40googlegroups.com.

 

Gaurav Bhoi

unread,
May 17, 2021, 3:25:47 PM5/17/21
to kivy-...@googlegroups.com
Hello Elliot, I m pasting a code down below where i tried threading. i m printing 'testing' in console. It works but while exiting app app crashes. please help

python code: 

import sys
from kivy.app import App
import threading
from kivy.uix.boxlayout import BoxLayout

class Box(BoxLayout):
pass

def func():
while True:
print('testing')

t1 = threading.Thread(target=func)
t1.start()

class Test(App):
def build(self):
return Box()

def on_stop(self):
sys.exit()
if __name__ == '__main__':
Test().run()
crash.png

You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/WPvGGCmtB_o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/60a2b239.1c69fb81.e3ef6.5b73SMTPIN_ADDED_MISSING%40gmr-mx.google.com.

Elliot Garbus

unread,
May 17, 2021, 5:07:02 PM5/17/21
to kivy-...@googlegroups.com

You want to be able to shutdown the thread at on_stop().  I created a class ThreadFunc that has an attribute run that can be used to stop the loop in func.

The method go creates and starts the thread.  The method stop() stops the loop, and waits for the thread to shut down and join the main thread of execution.

This will allow a clean close.

 

 

 

import sys
from kivy.app import App
import threading
from kivy.uix.boxlayout import BoxLayout

class Box(BoxLayout):
   
pass

class
ThreadFunc:
   
def __init__(self):
       
self.run = True
       
self.t1 = None

    def
func(self):
       
while self.run:
           
print('testing')

   
def go(self):
       
self.t1 = threading.Thread(target=self.func)
       
self.t1.start()

   
def stop(self):
       
self.run = False
       
self.t1.join()

class Test(App):
   
def build(self):
       
self.tf = ThreadFunc()
       
self.tf.go()
       
return Box()

   
def on_stop(self):
       
self.tf.stop()

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

 

 

 

 

From: Gaurav Bhoi
Sent: Monday, May 17, 2021 12:26 PM
To: kivy-...@googlegroups.com
Subject: Re: [kivy-users] how to access main loop of kivy

 

Hello Elliot, I m pasting a code down below where i tried threading. i m printing 'testing' in console. It works but while exiting app app crashes. please help

 

python code: 

 

import sys
from kivy.app import App
import threading
from kivy.uix.boxlayout import BoxLayout

class Box(BoxLayout):
   
pass

def
func():
   
while True:
       
print('testing')

t1 = threading.Thread(
target=func)
t1.start()

class Test(App):
   
def build(self):
       
return Box()

   
def on_stop(self):
        sys.exit()
if __name__ == '__main__':
    Test().run()

 

On Mon, May 17, 2021 at 11:43 PM Elliot Garbus <elli...@cox.net> wrote:

I would suggest that you use Clock.schedule_interval() to run this code, and run it at the longest interval possible.

 

If you can be more descriptive about what you want to do, I might have a different suggestion…  threading, multiprocessing..

 

From: Gaurav Bhoi
Sent: Monday, May 17, 2021 10:18 AM
To: Kivy users support
Subject: [kivy-users] how to access main loop of kivy

 

Hello guys, while asking this question I m assuming main loop is same loop like other loops nothing fancy. I want to add some piece of code to run it forever. How shall I do it . I m not adding more loops, I will make my app slow. please help thank you

--
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/964251d0-0826-436b-b01e-33c4e0d5e764n%40googlegroups.com.

 

--
You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/WPvGGCmtB_o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/60a2b239.1c69fb81.e3ef6.5b73SMTPIN_ADDED_MISSING%40gmr-mx.google.com.

--
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.

crash.png

Elliot Garbus

unread,
May 17, 2021, 5:17:53 PM5/17/21
to kivy-...@googlegroups.com

Here is the code using Clock.schedule_interval.

 

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock

class Box(BoxLayout):
   
pass

def
func(dt):
   
print('testing')

class Test(App):
   
def build(self):
        Clock.schedule_interval(func,
.1)
       
return Box()

Reply all
Reply to author
Forward
0 new messages