How to use pyttsx module in kivy app?
import kivy
import sys
kivy.require('1.9.0')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
class My(App):
def build(self):
return Myapp()
class Myapp(FloatLayout):
def callback(self,instance):
import pyttsx
self.engine=pyttsx.init()
if self.t1.text:
self.engine.say("you wrote something")
self.engine.runAndWait()
def __init__(self, **kwargs):
super(Myapp, self).__init__(**kwargs)
self.t1=TextInput(multiline=True,size_hint_x=0.5, size_hint_y=0.5)
btn1=Button(text='read',size_hint=(.1,.1),pos_hint= {'x': .5,'top': .2})
self.add_widget(self.t1)
self.add_widget(btn1)
self.bind(on_press=self.callback)
if __name__=='__main__':
My().run()
On clicking the button the callback function is called and it has to speak the text entered in text field.But it doesn't work.Is there any other way to do it?
Use the text to speech functions in plyer. These will call the Android api for the task.
Related
Like, can an output-only TextEntry(readonly=True) be changed with TextEntry.insert_text()? Does the readonly refers ONLY to the user part of the GUI?
I assume you actually mean TextInput. If so, the answer is: no, you cannot change the text in code using insert_text, but you can change it using a simple text=.
John Anderson has already given an answer.
You can download the Kivy Launcher or QPython from google playstore so that you can test your kivy codes without using a desktop.
Here is a simple code with TextInput using QPython2:
#-*-coding:utf8;-*-
#qpy:2
#qpy:kivy
#qpy:fullscreen
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.button import Button
class TestingText(Widget):
def __init__(self):
super(TestingText, self).__init__()
self.size = Window.size
self.t = TextInput(readonly=True)
self.b1 = Button(text="Insert")
self.b2 = Button(text="Set")
self.t.center = self.center
self.b2.right = self.right
self.b1.on_press = self.inserttext
self.b2.on_press = self.settext
self.add_widget(self.t)
self.add_widget(self.b1)
self.add_widget(self.b2)
def inserttext(self):
self.t.insert_text("insertme")
def settext(self):
self.t.text = "setme"
class Test(App):
def build(self):
return TestingText()
Test().run()
Did I write correctly? After the application is turned on, the android device must vibrate, but after the application turns on, it crashes. Why my apps on kivy crashed? Code:
from plyer import vibrator
from kivy.app import App
from kivy.properties import StringProperty
from kivy.clock import Clock, mainthread
from kivy.uix.floatlayout import FloatLayout #the UI layout
from kivy.uix.label import Label #a label to show information
class UI(FloatLayout):
"""docstring for UI"""
def __init__(self):
super(UI, self).__init__()
self.lblAcce = Label(text="Vibrate: ") #create a label at the center
self.add_widget(self.lblAcce) #add the label at the screen
time = 2
try:
vibrator.vibrate(time=time)
#if you want do disable it, just run: accelerometer.disable()
Clock.schedule_interval(self.update, 1.0/24) #24 calls per second
except:
self.lblAcce.text = "Failed to start vibrate" #error
class Vibrate(App): #our app
def build(self):
ui = UI()# create the UI
return ui #show it
if __name__ == '__main__':
Vibrate().run() #start our app
I have a kivy app with some textinput and I want to show in my smartphone a numeric keyboard. I've been reading about it and I think that with the property input_type=number I could get the right result but I realised that with the kivy updates doesn't work nowadays. How could I get the numeric keyboard when my textinput is focused? With the app in landscape mode it could be useful or the keyboard still will take half screen? Here do you have the code:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
class LoginScreen(GridLayout):
def __init__(self,**kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.cols=2
self.add_widget(Label(text='Subject'))
self.add_widget(Label(text=''))
self.add_widget(Label(text='1'))
self.add_widget(TextInput(multiline=False))
self.add_widget(Label(text='2'))
self.add_widget(TextInput(multiline=False))
self.add_widget(Label(text='3'))
self.add_widget(TextInput(multiline=False))
self.add_widget(Label(text='4'))
self.add_widget(TextInput(multiline=False))
b1=Button(text='Exit',background_color=[0,1,0,1],height=int(Window.height)/9.0) #doesn't work properly
self.add_widget(b1)
b2=Button(text='Run',background_color=[0,1,0,1],height=int(Window.height)/9.0) #doesn't work properly
self.add_widget(b2)
b1.bind(on_press=exit)
class SimpleKivy(App):
def build(self):
return LoginScreen()
if __name__=='__main__':
SimpleKivy().run()
I think is a bit late, bu maybe someone looks for it tomorrow.
Is true you should change the input_type propery of your TextInput, in your case for example:
self.add_widget(TextInput(multiline=False, input_type = 'number'))
I suggest you create a new custom widget for that in order works in android and desktop, like this that implement a maxdigits property:
class IntegerInput(TextInput):
def __init__(self, **kwargs):
super(IntegerInput, self).__init__(**kwargs)
self.input_type = 'number'
def insert_text(self, substring, from_undo=False):
if substring.isnumeric():
if hasattr(self, "maxdigits"):
if len(self.text) < self.maxdigits:
return super(IntegerInput,self).insert_text(substring, from_undo=from_undo)
else:
return super(IntegerInput, self).insert_text(substring, from_undo=from_undo)
I wrote an small kivy program, because I wanted to learn how to save values using .json files.
I checked out the documetation and found this:http://kivy.org/docs/api-kivy.storage.html#module-kivy.storage
I tried to to it similar,but I got the error:ImportError: No module named storage.jsonstore
Why doesn't it work?
Here is my code:
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.storage.jsonstore import JsonStore
from kivy.properties import StringProperty
from kivy.properties import NumericProperty
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
class Layout(BoxLayout):
def save(self, vinput):
store = JsonStore('hello.json')
store.put('tito', inpud=vinput)
ROOT = Builder.load_string('''
BoxLayout:
orientation: 'vertical'
TextInput:
id:my_textinput
Button:
text: 'save'
Button:
text: 'acsess'
''')
class Caption(App):
def build(self):
Window.clearcolor = (0, 0, 1, 1)
return ROOT
if __name__ == '__main__':
Caption().run()
I have never used JsonStore before, I will have a look at it, then will update the answer, but for now, you could do it this way.
import json
#If you want to write in your JSON file.
out_file = open("your_file.json","w")
json.dump(result,out_file, indent=4)
# result ^ contains your data.
# indent = 4 is to make your file more readable.
out_file.close()
#If you want to read your JSON file.
in_file = open("your_file.json","r")
result = json.load(in_file)
in_file.close()
EDIT: 1
It actually works perfectly fine.
Try this improved code.
Your .py file
from kivy.app import App
from kivy.storage.jsonstore import JsonStore
from kivy.uix.boxlayout import BoxLayout
class Lay_out(BoxLayout):
def save(self, vinput):
store = JsonStore('hello.json')
store.put('tito', inpud=vinput)
class CaptionApp(App):
def build(self):
return Lay_out()
if __name__ == '__main__':
CaptionApp().run()
Your .kv file
<Lay_out>:
orientation: 'vertical'
TextInput:
id:my_textinput
Button:
text: 'save'
on_release: root.save(my_textinput.text)
Button:
text: 'acsess'
I got and issue that when i bind a function with an button that works good but when i bind a function which has a infinite loop in it so screen freezes and other button do not works as example below....please give me solution to break that loop using another button..
from kivy.uix.popup import Popup
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.stacklayout import StackLayout
class APPLICATION_START(FloatLayout):
def __init__(self, **kwargs):
super(MyApp, self).__init__(**kwargs)
self.buone = Button(text="Start Loop",pos=(0,180),size=(470,90),size_hint=(None,None))
self.logi.bind(on_release=self.loooop)
self.exitbtn = Button(text="exit",pos=(0,90),size=(235,70),size_hint=(None,None))
self.exitbtn.bind(on_press=exit)
def loooop(self,instance):
while True:
# do any Activity
#I want to break this loop when someone press on exit button
class MyApp(App):
def build(self):
return APPLICATION_START()
if __name__ == '__main__':
MyApp().run()
The gui can't update until your function returns, which it doesn't.
Either use a thread for your task, or if it is composed of many repetitions of a short task you can do Clock.schedule_interval(your_function, some_timestep) to run it repeatedly.