Function "on_complete" called before "completed" on kivy app with plyer - android

It's me again!
Well, that's really strange.
I'using kivy for make an App for Android.
I can use the camera, but or the app resets or do something strange.
Here is the problem:
def chamar_camera(nome,pc,objeto,label_passa,instance):
agora = datetime.now()
nome_arquivo = '%s_%s_%.4i_%.2i_%.2i_%.2i_%.2i_%.2i.jpg' % (nome,pc,agora.year,agora.month,agora.day,agora.hour,agora.minute,agora.second)
# Option 1 - These two lines work:
#def sair():print 'oi'
#camera.take_picture(nome_arquivo, sair)
# Option 2 - These two lines work too:
def sair(label_passa,nome_arquivo):print 'oi'
camera.take_picture(nome_arquivo,on_complete=sair(label_passa,nome_arquivo))
# Option 3 - But these don't:
#def sair(label_passa,nome_arquivo):label_passa.text = nome_arquivo
#camera.take_picture(nome_arquivo, on_complete=sair(label_passa,nome_arquivo))
def on_pause(self):return True
def on_resume(self):pass
On option 3, I write a text (nome_arquivo) on a label widget (label_passa), but what happens is that the text is wrote before the camera be activated. So the camera appear, I can take a picture and the App restarts. I also tried just a "def sair(): pass", but this doesn't work. The only thing working is an "print", but on my app I need to write something in that label and update an sqlite database. Any idea why the function is being called before the camera action?
Thanks!

on_pause and on_resume should be defined as methods of your App class, not (as you have here) functions defined locally within the chamar_camera function.

Related

How to disable multi-touch function for Android App

So I have created Mobile App called PushApp using Python library Kivy. App lets user input number of how many pushups he wants to do. Then its supposed to decrement the number by one when chest hits screen(button). If screen is touched by multiple body parts it decrements many numbers. I would like to know how to disable multitouch function for Android phones at least.
Regards,
Highzivs
I have managed to solve this problem by creating function (BUTTON_DOWN()) that returns how many times button has been pushed down and function (BUTTON_UP) that returns button release times.
Then I created function (PUSH_UP) which returns true if BUTTON_DOWN()==BUTTON_UP()
in Python:
class CountScreen(Screen):
DOWN_COUNT=0
UP_COUNT=0
PUSH_UP_COUNT=0
def BUTTON_DOWN(self):
self.DOWN_COUNT=self.DOWN_COUNT+1
return self.DOWN_COUNT
def BUTTON_UP(self):
self.UP_COUNT=self.UP_COUNT+1
return self.UP_COUNT
def PUSH_UP(self):
if self.BUTTON_UP()==self.BUTTON_DOWN():
return True
in Kivy:
<CountScreen>:
name:"CountingWindow"
GridLayout:
cols:1
Label:
id:lbl_1
font_size:400
text:""
Button:
id:"btn_1"
background_color:(0,0,0,.1)
on_press:
root.BUTTON_DOWN()
on_release:
root.BUTTON_UP()
if root.PUSH_UP():lbl_1.text=str(int(lbl_1.text)-1) #DECREMENTS 1 FROM USER INPUT
if lbl_1.text == '0':app.root.current="FinalScreen" #GO TO FINAL WINDOW WHEN FINISHED

Android Fotoapparat library

