my kivy simple hello world app is not closing I'm using raspberry pi B and I can't close it I must unplug my raspberry pi 5v adapter to close it
I'm using rasbian jessie
this is the very simple code
import kivy
from kivy.app import App
from kivy.uix.label import Label
class mamdouh(App):
def build(self):
return Label(text='mamdouh')
if __name__=='__main__':
mamdouh().run()
What I was trying to say in the comment was. That you need to have some action to cause the quit, as the run method will run in a loop indefinitely otherwise.
If you now click on the Button labeled 'paul' it will quit.
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
class mamdouh(App):
def build(self):
lbl = Label(text='paul')
btn = Button(text='mamdouh')
btn.bind(on_press=lambda b: app.stop())
lbl.add_widget(btn)
return lbl
if __name__ == '__main__':
app = mamdouh()
app.run()
I know nothing about kivy but I can see that this should allow you to quit, whether you want a Button in your app is another question.
Another way to kill it and avoid the reboot is simply to go back to the prompt where you launched your app and do CTRL + C. This will only work from the prompt though, not from the app window itself.
Related
I have a simple python kivy source code (only a example) and I execute the code in virtuelenv. The "program" runs without errors.
But unfortunately I see not the open window - only the Python Icon on the bottom from my task bar.
What I have to do that the windows opens?
# Datei: hello.py
import kivy
kivy.require('1.9.0') # Mindest-Version von Kivy
from kivy.app import App
from kivy.uix.button import Button
class HelloApp(App):
def build(self):
return Button(text='Hallo Welt!')
if __name__== "__main__":
HelloApp().run()
image
I have problem with disabling of buttons in kivy library. When I disable button, it simply not disable. It waits in some strange way.
Let me show you my code:
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
import time
class MainApp(App):
def build(self):
self.l = FloatLayout()
b = Button(text="BUTTON", pos_hint={"top":0.8, "right": 0.8}, size_hint=(0.6, 0.6))
b.bind(on_press=self.press)
self.l.add_widget(b)
return self.l
def press(self, btn):
btn.disabled = True
time.sleep(3.0)
btn.disabled = False
app = MainApp()
app.run()
When I press button, I want to disable it for 3 sec. But instead of it program "freeze" (without disabling of button), and then after 3 secs do animation of press (button blinks with blue color). Of cource program must "freeze" because of time.sleep(3.0), but after disabling of button (Which must be gray, but it dont change color...)
How to solve it? If I put there instead time.sleep() something like for cycle (with about 10 milions of cycle) to imitate of "doing something" by program, it behaves in the same way...
So how I can solve it? How to disable button in kivy, then do something and after it is done enable button again?
Thanks!
EDIT: My problem isn't, that program freezes for 3 seconds. I understand that calling time.sleep() is blocking. What I don't understand is why button is not disabled before (and during) sleep...
The time.sleep is blocking the code. Instead you need to use Clock to enable the button after 3 seconds. Below is the corrected code to achieve your target:
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.clock import Clock
from functools import partial
class MainApp(App):
def build(self):
self.l = FloatLayout()
b = Button(text="BUTTON", pos_hint={"top":0.8, "right": 0.8}, size_hint=(0.6, 0.6))
b.bind(on_press=self.press)
self.l.add_widget(b)
return self.l
def press(self, btn):
btn.disabled = True
Clock.schedule_once(partial(self.btn_enable, btn), 3)
def btn_enable(self, btn, *args):
btn.disabled = False
app = MainApp()
app.run()
TL; DR
The animation happens after the press function is called. This means that you freeze the program when doing time.sleep.
What to do about it?
Instead, you need to do something non-blocking, meaning that it runs in three seconds, but it doesn't cause the program to freeze. Something that would probably work is to utilize threads (something similar to the example, but dealing with sending variables across threads).
Example
Here is an example for your code that does not work, so you can understand the gist of it. Most likely, you are going have to deal with passing variables across threads:
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
# import time
import threading
class MainApp(App):
def build(self):
self.l = FloatLayout()
b = Button(text="BUTTON", pos_hint={"top":0.8, "right": 0.8}, size_hint=(0.6, 0.6))
b.bind(on_press=self.press)
self.l.add_widget(b)
return self.l
def press(self, btn):
btn.disabled = True
# time.sleep(3.0)
threading.Timer(3.0, lambda: btn.disabled = False).start()
app = MainApp()
app.run()
This was inspired by this answer.
I'm planning to use kivy to develop an app related to one of my project. But still I couldn't find a way to make the app run in background and while a certain condition is satisfied I need it to display a push notification in mobile. How can I add push notification feature in kivy? Please let me know if someone know how to make it work. (Note - Even the app is closed in mobile it should run in background..).
I can't tell you how to keep running in the background (because I don't know either) but I can show you how to notify.
by the way,plyer doesn't work as well as windows on android so you should throw with java lib jnius
codes is here:
from kivy.app import App
from kivy.uix.button import Button
from jnius import autoclass
def notify(*args):
AndroidString = autoclass('java.lang.String')
PythonActivity = autoclass('org.kivy.android.PythonActivity')
NotificationBuilder = autoclass('android.app.Notification$Builder')
Context = autoclass('android.content.Context')
Drawable = autoclass('org.test.notify.R$drawable')
icon = Drawable.icon
notification_builder = NotificationBuilder(PythonActivity.mActivity)
notification_builder.setContentTitle(AndroidString('Title'.encode('utf-8')))
notification_builder.setContentText(AndroidString('Message'.encode('utf-8')))
notification_builder.setSmallIcon(icon)
notification_builder.setAutoCancel(True)
notification_service = notification_service = PythonActivity.mActivity.getSystemService(Context.NOTIFICATION_SERVICE)
notification_service.notify(0,notification_builder.build())
class NotifyApp(App):
def build(self):
return Button(text="notify", on_press=notify)
if __name__ == '__main__':
NotifyApp().run()
if you keep getting errors,look here
I would like to run a Method when the user tries to exit the app , kind of like a "are you sure you want to exit" or "Do you want to save the file" type of message whenever the user tries to exit by clicking the Exit button on top of the window
Some thing like
on_quit: app.root.saveSession()
If you want your application to simply run things after the GUI has closed, the easiest and smallest approach would be to place any exit code after TestApp().run(). run() creates a endless loop which also clears any event-data from within kivy so it doesn't hang. That endless loop breaks as soon as the window/gui instance dies. So there for, any code after will execute only after the GUI dies too.
If you want to create a graceful shutdown of the GUI with for instance socket-closing events or a popup asking the user if that's what they really want to do, then creating a hook for the on_request_close event is the way to go:
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()
Courtesy of pythonic64 who created a gist on the topic in a issue way back when.
Is there an option to create keyboard shortcut Ex.- Ctrl+Q to exit the application / close the window of a desktop application written in Kivy and Python?
I am on Windows OS.
Thank you in advance.
Arnab
Kivy's Window.on_keyboard (doc) event allows you to catch keyboard key pressing event.
Example app that exits if press ctrl+q:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.core.window import Window
class RootWidget(BoxLayout):
pass
class TestApp(App):
def build(self):
Window.bind(on_keyboard=self.on_keyboard) # bind our handler
return RootWidget()
def on_keyboard(self, window, key, scancode, codepoint, modifier):
if modifier == ['ctrl'] and codepoint == 'q':
self.stop()
if __name__ == '__main__':
TestApp().run()