[Beginner] Toggle Button

2,298 views
Skip to first unread message

lu...@sliacky.eu

unread,
Nov 2, 2014, 5:49:04 PM11/2/14
to kivy-...@googlegroups.com
Hi guys,

sorry for stupid question, but i begin with python and kivy a i have a basic problem:
I try create simple "application" with Checkbox and Toggle Button. Checkbox works fine, but i don't know how to work Toggle Button :/

My python code:
checkbox_forum.py

from kivy.uix.gridlayout import GridLayout
from kivy.app import App
from kivy.properties import StringProperty


class Check(GridLayout):
   
Status = StringProperty("Begin State")
   
Status1 = StringProperty("Begin ToggleButton")

   
def checkActive(self, *args):
       
if args[1]:
           
self.Status = "Device On"
           
print("Turning On")
       
else:
           
self.Status = "Device Off"
           
print("Turning Off")

   
def checkActive1(self, *args):
       
if args[1]:
           
self.Status3 = "Devide On"
           
print("Turning On")
       
else:
           
self.Status3 = "Device Off"
           
print("Turning Off")


class Check_forumApp(App):
   
def build(self):
       
self.title = "Checkboxes and Buttons"
       
return Check()

if __name__ == '__main__':
   
Check_forumApp().run()


check_forum.kv

<Check>:
    cols: 3
    canvas:
        Color:
            rgb: .2,.2,.2
        Rectangle:
            pos: self.pos
            size: self.size

    Label:
        text: 'A checkbox'
    CheckBox:
        on_active: root.checkActive(*args)
    Label:
        text: root.Status


    Label:
        text: 'Toggle Button'
    ToggleButton:
        text: 'ToggleButton'
        active: True
        on_press: root.checkActive1(*args)
    Label:
        text: root.Status1



In Toggle Button I want same operations like in Checkbox:

Print current Status
Change Status in kivy window.

Can you help to beginner?
Really thanks.

Bruce Cropley

unread,
Nov 2, 2014, 8:46:33 PM11/2/14
to kivy-...@googlegroups.com
When you are first starting out with Kivy, a really useful resource is the examples that come with the distribution. You can often find some code that you can tweak to see how it works.

Good luck :)

Lukáš Sliacky

unread,
Nov 3, 2014, 6:02:25 AM11/3/14
to kivy-...@googlegroups.com
Hi Bruce,

thank for your answer.
Of course, i tried "kivycatalog.py" and "showcase.py", where is a lot of examples, but only "visual cases" and no python functions.
I have problem with actions from kv file to python funcions (i.e. print() ).

ZenCODE

unread,
Nov 3, 2014, 3:06:04 PM11/3/14
to kivy-...@googlegroups.com
In your kv, try changing:

    on_press: root.checkActive1(*args)

to

    on_active: root.checkActive1(*args)

That not what you are looking for?

Cheers



Lukáš Sliacky

unread,
Nov 4, 2014, 3:10:48 AM11/4/14
to kivy-...@googlegroups.com
Hi ZenCODE,

thanks for your answer.

your code:
on_active: root.checkActive1(*args)

i was try before, but answer of terminal is

 ...
     
18:        text: 'Toggle Button'
     
19:    ToggleButton:
 
>>   20:        on_active: root.checkActive1(*args)
     
21:        text: 'ToggleButton'
     
22:    Label:
 
...
 
KeyError: 'active'

and this is my problem :/



ZenCODE

unread,
Nov 4, 2014, 4:08:56 AM11/4/14
to kivy-...@googlegroups.com
Sorry, try:

     on_state: root.checkActive1(*args)


Lukáš Sliacky

unread,
Nov 4, 2014, 4:43:28 AM11/4/14
to kivy-...@googlegroups.com
Hmm,

on_state: root.checkActive1(*args)

is better, works without errors, but not switching states.

kivy:
    Label:
        text
: 'Toggle Button'
   
ToggleButton:

        on_state
: root.checkActive1(*args)
        text
: 'ToggleButton'
   
Label:
        text
: root.Status1

Python:
    def checkActive1(self, *args):
       
if args[1]:

           
self.Status1 = "Device On"

           
print("Turning On")
       
else:

           
self.Status1 = "Device Off"
           
print("Turning Off")

Terminal (clicking on ToggleButton):
Turning On
Turning On
Turning On
Turning On
Turning On

and Status1 is only "Device On".

Checkbox switching states normally, but i don't know, why ToggleButton, not...

ZenCODE

unread,
Nov 4, 2014, 8:31:54 AM11/4/14
to kivy-...@googlegroups.com
Have a look at when parameters are coming through to checkActive1:

    def checkActive1(self, *args):
        print "args=" + str(args)
       
if args[1]: ....

You might find that you need to used args[0]?

Lukáš Sliacky

unread,
Nov 4, 2014, 10:13:21 AM11/4/14
to kivy-...@googlegroups.com
Hi ZenCODE, 

python:
    def checkActive1(self, *args):
       
print ("args=" + str(args))
       
if args[0]:

           
self.Status1 = "Device On"
           
print("Turning On")
       
else:
           
self.Status1 = "Device Off"
           
print("Turning Off")

terminal:

args=(<kivy.uix.togglebutton.ToggleButton object at 0x7f19828c0ca8>, 'down')
Turning On
args
=(<kivy.uix.togglebutton.ToggleButton object at 0x7f19828c0ca8>, 'normal')
Turning On
args
=(<kivy.uix.togglebutton.ToggleButton object at 0x7f19828c0ca8>, 'down')
Turning On
args
=(<kivy.uix.togglebutton.ToggleButton object at 0x7f19828c0ca8>, 'normal')
Turning On
args
=(<kivy.uix.togglebutton.ToggleButton object at 0x7f19828c0ca8>, 'down')

args[0] or args[1] have same output...


ZenCODE

unread,
Nov 4, 2014, 2:51:20 PM11/4/14
to kivy-...@googlegroups.com
Hi

Well
, not really the same output. args[0] is the toggle button itself and args[1] the state. Your code is testing:

    if args[0]:

That will always be True because the any object (here a toggle button) evaluates to True, and only None to False in the Python Truthiness universe. When you need to do is test the state of the button, so either:

    if args[0].state == 'down':

or, more optimally:

    if args[1] == 'down':

That should do it? Take note of the technique here of printing stuff to explore what information is coming through. Python introspection makes it easy to do this and it's an invaluable help in figuring out what is going wrong...;-)

Cheers




Lukáš Sliacky

unread,
Nov 4, 2014, 3:51:33 PM11/4/14
to kivy-...@googlegroups.com
Wow,

now works great.
Really thanks for your willingness and patience :)

ZenCODE

unread,
Nov 5, 2014, 12:53:37 AM11/5/14
to kivy-...@googlegroups.com
It's a pleasure :-)
Reply all
Reply to author
Forward
0 new messages