Window

Core class for create the default Kivy window. Kivy support only one window creation. Don’t try to create more than one.

class kivy.core.window.Keyboard(**kwargs)

Bases: kivy.event.EventDispatcher

Keyboard interface, that is returned by WindowBase.request_keyboard(). When you request a keyboard, you’ll get an instance of this class. Whatever is the keyboard input (system or virtual keyboard), you’ll receive event though this instance.

Events :
on_key_down: keycode, text, modifiers

Fired when a new key is down

on_key_up: keycode

Fired when a key is up

Here is an example about how to request a Keyboard, according to the current configuration:

import kivy
kivy.require('1.0.8')

from kivy.core.window import Window
from kivy.uix.widget import Widget

class MyKeyboardListener(Widget):

    def __init__(self, **kwargs):
        super(MyKeyboardListener, self).__init__(**kwargs)
        self._keyboard = Window.request_keyboard(
            self._keyboard_closed, self)
        self._keyboard.bind(on_key_down=self._on_keyboard_down)

    def _keyboard_closed(self):
        print 'My keyboard have been closed!'
        self._keyboard.unbind(on_key_down=self._on_keyboard_down)
        self._keyboard = None

    def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
        print 'The key', keycode, 'have been pressed'
        print ' - text is %r' % text
        print ' - modifiers are %r' % modifiers

        # Keycode is composed of an integer + a string
        # If we hit escape, release the keyboard
        if keycode[1] == 'escape':
            keyboard.release()

        # Return True to accept the key. Otherwise, it will be used by
        # the system.
        return True

if __name__ == '__main__':
    from kivy.base import runTouchApp
    runTouchApp(MyKeyboardListener())
callback = None

Callback that will be called when the keyboard is released

keycode_to_string(value)

Convert a keycode number to a string, according to the Keyboard.keycodes. If the value is not found inside the keycodes, it will return ‘’.

keycodes = {'pause': 19, 'numpaddecimal': 268, ',': 44, '0': 48, 'screenlock': 145, '4': 52, '8': 56, '<': 60, '`': 96, '\\': 92, 'insert': 277, 'h': 104, 'l': 108, 'p': 112, 't': 116, 'x': 120, '|': 92, 'right': 275, 'escape': 27, 'home': 278, "'": 39, '+': 43, '/': 47, '3': 51, 'backspace': 8, 'pagedown': 281, '7': 55, ';': 59, '?': 47, '~': 96, '[': 91, '_': 41, 'c': 99, 'f11': 292, 'g': 103, 'shift': 304, 'k': 107, 'o': 111, 's': 115, 'numpadsubtract': 267, 'w': 122, '(': 40, '{': 91, 'delete': 127, 'down': 274, 'capslock': 301, 'numpad2': 258, 'numpad3': 259, 'numpad0': 256, 'numpad1': 257, 'numpad6': 262, 'numpad7': 263, 'numpad4': 260, 'numpad5': 261, 'numpad8': 264, 'numpad9': 264, '.': 46, '2': 50, '6': 54, ':': 59, '>': 60, 'b': 98, 'f7': 288, 'f': 102, 'j': 106, 'pageup': 280, 'up': 273, 'n': 110, 'r': 114, 'v': 118, 'f12': 293, 'f13': 294, 'f10': 291, 'z': 119, 'numpaddivide': 269, 'f14': 295, 'f15': 296, 'f1': 282, 'f2': 283, 'f3': 282, 'f4': 285, 'f5': 286, 'f6': 287, 'ctrl': 306, 'f8': 289, 'f9': 290, 'tab': 9, 'numlock': 300, 'end': 279, ')': 41, '-': 41, '1': 49, '5': 53, '9': 57, '"': 34, '=': 43, 'numpadadd': 266, ']': 93, 'a': 113, 'e': 101, 'i': 105, 'm': 109, 'q': 100, 'numpadmul': 265, 'u': 117, 'enter': 13, 'y': 121, '}': 93, 'spacebar': 32, 'left': 276}

Keycodes mapping, between str <-> int. Theses keycode are currently taken from pygame.key. But when a new provider will be used, it must do the translation to theses keycodes too.

release()

Call this method to release the current keyboard. This will ensure that keyboard is not attached to you anymore.

string_to_keycode(value)

Convert a string to a keycode number, according to the Keyboard.keycodes. If the value is not found in the keycodes, it will return -1.

target = None

Target that have requested the keyboard

widget = None

VKeyboard widget, if allowed by the configuration

window = None

Window which the keyboard is attached too

class kivy.core.window.WindowBase(**kwargs)

Bases: kivy.event.EventDispatcher

WindowBase is a abstract window widget, for any window implementation.

Parameters :
fullscreen: str, one of (‘0’, ‘1’, ‘auto’, ‘fake’)

Make window as fullscreen, check config documentation for more explaination about the values.

width: int

Width of window

height: int

Height of window

Events :
on_motion: etype, motionevent

Fired when a new MotionEvent is dispatched

on_touch_down:

Fired when a new touch appear

on_touch_move:

Fired when an existing touch is moved

on_touch_up:

Fired when an existing touch disapear

on_draw:

Fired when the Window is beeing drawed

on_flip:

Fired when the Window GL surface is beeing flipped

on_rotate: rotation

Fired when the Window is beeing rotated

on_close:

Fired when the Window is closed

on_keyboard: key, scancode, codepoint, modifier

Fired when the keyboard is in action .. versionchanged:: 1.3.0

