Button package: implement different type of button

# Jump directly to Examples

Button package: implement different type of button

class pymt.ui.widgets.button.MTButton(**kwargs)

Bases: pymt.ui.widgets.label.MTLabel

MTButton is a button implementation using MTLabel

Parameters :
label : string, default is ‘’

Label of button

anchor_x : string, default to ‘center’

Horizontal alignment of label inside button (left, center, right)

anchor_y : string, default to ‘center’

Vertical alignment of label inside button (bottom, center, top)

multiline : bool, default is False

Indicate if button is a multiline button

Styles :

Note

All the css attributes can be postfixed with the state of the button. It will only work with attributes used for background. For example

`bg-color` for normal state
`bg-color-down` for down state
bg-color : color

Background color of the button

color : color

color of the text/label on teh button

font-name : str

Name of font to use

font-size : int

Size of font in pixel

font-style : str

Style of font, can be “bold”, “italic” or “bolditalic”

draw-border : bool

Indicate if the border must be drawed or not

border-radius : int

Size of radius in pixel

border-radius-precision : float

Precision of the radius drawing (1 mean no precision)

draw-text-shadow : bool

Indicate if the text shadow must be drawed

text-shadow-color : color

Color of the text shadow

text-shadow-position : x y

Relative position of shadow text

draw-alpha-background : bool

Indicate if the alpha background must be drawed

Events :
on_state_change : (state string “down” or “normal”, )

Fired when the state of the button change

on_press (touch object, )

Fired when the button are pressed (not released)

on_release (touch object, )

Fired when the button are released

state

Sets the state of the button, “normal” or “down”

class pymt.ui.widgets.button.MTToggleButton(**kwargs)

Bases: pymt.ui.widgets.button.MTButton

Toggle button implementation, based on MTButton

Parameters :
group: str, default to None

Name of the selection group. If the button have the same groupname as other, when his state will be down, all other button will have up state.

static get_selected_widgets(groupname)

Return all widgets selected in a group

static get_widgets(groupname)

Return all widgets in a group

group

Return the group of a toggle button

class pymt.ui.widgets.button.MTImageButton(**kwargs)

Bases: pymt.ui.widgets.button.MTButton

MTImageButton is a enhanced MTButton that draw an image instead of a text.

Parameters :
filename : str

Filename of image

image : Image

Instead of giving a filename, give a Image object

scale : float, default is 1.0

Scaling of image, default is 100%, ie 1.0

Examples

File ui_widgets_button_align.py

from pymt import *

w = MTWidget()

y = 0
for halign in ('left', 'center', 'right'):
    y += 100
    x = 100
    for valign in ('top', 'center', 'bottom'):
        m = MTButton(label='%s:%s' % (halign, valign),
                     pos=(x, y), size=(150, 30),
                     anchor_x=halign, anchor_y=valign)
        x += 200
        w.add_widget(m)

runTouchApp(w)

File ui_widgets_button_css.py

from pymt import *

# Create a CSS with 2 rules
additional_css = '''
.simple {
        draw-alpha-background: 0;
        draw-border: 0;
        draw-text-shadow: 1;
}

.colored {
        bg-color: rgb(68, 170, 0);
        border-radius: 20;
        border-radius-precision: .1;
        font-size: 16;
}
'''

# Add the CSS into PyMT
css_add_sheet(additional_css)

# Create different button, with one or 2 rules at the same time
v = MTBoxLayout(orientation='vertical', padding=20, spacing=20)
v.add_widget(MTButton(label='Coucou'))
v.add_widget(MTButton(label='Coucou', cls='simple'))
v.add_widget(MTButton(label='Coucou', cls='colored'))
v.add_widget(MTButton(label='Coucou', cls=('simple', 'colored')))

runTouchApp(v)

File ui_widgets_button_toggle.py

from pymt import *

b = MTToggleButton(label='Push me')

@b.event
def on_press(*largs):
        print 'on_press()', b.state, largs

@b.event
def on_release(*largs):
        print 'on_release()', b.state, largs

runTouchApp(b)

File ui_widgets_button.py

'''
Button example with all events in button.
'''

from pymt import *

b = MTButton(label='Push me')
@b.event
def on_press(*largs):
        print 'on_press()', b.state, largs

@b.event
def on_release(*largs):
        print 'on_release()', b.state, largs

@b.event
def on_state_change(*largs):
    print 'on_state_change()', b.state, largs

runTouchApp(b)

File ui_widgets_buttonmatrix.py

from pymt import *

# create a custom 10x10 matrix, fullscreen
m = MTButtonMatrix(matrix_size=(10, 10), size_hint=(1, 1))

# create a default handler for the on_press event
def m_on_press(args):
    # extract row / column / state
    row, column, state = args
    print 'matrix change at %d x %d = %s' % (row, column, state)

# connect the handler to the widget
m.connect('on_press', m_on_press)

runTouchApp(m)

Table Of Contents

Previous topic

ScreenLayout: display only one widget in fullscreen at time

Next topic

Button matrix: a lightweight and optimized grid of buttons

This Page