I'm new to Android-Developement and I would like to make a Camera-app. I found this library (this is the Github page).
But I don't know how to implement a library. I followed these steps (method 2) but I'm getting an error in a popup window called 'IDE Fatal Errors'. It says: 'To investigate / fix the problem IDE wants to attach following files to the bug report. We recommend to include all the files providing maximum information. Note: all the data you send will be kept private.' Then I can select a 'diagnostic.txt'. There is a section 'file content' where 'rootsChanged' is written. I can report the whole window to Google.
The following step is to configure the 'Fotoapparat' instance. What is an instance? When I search on Google I only find articles talking about making a library.
I'm sorry if these are stupid question but I am a beginner and I would like to learn more about Android-Development. Thanks in advance for your time and help.
Add this line in your build.gradle(Module: app) file ->
dependecies {
//Your other dependencies...
implementation 'io.fotoapparat:fotoapparat:2.3.3'
}
And start using your code. Library is working fine.
EDIT - >
You need to learn basics of java.
To setup instance of the object you need to create a variable.
Hence in your case:
Fotoapparat yourVariableName = new FotoapparatFotoapparat
.with(context)
.into(cameraView) // view which will draw the camera preview
.previewScaleType(ScaleType.CenterCrop) // we want the preview to fill the view
.photoResolution(ResolutionSelectorsKt.highestResolution()) // we want to have the biggest photo possible
.lensPosition(LensPositionSelectorsKt.back()) // we want back camera
.focusMode(SelectorsKt.firstAvailable( // (optional) use the first focus mode which is supported by device
FocusModeSelectorsKt. continuousFocusPicture(),
FocusModeSelectorsKt.autoFocus(), // in case if continuous focus is not available on device, auto focus will be used
FocusModeSelectorsKt.fixed() // if even auto focus is not available - fixed focus mode will be used
))
.flash(SelectorsKt.firstAvailable( // (optional) similar to how it is done for focus mode, this time for flash
FlashSelectorsKt.autoRedEye(),
FlashSelectorsKt.autoFlash(),
FlashSelectorsKt.torch()
))
.frameProcessor(myFrameProcessor) // (optional) receives each frame from preview stream
.logger(LoggersKt.loggers( // (optional) we want to log camera events in 2 places at once
LoggersKt.logcat(), // ... in logcat
LoggersKt.fileLogger(this) // ... and to file
))
.build();
And start using yourVariableName.

CORONA SDK: Create a back to main button stopping the already started scenes.