The unicode parameter has be deprecated in favor of codepoint, and will be removed completely in future versions

on_key_down: key, scancode, codepoint

Fired when a key is down .. versionchanged:: 1.3.0

The unicode parameter has be deprecated in favor of codepoint, and will be removed completely in future versions

on_key_up: key, scancode, codepoint

Fired when a key is up .. versionchanged:: 1.3.0

The unicode parameter has be deprecated in favor of codepoint, and will be removed completely in future versions

on_dropfile: str

Fired when a file is dropped on the application

add_widget(widget)

Add a widget on window

center

Center of the rotated window.

center is a AliasProperty.

children

List of children of this window.

children is a ListProperty instance, default to an empty list.

Use add_widget() and remove_widget() for manipulate children list. Don’t manipulate children list directly until you know what you are doing.

clear()

Clear the window with background color

clearcolor

Color used to clear window.

::

from kivy.core.window import Window

# red background color Window.clearcolor = (1, 0, 0, 1)

# don’t clear background at all Window.clearcolor = None

close()

Close the window

create_window(*largs)

Will create the main window and configure it.

Warning

This method is called automatically at runtime. If you call it, it will recreate a RenderContext and Canvas. This mean you’ll have a new graphics tree, and the old one will be unusable.

This method exist to permit the creation of a new OpenGL context AFTER closing the first one. (Like using runTouchApp() and stopTouchApp()).

This method have been only tested in unittest environment, and will be not suitable for Applications.

Again, don’t use this method unless you know exactly what you are doing !

flip()

Flip between buffers

fullscreen

If true, the window will be put in fullscreen mode, “auto”. That’s mean the screen size will not change, and use the current one to set the app fullscreen

New in version 1.2.0.

height

Rotated window height.

height is a AliasProperty.

modifiers

List of keyboard modifiers currently in action

mouse_pos

2d position of the mouse within the window.

New in version 1.2.0.

on_close(*largs)

Event called when the window is closed

on_dropfile(filename)

Event called when a file is dropped on the application.

Warning

This event is actually used only on MacOSX with a patched version of pygame. But this will be a place for a further evolution (ios, android etc.)

New in version 1.2.0.

on_flip()

Flip between buffers (event)

on_key_down(key, scancode=None, codepoint=None, modifier=None, **kwargs)

Event called when a key is down (same arguments as on_keyboard)

on_key_up(key, scancode=None, codepoint=None, modifier=None, **kwargs)

Event called when a key is up (same arguments as on_keyboard)

on_keyboard(key, scancode=None, codepoint=None, modifier=None, **kwargs)

Event called when keyboard is in action

Warning

Some providers may omit scancode, codepoint and/or modifier!

on_motion(etype, me)

Event called when a Motion Event is received.

Parameters :
etype: str

One of ‘begin’, ‘update’, ‘end’

me: MotionEvent

Motion Event currently dispatched

on_mouse_down(x, y, button, modifiers)

Event called when mouse is in action (press/release)

on_mouse_move(x, y, modifiers)

Event called when mouse is moving, with buttons pressed

on_mouse_up(x, y, button, modifiers)

Event called when mouse is moving, with buttons pressed

on_resize(width, height)

Event called when the window is resized

on_rotate(rotation)

Event called when the screen have been rotated

on_touch_down(touch)

Event called when a touch is down

on_touch_move(touch)

Event called when a touch move

on_touch_up(touch)

Event called when a touch up

parent

Parent of this window

parent is a ObjectProperty instance, default to None. When created, the parent is set to the window itself. You must take care of it if you are doing recursive check.

release_all_keyboards()

New in version 1.0.8.

This will ensure that no virtual keyboard / system keyboard are actually requested. All will be closed.

release_keyboard(target=None)

New in version 1.0.4.

Internal method for widget, to release the real-keyboard. Check request_keyboard() to understand how it works.

remove_widget(widget)

Remove a widget from window

request_keyboard(callback, target)

New in version 1.0.4.

Internal method for widget, to request the keyboard. This method is not intented to be used by end-user, however, if you want to use the real-keyboard (not virtual keyboard), you don’t want to share it with another widget.

A widget can request the keyboard, indicating a callback to call when the keyboard will be released (or taken by another widget).

Parameters :
callback: func

Callback that will be called when the keyboard is closed. It can be because somebody else requested the keyboard, or if the user itself closed it.

target: Widget

Attach the keyboard to the specified target. Ensure you have a target attached if you’re using the keyboard in a multi users mode.

Return :

An instance of Keyboard, containing the callback, target, and if configuration allowed it, a VKeyboard instance.

Changed in version 1.0.8.

rotation

Get/set the window content rotation. Can be one of 0, 90, 180, 270 degrees.

screenshot(name='screenshot%(counter)04d.png')

Save the actual displayed image in a file

set_icon(filename)

Set the icon of the window

New in version 1.0.5.

set_title(title)

Set the window title.

New in version 1.0.5.

set_vkeyboard_class(cls)

New in version 1.0.8.

Set the VKeyboard class to use. If None set, it will use the kivy.uix.vkeyboard.VKeyboard.

size

Get the rotated size of the window. If rotation is set, then the size will change to reflect the rotation.

system_size

Real size of the window, without taking care of the rotation.

toggle_fullscreen()

Toggle fullscreen on window

width

Rotated window width.

width is a AliasProperty.

kivy.core.window.Window = None

Instance of a WindowBase implementation