I'm working in simple kivy app for android that play sound every minute, i want to make it work in background even when I close app
from kivy.app import App
from kivy.uix.button import Button
from kivy.core.audio import SoundLoader
import time
class MyApp(App):
def build(self):
self.sound = SoundLoader.load('1.wav')
start_but = Button(text='start',
on_press = self.click
)
return start_but
def click(self, obj):
while True:
self.sound.play()
time.sleep(60)
if __name__ == '__main__':
MyApp().run()
I tried to make the kivy window only restart after that i exit the window but i still can't find a way , any ideas ?
import sys
import kivy, os
from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock
class MyApp(App):
def build(self):
Clock.schedule_once(self.restart, 10)
return Label(text='Hello World One!')
def restart(self, dt):
os.execv(sys.executable, [sys.executable] + sys.argv)
if __name__ == '__main__':
MyApp().run()
I'm working on an app, and I need the images to display independently at a specific timing. I have set up a thread using python's stock threading module, it runs and works normally instead of the image it displays a black square. Does anyone know how to fix it?
Here is my code to reproduce the issue:
import threading
from kivy.app import App
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
class TestApp(App):
def build(self):
self.fl = FloatLayout()
self.fl.add_widget(Button(text="show image", on_press=self.start_thread))
return self.fl
def insertfunc(self):
self.fl.add_widget(Image(source="HeartIcon.png"))
def start_thread(self, instance):
threading.Thread(target=self.insertfunc).start()
TestApp().run()
Any help will be appreciated!
The add_widget() must be done on the main thread. I assume that you are using threading because you have additional things to do on the Thread aside from just the add_widget(). Based on that assumption, here is a modified version of your code that does what I think you want:
import threading
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
class TestApp(App):
def build(self):
self.fl = FloatLayout()
self.fl.add_widget(Button(text="show image", on_press=self.start_thread))
return self.fl
def insert_image(self, dt):
self.fl.add_widget(Image(source="HeartIcon.png"))
def insertfunc(self):
# do some calculations here
Clock.schedule_once(self.insert_image)
def start_thread(self, instance):
threading.Thread(target=self.insertfunc).start()
TestApp().run()
If you are not doing anything else in the new thread, then you don't actually need another thread. The start_thread() method can just do the:
self.fl.add_widget(Image(source="HeartIcon.png"))
import kivy
from kivy.app import App
from kivy.uix.label import Label
class Simplekivy(App):
def build(self):
return Label(text="salam")
if __name__ == "__main__":
Simplekivy().run()
this is what i get
"
[CRITICAL] [App ] Unable to get a Window, abort.
"
import kivy
from kivy.app import App
from kivy.uix.label import Label
class Simplekivy(App):
def build(self):
return Label(text="aleykum salam")
if __name__ == "__main__":
Simplekivy().run()
This code works; your installation/dependencies must be at fault.
Please give us more to see what may be the issue.
I would like to prevent the EXIT of a kivy App on Android with the ESCAPE ARROW.
When I write exit_on_escape = 0 in the config.ini file, it does not change anything (however on WINDOWS 8 with the Esc key this works).
I have also tried unsuccessfully to intercept the on_request_close.
import kivy
kivy.require('1.0.8')
from kivy.core.window import WindowBase
from kivy.uix.widget import Widget
from kivy.app import App
from kivy.clock import Clock
class Test(Widget):
def __init__(self, **kwargs):
super(Test,self).__init__(**kwargs)
class TestApp(App):
def build(self):
Clock.schedule_once(self.my_callback, 0)
return Test()
def my_callback(self,*dt):
print("mycallback")
win=self._app_window
win.fullscreen=1 #OK
win.bind(on_request_close=self.alwaystrue)
def alwaystrue(*largs, **kwargs) :
print("alwaystrue")#never happens ...
return True
if __name__ == '__main__':
TestApp().run()
Do something like this in your App class:
from kivy.app import App
from kivy.core.window import Window
class MyApp(App):
def build(self):
self.bind(on_start=self.post_build_init)
# do all your normal stuff as well
return whatever
def post_build_init(self,ev):
if platform() == 'android':
import android
android.map_key(android.KEYCODE_BACK, 1001)
win = Window
win.bind(on_keyboard=self.key_handler)
def key_handler(self, window, keycode1, keycode2, text, modifiers):
if keycode1 == 27 or keycode1 == 1001:
# Do whatever you want here - or nothing at all
# Returning True will eat the keypress
return True
return False
This is probably not all be necessary (using the intermediate post_build_init() method and the android.map_key() thing), but I originally got it from a mailing post and I don't think I have an updated version. Anyway, it works for me.
Add the following code snippet just below the import kivy statement
from kivy.config import Config
Config.set('kivy', 'exit_on_escape', '0')
Here is a working example, how to use on_request_close.
The line:
Config.set('kivy', 'exit_on_escape', '0')
disables the Esc -button.
from kivy.config import Config
Config.set('kivy', 'exit_on_escape', '0')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.core.window import Window
class ChildApp(App):
def build(self):
Window.bind(on_request_close=self.on_request_close)
return Label(text='Child')
def on_request_close(self, *args):
self.textpopup(title='Exit', text='Are you sure?')
return True
def textpopup(self, title='', text=''):
"""Open the pop-up with the name.
:param title: title of the pop-up to open
:type title: str
:param text: main text of the pop-up to open
:type text: str
:rtype: None
"""
box = BoxLayout(orientation='vertical')
box.add_widget(Label(text=text))
mybutton = Button(text='OK', size_hint=(1, 0.25))
box.add_widget(mybutton)
popup = Popup(title=title, content=box, size_hint=(None, None), size=(600, 300))
mybutton.bind(on_release=self.stop)
popup.open()
if __name__ == '__main__':
ChildApp().run()