im developing an APP in corona SDK, it's a serie of tests, so i made every test separately and then i created a main Scene that contains this tests. To handle any click error or misunderstanding i created a "back to intro" button, the problem is, when i start a test (for example test2) i'm in the middle of the test and i use the back to intro button (that takes me to intro, there's no prob with that) the Test2 keeps going and when i try to re-take the test shows an error related to this. I've trying different ways but i can't make it work, here is the code of the button:
local function handleButtonEvent( event )
if ( "ended" == event.phase ) then
storyboard.showOverlay( "cerrar sesion", options )
end
end
local button1 = widget.newButton
{
width = 60,
height = 60,
defaultFile = "images/boton_config.png",
overFile = "images/boton_config.png",
label = "",
onEvent = handleButtonEvent
}
-- Center the button
button1.x = display.contentCenterX+550
button1.y = 35
button1.isVisible=true;
storyboard.gotoScene("test0intro")
Plz help :<
I think I may have had this problem. I would advise switching to composer rather than storyboard a lot of these issues are easier to find. I am unsure about storyboard but for composer I know that I had to remove all event listeners, remove the sceneGroup, stop the physics and then remove the scene in the scene:hide method. I'm unsure of what the equivalent of this is in storyboard. Otherwise when I went to restart to the game (as you seem to be doing), the game would crash. Hope that helps!

Android and PyQt application

I wrote an application using PyQt for Windwos/Linux and I want to port it to Android. Application is simple, has 2 levels of GUI and I do not want to implement touch functions, but to control application using USB keyboard.
I am using this project:
android-python27
to interpret my PyQt code for Android, but it doesn't work. I suppose that I have to modify code, but I do not know how. I only found one sample for using android-python27:
import android, time
droid = android.Android()
while 1:
droid.makeToast("Hello from Python 2.7 for Android")
time.sleep(5)
My application is being started in this way:
app = QtGui.QApplication(sys.argv)
app.Encoding(QtGui.QApplication.UnicodeUTF8)
ui = MainMenu()
class MainMenu(QtGui.QWidget):#glavni izbornik
def __init__(self):
super(MainMenu, self).__init__()
i=0
self.UI(self)
def UI(self, Form):
...
What should I change to adopt code to android-python27?

back button functionality on android with kivy

in Kivy, when I press the back button on my android device it throws me out of the application. is there a way to return back to the previous screen using the Kivy language and not python? this is what I have written in kivy:
<MyAppClass>:
AnchorLayout:
anchor_x : 'center'
anchor_y : 'top'
ScreenManager:
size_hint : 1, .9
id: _screen_manager
Screen:
name:'screen1'
StackLayout:
# irrelevant code
Screen:
name:'screen2'
StackLayout:
# irrelevant code
I need to manipulate the screen manager and its screens from python... if I can do so I will be ok with python.
Kivy on android binds the back button to the esc button so binding and listening to esc button in your app would help you handle how your app behaves when the back button is pressed.
In other words in your app when testing it on your desktop listen to the escape key from the system keyboard, this will be automatically be translated to being the back button on your android device. Something like::
def on_start():
from kivy.base import EventLoop
EventLoop.window.bind(on_keyboard=self.hook_keyboard)
def hook_keyboard(self, window, key, *largs):
if key == 27:
# do what you want, return True for stopping the propagation
return True
i guess that i have solved it but should thank both #inclement and #qua-non! your answers guys led me to the right way! so in kv i assume that i gave an id to my screen manager (please refer to my question where i have written the kv code) , in python i should do the following:
from kivy.core.window import Window
from kivy.properties import ObjectProperty
class MyAppClass(FloatLayout):#its a FloatLayout in my case
_screen_manager=ObjectProperty(None)
def __init__(self,**kwargs):
super(MyAppClass,self).__init__(**kwargs)
#code goes here and add:
Window.bind(on_keyboard=self.Android_back_click)
def Android_back_click(self,window,key,*largs):
if key == 27:
self._scree_manager.current='screen1'#you can create a method here to cache in a list the number of screens and then pop the last visited screen.
return True
class MyApp(App):
def build(self):
return MyAppClass()
if __name__=='__main__':
MyApp().run()
This is certainly possible. Here's a short example app with the method I use to do this:
from kivy.utils import platform
from kivy.core.window import Window
class ExampleApp(App):
manager = ObjectProperty()
def build(self):
sm = MyScreenManager()
self.manager = sm
self.bind(on_start=self.post_build_init)
return sm
def post_build_init(self, *args):
if platform() == 'android':
import android
android.map_key(android.KEYCODE_BACK, 1001)
win = Window
win.bind(on_keyboard=self.my_key_handler)
def my_key_handler(self, window, keycode1, keycode2, text, modifiers):
if keycode1 in [27, 1001]:
self.manager.go_back()
return True
return False
This should give the right basic idea, but a few notes:
ScreenManager doesn't keep track of the previous screens, it's up to you to implement this how you like. My example assumes you defined a class MyScreenManager with a go_back method.
It might not be necessary to bind to on_start and run post_build_init, this is just how the example I originally used did it (see below). It might be important sometimes though, possibly if the window is not initialised when build() is run, and the original mailing list post suggests the author needed it for some reason.
The example listens for keycodes 27 or 1001. As qua-non said while I was writing this, the former listens for esc, so you can get the same behaviour on desktop.
I didn't try without the android.map_key line, but it seems like it may not be necessary.
You mention you want to use kivy language and not python. You need to do some python to get this result, and I don't see a way around that (it's not really the domain of the kv language). I guess you could shift some stuff to kv by defining a 'go_back' event somewhere and triggering this when the key is pressed, along with binding your screenmanager to watch that event, but it seems like a long way around.
I based my code on the mailing list thread at https://groups.google.com/forum/#!topic/kivy-users/7rOZGMMIFXI . There might be a better way, but this is quite functional.
Now all the way in 2020 I'm using:
Clock.schedule_once(lambda x: Window.bind(on_keyboard=self.hook_keyboard))
in combination with a similar hook_keyboard method to the other answers, to delay the bind in my build method. Works fine, but none of these other ways ways seemed to work for me anymore.

Categories

Resources