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'
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
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.
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.
I'm having trouble with assigning a value to a variable while using TextInput from the Kivy library.
First of all, I'm trying to create a lease calculator on Kivy. I have already created the calculator but now I'm trying to convert it to the Kivy framework so I can use it on an android device.
I got stuck while trying to subtract the two values (lease and mileage). I tried:
milesleft = int(lease) - int(mileage) but it told me "int() argument must be a string or a number, not 'TextInput'"
I'm very confused and have been searching for awhile for a solution. Please, any help or advice is appreciated!
leaseapp.py
import kivy
kivy.require('1.7.2')
from datetime import datetime, timedelta
import time
from kivy.core.window import Window
from kivy.uix.textinput import TextInput
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scatter import Scatter
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.config import Config
from kivy.base import runTouchApp
if __name__ == '__main__':
root = BoxLayout(orientation='vertical', padding=20, spacing=10)
lease = TextInput(multiline=False, hint_text="Lease allowance per year", input_type='number')
lease.add_widget(TextInput(size_hint=(1, None)))
root.add_widget(lease)
mileage = TextInput(multiline=False, hint_text="Current mileage", input_type='number')
mileage.add_widget(TextInput(size_hint=(1, None)))
root.add_widget(mileage)
milesleft = int(lease) - int(mileage)
submitbutton = Button(text='Submit')
root.add_widget(submitbutton)
runTouchApp(root)
Try
milesleft = int(lease.text) - int(mileage.text)