Qpython Kivy TextInput focus is not working - android

Focusing on the TextInput ends this simple Python script
using Qpython on an Android phone
from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
Builder.load_string("""
#:import kivy kivy
#:import sys sys
<Home>:
orientation: "vertical"
Label:
text: "testing textinput problem"
TextInput:
text: "123"
Label:
text: "kivy version %s" % kivy.__version__
""")
class Home (BoxLayout):
pass
runTouchApp (Home() )
There is no error message in the Runtime Log.
The error does not occur when the script is run on the PC.
The error occurs after Qpython switched to Kivy 1.8

Related

Buildozer how to package app with service

I have a simple program to try making an app with a service that plays a song. Here is my code:
main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.utils import platform
kv = '''
Button:
text: 'push me!'
'''
class ServiceApp(App):
def build(self):
if platform == 'android':
from android import AndroidService
service = AndroidService('Song App', 'Song Playing')
service.start('service started')
return Builder.load_string(kv)
ServiceApp().run()
and service main.py
from kivy.core.audio import SoundLoader
#I have a file called song.wav in the same directory
sound = SoundLoader.load('song.wav')
if sound:
sound.play()
This works perfectly in Kivy Launcher, but I don't know how I can package this with buildozer. Any help would be appreciated!

Easy kivy app work on windows but not on Android

I'm trying to study Kivy, so I've done a simple gui that work on windows but when I run it on Android with Kivy Launcher I'm getting this error trought ADB LOGCAT :
This is a simple kv code :
#:kivy 1.0.9
<ROT>:
FloatLayout:
AnchorLayout:
anchor_x:"center"
anchor_y: "top"
Button:
size_hint : 1,1
text : "prova"
on_press: root.press()
and this is the main.py :
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
class ROT(Widget):
def press(self):
print "ciao"
pass
class PongApp(App):
def build(self):
self.root = Builder.load_file('questionario.kv')
return ROT()
if __name__ == '__main__':
PongApp().run()
I can't understand why it doesn't want to work

Python Kivy - App that open url in a native web browser

I try to make a simple app that open a web page inside Kivy after clicking a button placed on a "Screen One".
I used this topic (Python - Showing a web browser/iframe right into the app) as reference but I didn't understand how to use the code provided by Michael...
So I tried this... and when I launch the apk (build with Buildozer) it didn't work :')
import kivy
kivy.require('1.9.2')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
# MICHAEL'S CODE
from kivy.utils import platform
from kivy.uix.widget import Widget
from kivy.clock import Clock
from jnius import autoclass
from android.runnable import run_on_ui_thread
WebView = autoclass('android.webkit.WebView')
WebViewClient = autoclass('android.webkit.WebViewClient')
activity = autoclass('org.renpy.android.PythonActivity').mActivity
class Wv(Widget):
def __init__(self, **kwargs):
super(Wv, self).__init__(**kwargs)
Clock.schedule_once(self.create_webview, 0)
#run_on_ui_thread
def create_webview(self, *args):
webview = WebView(activity)
webview.getSettings().setJavaScriptEnabled(True)
wvc = WebViewClient();
webview.setWebViewClient(wvc);
activity.setContentView(webview)
webview.loadUrl('http://www.google.com/')
# END OF MICHAEL'S CODE
Builder.load_string('''
<ScreenOne>:
BoxLayout:
Label:
text: "SCREEN 1"
Button:
text: "GO GO GO TO GOOGLE !"
on_press: root.open_browser()
<ScreenTwo>:
BoxLayout:
Label:
text: "SCREEN 2"
Button:
text: "GO GO GO TO SCREEN 1"
on_press:
root.manager.transition.direction = "right"
root.manager.transition.duration = 1
root.manager.current = "screen_one"
''')
class ScreenOne(Screen):
def open_browser(self):
return Wv()
class ScreenTwo(Screen):
pass
screen_manager = ScreenManager()
screen_manager.add_widget(ScreenOne(name="screen_one"))
screen_manager.add_widget(ScreenTwo(name="screen_two"))
class BrowserApp(App):
def build(self):
return screen_manager
app = BrowserApp()
app.run()
The app don't crash but just close when I start it.
What I'm doing wrong ? I'm sure that I don't use it the right way...
Log from adb logcat:
06-13 12:54:47.559 7429 7510 I python : ImportError: No module named android
06-13 12:54:47.579 7429 7510 I python : Python for android ended.
From the log you posted in the comments I extracted the two important lines:
06-13 12:54:47.559 7429 7510 I python : ImportError: No module named android
06-13 12:54:47.579 7429 7510 I python : Python for android ended.
This basically means that the copied code:
from android.runnable import run_on_ui_thread
won't work because it doesn't detect the android module. The module has a separate recipe, therefore you will need to add it to the requirements, so that it compiles the Cython code and adds it to your app, otherwise the import will always fail.
Basically you always want to search for 3-4 keywords when looking into such a messy logcat → "python", "Traceback", "Python for android", "kivy". There's a filter in buildozer for that if you use it:
android.logcat_filters = *:S python:D

how to connect external libraries in Kivy

When you try to build the application. Application normally collected and only runs if there is no outside application libraries. When you attempt to connect networkx library. Appendix normally gather. But when you try to run directly on the device. Pops saver "Loadind ..." and the application falls. What you need to change to get everything working.
Attached is the application code:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scatter import Scatter
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics.vertex_instructions import *
from kivy.graphics.context_instructions import Color
import networkx as nx
G = nx.Graph()
G.add_node(1)
G.add_node(2)
G.add_node(3)
G.add_node(4)
G.add_edge(1,2)
G.add_edge(1,3)
G.add_edge(3,4)
G.add_edge(2,3)
class SimpleKivy(App):
def build(self):
b = BoxLayout()
l = Label(text=str(nx.shortest_path(G,1,4)))
textinput1 = TextInput(text=str(nx.shortest_path(G,1,4)))
textinput1.bind(text=l.setter('text'))
f = FloatLayout()
s = Scatter()
s.add_widget(l)
f.add_widget(s)
b.add_widget(f)
b.add_widget(textinput1)
return b
if __name__ == "__main__":
SimpleKivy().run()
In your buildozer.spec file, line 39 add your third-party requirments.
requirements = kivy,networkx, # or what ever

import widget in kivy file

I created some custom widget.
from kivy.uix.widget import Widget
from kivy.lang import Builder
class ExampleWidget(Widget):
Builder.load_file("kv/example.kv")
kv/example.kv
#:kivy 1.9.1
<ExampleWidget>:
Label:
text: Example
Than I want to create another widget with example widget. Like this:
kv/second.kv
#:kivy 1.9.1
<SecondWidget>:
ExampleWidget:
But I got this error
kivy.factory.FactoryException: Unknown class <ExampleWidget>
Kivy can't find my custom widget, so how I can import it to another kivy file?
You can import it using following syntax (assuming that ExampleWidget is defined in example.py file and you have __init__.py in your directory):
#: import ExampleWidget example.ExampleWidget
<SecondWidget>:
ExampleWidget:
Described in the documentation.

Categories

Resources