How to make a Kivy App that interprets python code? - kivy

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
class MyApp(App):
def build(self):
layout = BoxLayout(padding=10, orientation='vertical')
btn1 = Button(text="Run")
btn1.bind(on_press=self.buttonClicked)
layout.add_widget(btn1)
self.lbl1 = Label(text="After Running.. ")
layout.add_widget(self.lbl1)
self.txt1 = TextInput(text='', multiline=True)
layout.add_widget(self.txt1)
return layout
# button click function
def buttonClicked(self, btn):
self.lbl1.text = self.txt1.text
# run app
if __name__ == "__main__":
MyApp().run()
I want to run python code in my Kivy app. That is, when I press "Run" it runs the program. Any ideas on how to do it?

codeInString = """
def main(x):
print(x)
main("Mo")
"""
codeObejct = compile(codeInString, 'function', 'exec')
exec(codeObejct)
I think this is one possible way to do it.

Related

I want write codes for counting numbers app. My app can count the numbers of pages. I want to add button for cleaning screen

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
class TestScreen(Screen):
def __init__(self, **kwargs):
Screen.__init__(self, **kwargs)
layout = BoxLayout(orientation="vertical")
self.add_widget(layout)
layout.add_widget(Label(text=self.name, font_size="150sp"))
button = Button(text="Count",font_size='30sp')
layout.add_widget(button)
button.bind(on_press=self.add_screen)
def add_screen(self, *args):
n = len(self.manager.screen_names)
screen = TestScreen(name="{}".format(n))
self.manager.add_widget(screen)
self.manager.current = screen.name
# Create the screen manager
sm = ScreenManager()
sm.add_widget(TestScreen(name=''))
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()
I can't add button with a view to cleaning screen. I know this app not for counting. But i only need add button for cleaning numbers.

How can I implement a scrolling label in Kivy without using Builder (or a .kv file)?

I am trying to implement a scrolling label in a Kivy program, and found this example (slightly modified) that works:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty
from kivy.lang import Builder
long_text = "".join(["this is a long line "+str(n)+"\n" for n in range(1,101)])
Builder.load_string('''
<ScrollableLabel>:
Label:
size_hint_y: None
height: self.texture_size[1]
text_size: self.width, None
text: root.text
''')
class ScrollableLabel(ScrollView):
text = StringProperty('')
class ScrollApp(App):
def build(self):
return ScrollableLabel(text=long_text)
if __name__ == "__main__":
ScrollApp().run()
Partly for my own education, I am trying to convert this sample to not use Builder (and not resort to a .kv file). I have modified the above example to:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty
long_text = "".join(["this is a long line "+str(n)+"\n" for n in range(1,101)])
class ScrollableLabel(ScrollView):
text = StringProperty('')
def __init__(self, **kwargs):
super(ScrollableLabel, self).__init__(**kwargs)
self.label = Label(size_hint_y=None, text=self.text)
self.label.height = self.label.texture_size[1]
self.label.text_size = (self.label.width, None)
self.add_widget(self.label)
class ScrollApp(App):
def build(self):
return ScrollableLabel(text=long_text)
if __name__ == "__main__":
ScrollApp().run()
To my obviously untutored eye, these programs look like they should be equivalent. However, my (second) version doesn't work correctly (on several fronts).
So my question is two-fold: why doesn't the second version work the same as the first, and (if the answer isn't obvious from the first), how can I make it do so?
Thanks! -David
Try this:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty
from kivy.clock import Clock
long_text = "".join(["this is a long line "+str(n)+"\n" for n in range(1,101)])
class ScrollableLabel(ScrollView):
text = StringProperty('')
def __init__(self, **kwargs):
super(ScrollableLabel, self).__init__(**kwargs)
self.label = Label(size_hint_y=None, text=self.text)
self.add_widget(self.label)
Clock.schedule_once(self.update, 1)
def update(self, *args):
self.label.text_size = (self.label.width, None)
self.label.height = self.label.texture_size[1]
class ScrollApp(App):
def build(self):
return ScrollableLabel(text=long_text)
if __name__ == "__main__":
ScrollApp().run()
the output now is the same as your first

How to know if user is scrolling up or scrolling down in Kivy?

I am trying to make an Application in kivy which uses ScrollView. Is there any way through which I can know if user is scrolling down or scrolling up.
You can store mouse position received in on_scroll_move and then determine the direction by comparing value you have now with value you've saved before.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
Builder.load_string('''
<MyWidget>:
ScrollView:
on_scroll_start: root.scroll_pos_y = args[1].pos[1]
on_scroll_move: root.scroll_direction(args[1].pos[1])
Label:
text: 'test'
size_hint_y: None
height: 1000
''')
class MyWidget(BoxLayout):
scroll_pos_y = 0
def scroll_direction(self, new_scroll_pos_y):
if new_scroll_pos_y - self.scroll_pos_y < 0:
print('up')
else:
print('down')
self.scroll_pos_y = new_scroll_pos_y
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()

How to use random with button in Kivy

I just want to have 2 buttons in my KIVY APP.
One with text "Hello" and other having a random number from 0-9.
My code
#!/usr/bin/kivy
import kivy
kivy.require('1.7.2')
from random import random
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from random import random
Builder.load_string("""
<Highest>:
r1c1: "hello"
r1c2: random.randrange(10)
GridLayout:
cols: 1
Button:
text: root.r1c1
Button:
text: root.r1c2
""")
class Highest(Screen):
pass
# Create the screen manager
sm = ScreenManager()
sm.add_widget(Highest(name='Highest'))
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()
My code works if I just have one button with text - Hello. Random seems not working.
Perhaps it's because randrange isn't returning a string, but an int. You could try:
r1c2: str(random.randrange(10))
OR
Try adding it as a function to your root widget:
class Highest(Screen):
def get_rand(self):
return str(random.randrange(10))
And your kv would look like this:
r1c2: root.get_rand()

How to change popup label in kivy

My application appends a variable score by one on each click. I want to display a popup after each click to show score.
My attempt:
#!/usr/bin/kivy
import kivy
kivy.require('1.7.2')
from random import random
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from random import random
from random import choice
from kivy.properties import StringProperty
import time
score=0
my_popup = Popup(title='Test popup',
content=Label(text=str(score)),
size_hint=(None, None))
Builder.load_string("""
<Highest>:
GridLayout:
cols: 1
Button:
id: btn_0
text: "0"
on_press: root.new()
Label:
""")
class Highest(Screen):
def new(self):
global score
score=score+1
self.ids['btn_0'].text = str(score)
my_popup.open()
# Create the screen manager
sm = ScreenManager()
sm.add_widget(Highest(name='Highest'))
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()
However, score is always displayed as 0. Score is increasing correctly and can be seen on button text.
You would probably need to define my_popup as a function with score as argument:
def my_popup(updated_score):
pop = Popup(title='Test popup', content=Label(text=str(updated_score)),
size_hint=(None,None))
pop.open()
Then call it at function "new" passing the updated score:
class Highest(Screen):
def new(self):
global score
score += 1
self.ids['btn_0'].text = str(score)
my_popup(score)

Resources