How to print button text value on console in Kivy - kivy

My app just have one button and its text is a random number from 1 to 9. I want to print the button text on console on on_press. Actually, i have minimized my problem, i want to compare the value with a variable, if button.text=5, do something else do_something_else.
My attempt:
#!/usr/bin/kivy
import kivy
kivy.require('1.7.2')
from random import random
from random import choice
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.properties import StringProperty
Builder.load_string("""
<Highest>:
GridLayout:
cols: 1
Button:
text: root.r1c2
on_press: root.new()
""")
class Highest(Screen):
r1c2 = StringProperty(str(int(random()*10)))
def new(self):
print self.text
# 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()
But this gives error.

You don't say what error you're getting, but one obvious problem is self.text doesn't exit. You are calling it on Highest(Screen), not the Button. To access kv objects from python you would normally tag them with ids and then use root_widget.ids.id_of_widget(see docs). An alternative is to use Kivy Properties.
Since you already have a Kivy Property bound to the text of the button, you can just print that out:
print self.r1c1

Related

how to remove the time and the navigation bar of the phone visible on my application?

I want to see my kivy application compiled with Buildozer on an android phone without seeing the navigation bar and the phone time .
I try:
from kivy.config import Config
Config.set('graphics', 'borderless', '1')
Config.set('graphics', 'fullscreen', 'True')
from kivy.core.window import Window
Window.show()

Transition and Function call on button press

I want to show a waiting screen while doing some stuff in the background. Actually, the screen does not show the waiting screen although the root.transition.current works correctly.
.py file
import sys
import time
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
kv = '''
<MyScreenManager>:
AcceptShutdown:
Waiting:
<AcceptShutdown>
name: 'acceptShutdown'
BoxLayout:
orientation: 'vertical'
BoxLayout:
Label:
text: 'Do you really want to exit?'
Button:
text: 'OK'
on_press:
root.manager.transition.direction = 'down' #these three
root.manager.current = 'waiting' #lines are
Clock.schedule_once(root.shutdown, 5) #the problem
<Waiting>
name: 'waiting'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Take a break.'
'''
Builder.load_string(kv)
class Waiting(Screen):
pass
class MyScreenManager(ScreenManager):
pass
class AcceptShutdown(Screen):
def shutdown(self, time):
sys.exit()
pass
class Shutdown(Screen):
pass
class Panel(App):
sm = MyScreenManager()
def build(self):
return self.sm
if __name__ == "__main__":
Panel().run()
It calls the function but it does not change the screen.
When I only do the transition (without the function call afterwards), it has the expected result. The screen shows the waiting screen.
Doing it the other way round (only function call without transition) also this works.
Hopefully, someone can help me out.
The problem is that time.sleep(5) does exactly what it says: the Python process sleeps for 5 seconds instead of updating the gui to reflect the changes you just made.
Instead, use Clock.schedule_once(root.shutdown, 5) and make shutdown expect an argumuent that it can ignore (Clock.schedule_once passes the time since the scheduling as an automatic argument). This will insert root.shutdown as something for Kivy to do later, without blocking the drawing loop.

Possible bug in Kivy markup

I have a kivy label and I wish to underline the text. The bold, colour and italic tags work correctly, but I seem to have a problem with underline and strikethrough. Is this a bug? I seem to have the correct syntax.
from kivy.app import App
from kivy.uix.label import Label
class Hello(App):
def build(self):
lbl = Label(text='[b]One[/b] [u]two[/u] [i]three[/i] [color=ff0000]four[/color] [s]five[/s]', markup=True)
return lbl
Hello().run()
I am using the SDL2 text provider and Kivy 1.9.1
Apparently it's not implemented in your version yet: https://github.com/kivy/kivy/issues/4059

Kivy-- Image as Button

I am just beginner in kivy and object oriented programming.
I have been practicing this code as a combination of the tutorials here:
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
Builder.load_string("""
<ImageButton>:
FloatLayout:
Image:
source:'resizedA.png'
size_hint: .2, .2
""")
class ImageButton(ButtonBehavior,FloatLayout, Image):
def on_press(self):
print ('pressed')
class The_AssignmentApp(App):
def build(self):
return ImageButton()
if __name__ == "__main__":
The_AssignmentApp().run()
My question is, why is that even if I press the other parts of the screen(not the image), the app still considers the whole scree as a button?
Pardon my ignorance here, I would really want to learn. Thanks!
class ImageButton(ButtonBehavior,FloatLayout, Image):
Don't inherit from multiple widgets (in this case FloatLayout and Image), this will lead to some weird bugs.
As for your specific problem, the ButtonBehavior is a parent class of the ImageButton which is the root widget and fills the screen. For this reason, the whole screen is a button, though you clearly intended otherwise.
Is the following more like what you wanted? You can also just use a FloatLayout instead of creating the new RootWidget class, I just did this to fit with what you already wrote.
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
class RootWidget(FloatLayout):
pass
class ImageButton(ButtonBehavior, Image):
def on_press(self):
print ('pressed')
Builder.load_string("""
<RootWidget>:
ImageButton:
source:'resizedA.png'
size_hint: .2, .2
""")
class The_AssignmentApp(App):
def build(self):
return RootWidget()
if __name__ == "__main__":
The_AssignmentApp().run()

How to call a function as soon as widget is launched in Kivy

My app just have one button. I want to call a function as soon as widget is created/called. I have used init for the same.
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.boxlayout import BoxLayout
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
Builder.load_string("""
<Highest>:
GridLayout:
cols: 1
Button:
id: btn_0
text: "0"
on_press: root.new()
""")
class Highest(Screen):
def __init__(self,name):
print "widget launched"
def new(self):
print "button pressed"
# 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()
On running, "widget launched" is displayed in consolde, but my application crashes with statement "python have stopped working". Please help.
If you really want to detect instantiation, create your own Button subclass that calls the function in its __init__ method.
If that's not directly what you really want, there may be better alternatives.

Resources