Quick search

Table Of Contents

Window

Core class for creating the default Kivy window. Kivy supports only one window per application: please don’t try to create more than one.

class kivy.core.window.Keyboard(**kwargs)[ソース]

ベースクラス: 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 the keyboard input is (system or virtual keyboard), you’ll receive events through this instance.

Events:
on_key_down: keycode, text, modifiers

Fired when a new key is pressed down

on_key_up: keycode

Fired when a key is released (up)

Here is an example of how to request a Keyboard in accordance with 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, 'text')
        if self._keyboard.widget:
            # If it exists, this widget is a VKeyboard object which you can use
            # to change the keyboard layout.
            pass
        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 in the keycodes, it will return ‘’.

release()[ソース]

Call this method to release the current keyboard. This will ensure that the keyboard is no longer attached to your callback.

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)[ソース]

ベースクラス: kivy.event.EventDispatcher

WindowBase is an abstract window widget for any window implementation.

Parameters:
borderless: str, one of (‘0’, ‘1’)

Set the window border state. Check the config documentation for a more detailed explanation on the values.

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

Make the window fullscreen. Check the config documentation for a more detailed explanation on the values.

width: int

Width of the window.

height: int

Height of the window.

minimum_width: int

Minimum width of the window (only works for sdl2 window provider).

minimum_height: int

Minimum height of the window (only works for sdl2 window provider).

allow_screensaver: bool

Allow the device to show a screen saver, or to go to sleep on mobile devices. Defaults to True. Only works for sdl2 window provider.

Events:
on_motion: etype, motionevent

Fired when a new MotionEvent is dispatched

on_touch_down:

Fired when a new touch event is initiated.

on_touch_move:

Fired when an existing touch event changes location.

on_touch_up:

Fired when an existing touch event is terminated.

on_draw:

Fired when the Window is being drawn.

on_flip:

Fired when the Window GL surface is being flipped.

on_rotate: rotation

Fired when the Window is being rotated.

on_close:

Fired when the Window is closed.

on_request_close:

Fired when the event loop wants to close the window, or if the escape key is pressed and exit_on_escape is True. If a function bound to this event returns True, the window will not be closed. If the the event is triggered because of the keyboard escape key, the keyword argument source is dispatched along with a value of keyboard to the bound functions.

バージョン 1.9.0 で追加.

on_cursor_enter:

Fired when the cursor enters the window.

バージョン 1.9.1 で追加.

on_cursor_leave:

Fired when the cursor leaves the window.

バージョン 1.9.1 で追加.

on_minimize:

Fired when the window is minimized.

バージョン 1.10.0 で追加.

on_maximize:

Fired when the window is maximized.

バージョン 1.10.0 で追加.

on_restore:

Fired when the window is restored.

バージョン 1.10.0 で追加.

on_hide:

Fired when the window is hidden.

バージョン 1.10.0 で追加.

on_show:

Fired when when the window is shown.

バージョン 1.10.0 で追加.

on_keyboard: key, scancode, codepoint, modifier

Fired when the keyboard is used for input.

バージョン 1.3.0 で変更: The unicode parameter has been deprecated in favor of codepoint, and will be removed completely in future versions.

on_key_down: key, scancode, codepoint, modifier

Fired when a key pressed.

バージョン 1.3.0 で変更: The unicode parameter has been deprecated in favor of codepoint, and will be removed completely in future versions.

on_key_up: key, scancode, codepoint

Fired when a key is released.

バージョン 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.

注釈

This event doesn’t work for apps with elevated permissions, because the OS API calls are filtered. Check issue #4999 for pointers to workarounds.

on_memorywarning:

Fired when the platform have memory issue (iOS / Android mostly) You can listen to this one, and clean whatever you can.

バージョン 1.9.0 で追加.

add_widget(widget, canvas=None)[ソース]

Add a widget to a window

allow_screensaver

Whether the screen saver is enabled, or on mobile devices whether the device is allowed to go to sleep while the app is open.

バージョン 1.10.0 で追加.

allow_screensaver is a BooleanProperty and defaults to True.

borderless

When set to True, this property removes the window border/decoration. Check the config documentation for a more detailed explanation on the values.

バージョン 1.9.0 で追加.

borderless is a BooleanProperty and defaults to False.

center

Center of the rotated window.

バージョン 1.0.9 で追加.

center is an AliasProperty.

children

List of the children of this window.

children is a ListProperty instance and defaults to an empty list.

Use add_widget() and remove_widget() to manipulate the list of children. Don’t manipulate the list directly unless you know what you are doing.

clear()[ソース]

Clear the window with the background color

clearcolor

Color used to clear the 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

バージョン 1.7.2 で変更: The clearcolor default value is now: (0, 0, 0, 1).

バージョン 1.0.9 で追加.

clearcolor is an AliasProperty and defaults to (0, 0, 0, 1).

close()[ソース]

Close the window

create_window(*largs)[ソース]

Will create the main window and configure it.

警告

This method is called automatically at runtime. If you call it, it will recreate a RenderContext and Canvas. This means 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 has only been tested in a unittest environment and is not suitable for Applications.

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

dpi()

Return the DPI of the screen. If the implementation doesn’t support any DPI lookup, it will just return 96.

警告

This value is not cross-platform. Use kivy.base.EventLoop.dpi instead.

flip()[ソース]

Flip between buffers

focus

Check whether or not the window currently has focus.

バージョン 1.9.1 で追加.

focus is a read-only AliasProperty and defaults to True.

fullscreen

This property sets the fullscreen mode of the window. Available options are: True, False, ‘auto’ and ‘fake’. Check the config documentation for more detailed explanations on these values.

fullscreen is an OptionProperty and defaults to False.

バージョン 1.2.0 で追加.

注釈

The ‘fake’ option has been deprecated, use the borderless property instead.

grab_mouse()[ソース]

Grab mouse - so won’t leave window

バージョン 1.10.0 で追加.

注釈

This feature requires the SDL2 window provider.

height

Rotated window height.

height is a read-only AliasProperty.

hide()[ソース]

Hides the window. This method should be used on desktop platforms only.

バージョン 1.9.0 で追加.

注釈

This feature requires the SDL2 window provider and is currently only supported on desktop platforms.

icon

A path to the window icon.

バージョン 1.1.2 で追加.

icon is a StringProperty.

keyboard_anim_args = {'t': 'in_out_quart', 'd': 0.5}

The attributes for animating softkeyboard/IME. t = transition, d = duration. Will have no effect on desktops.

バージョン 1.10.0 で追加.

keyboard_anim_args is a dict with values ‘t’: ‘in_out_quart’, ‘d’: .5.

keyboard_height

Returns the height of the softkeyboard/IME on mobile platforms. Will return 0 if not on mobile platform or if IME is not active.

注釈

This property returns 0 with SDL2 on Android, but setting Window.softinput_mode does works.

バージョン 1.9.0 で追加.

keyboard_height is a read-only AliasProperty and defaults to 0.

keyboard_padding

The padding to have between the softkeyboard/IME & target or bottom of window. Will have no effect on desktops.

バージョン 1.10.0 で追加.

keyboard_padding is a NumericProperty and defaults to 0.

left

Left position of the window.

注釈

It’s an SDL2 property with [0, 0] in the top-left corner.

バージョン 1.10.0 で変更: left is now an AliasProperty

バージョン 1.9.1 で追加.

left is an AliasProperty and defaults to position set in Config.

maximize()[ソース]

Maximizes the window. This method should be used on desktop platforms only.

バージョン 1.9.0 で追加.

注釈

This feature requires the SDL2 window provider and is currently only supported on desktop platforms.

minimize()[ソース]

Minimizes the window. This method should be used on desktop platforms only.

バージョン 1.9.0 で追加.

注釈

This feature requires the SDL2 window provider and is currently only supported on desktop platforms.

minimum_height

The minimum height to restrict the window to.

バージョン 1.9.1 で追加.

minimum_height is a NumericProperty and defaults to 0.

minimum_width

The minimum width to restrict the window to.

バージョン 1.9.1 で追加.

minimum_width is a NumericProperty and defaults to 0.

modifiers

List of keyboard modifiers currently active.

バージョン 1.0.9 で追加.

modifiers is an AliasProperty.

mouse_pos

2d position of the mouse within the window.

バージョン 1.2.0 で追加.

mouse_pos is an ObjectProperty and defaults to [0, 0].

on_close(*largs)[ソース]

Event called when the window is closed

on_cursor_enter(*largs)[ソース]

Event called when the cursor enters the window.

バージョン 1.9.1 で追加.

注釈

This feature requires the SDL2 window provider.

on_cursor_leave(*largs)[ソース]

Event called when the cursor leaves the window.

バージョン 1.9.1 で追加.

注釈

This feature requires the SDL2 window provider.

on_dropfile(filename)[ソース]

Event called when a file is dropped on the application.

警告

This event currently works with sdl2 window provider, on pygame window provider and OS X with a patched version of pygame. This event is left in place for further evolution (ios, android etc.)

バージョン 1.2.0 で追加.

on_flip()[ソース]

Flip between buffers (event)

on_hide(*largs)[ソース]

Event called when the window is hidden.

バージョン 1.10.0 で追加.

注釈

This feature requires the SDL2 window provider.

on_joy_axis(stickid, axisid, value)[ソース]

Event called when a joystick has a stick or other axis moved

バージョン 1.9.0 で追加.

on_joy_ball(stickid, ballid, value)[ソース]

Event called when a joystick has a ball moved

バージョン 1.9.0 で追加.

on_joy_button_down(stickid, buttonid)[ソース]

Event called when a joystick has a button pressed

バージョン 1.9.0 で追加.

on_joy_button_up(stickid, buttonid)[ソース]

Event called when a joystick has a button released

バージョン 1.9.0 で追加.

on_joy_hat(stickid, hatid, value)[ソース]

Event called when a joystick has a hat/dpad moved

バージョン 1.9.0 で追加.

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 released (same arguments as on_keyboard)

on_keyboard(key, scancode=None, codepoint=None, modifier=None, **kwargs)[ソース]

Event called when keyboard is used.

警告

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

on_maximize(*largs)[ソース]

Event called when the window is maximized.

バージョン 1.10.0 で追加.

注釈

This feature requires the SDL2 window provider.

on_memorywarning()[ソース]

Event called when the platform have memory issue. Your goal is to clear the cache in your app as much as you can, release unused widget, etc.

Currently, this event is fired only from SDL2 provider, for iOS and Android.

バージョン 1.9.0 で追加.

on_minimize(*largs)[ソース]

Event called when the window is minimized.

バージョン 1.10.0 で追加.

注釈

This feature requires the SDL2 window provider.

on_motion(etype, me)[ソース]

Event called when a Motion Event is received.

Parameters:
etype: str

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

me: MotionEvent

The Motion Event currently dispatched.

on_mouse_down(x, y, button, modifiers)[ソース]

Event called when the mouse is used (pressed/released)

on_mouse_move(x, y, modifiers)[ソース]

Event called when the mouse is moved with buttons pressed

on_mouse_up(x, y, button, modifiers)[ソース]

Event called when the mouse is moved with buttons pressed

on_request_close(*largs, **kwargs)[ソース]

Event called before we close the window. If a bound function returns True, the window will not be closed. If the the event is triggered because of the keyboard escape key, the keyword argument source is dispatched along with a value of keyboard to the bound functions.

警告

When the bound function returns True the window will not be closed, so use with care because the user would not be able to close the program, even if the red X is clicked.

on_resize(width, height)[ソース]

Event called when the window is resized.

on_restore(*largs)[ソース]

Event called when the window is restored.

バージョン 1.10.0 で追加.

注釈

This feature requires the SDL2 window provider.

on_rotate(rotation)[ソース]

Event called when the screen has been rotated.

on_show(*largs)[ソース]

Event called when the window is shown.

バージョン 1.10.0 で追加.

注釈

This feature requires the SDL2 window provider.

on_textinput(text)[ソース]

Event called when text: i.e. alpha numeric non control keys or set of keys is entered. As it is not guaranteed whether we get one character or multiple ones, this event supports handling multiple characters.

バージョン 1.9.0 で追加.

on_touch_down(touch)[ソース]

Event called when a touch down event is initiated.

バージョン 1.9.0 で変更: The touch pos is now transformed to window coordinates before this method is called. Before, the touch pos coordinate would be (0, 0) when this method was called.

on_touch_move(touch)[ソース]

Event called when a touch event moves (changes location).

バージョン 1.9.0 で変更: The touch pos is now transformed to window coordinates before this method is called. Before, the touch pos coordinate would be (0, 0) when this method was called.

on_touch_up(touch)[ソース]

Event called when a touch event is released (terminated).

バージョン 1.9.0 で変更: The touch pos is now transformed to window coordinates before this method is called. Before, the touch pos coordinate would be (0, 0) when this method was called.

parent

Parent of this window.

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

raise_window()[ソース]

Raise the window. This method should be used on desktop platforms only.

バージョン 1.9.1 で追加.

注釈

This feature requires the SDL2 window provider and is currently only supported on desktop platforms.

release_all_keyboards()[ソース]

バージョン 1.0.8 で追加.

This will ensure that no virtual keyboard / system keyboard is requested. All instances will be closed.

release_keyboard(target=None)[ソース]

バージョン 1.0.4 で追加.

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

remove_widget(widget)[ソース]

Remove a widget from a window

request_keyboard(callback, target, input_type='text')[ソース]

バージョン 1.0.4 で追加.

Internal widget method to request the keyboard. This method is rarely required by the end-user as it is handled automatically by the TextInput. We expose it in case you want to handle the keyboard manually for unique input scenarios.

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

Parameters:
callback: func

Callback that will be called when the keyboard is closed. This can be because somebody else requested the keyboard or the user closed it.

target: Widget

Attach the keyboard to the specified target. This should be the widget that requested the keyboard. Ensure you have a different target attached to each keyboard if you’re working in a multi user mode.

バージョン 1.0.8 で追加.

input_type: string

Choose the type of soft keyboard to request. Can be one of ‘text’, ‘number’, ‘url’, ‘mail’, ‘datetime’, ‘tel’, ‘address’.

注釈

input_type is currently only honored on mobile devices.

バージョン 1.8.0 で追加.

Return:

An instance of Keyboard containing the callback, target, and if the configuration allows it, a VKeyboard instance attached as a .widget property.

注釈

The behavior of this function is heavily influenced by the current keyboard_mode. Please see the Config’s configuration tokens section for more information.

restore()[ソース]

Restores the size and position of a maximized or minimized window. This method should be used on desktop platforms only.

バージョン 1.9.0 で追加.

注釈

This feature requires the SDL2 window provider and is currently only supported on desktop platforms.

rotation

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

バージョン 1.0.9 で追加.

rotation is an AliasProperty.

screenshot(name='screenshot{:04d}.png')[ソース]

Save the actual displayed image in a file

set_icon(filename)[ソース]

Set the icon of the window.

バージョン 1.0.5 で追加.

set_title(title)[ソース]

Set the window title.

バージョン 1.0.5 で追加.

set_vkeyboard_class(cls)[ソース]

バージョン 1.0.8 で追加.

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

show()[ソース]

Shows the window. This method should be used on desktop platforms only.

バージョン 1.9.0 で追加.

注釈

This feature requires the SDL2 window provider and is currently only supported on desktop platforms.

show_cursor

Set whether or not the cursor is shown on the window.

バージョン 1.9.1 で追加.

show_cursor is a BooleanProperty and defaults to True.

size

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

バージョン 1.0.9 で追加.

size is an AliasProperty.

softinput_mode

This specifies the behavior of window contents on display of the soft keyboard on mobile platforms. It can be one of ‘’, ‘pan’, ‘scale’, ‘resize’ or ‘below_target’. Their effects are listed below.

Value Effect
‘’ The main window is left as is, allowing you to use the keyboard_height to manage the window contents manually.
‘pan’ The main window pans, moving the bottom part of the window to be always on top of the keyboard.
‘resize’ The window is resized and the contents scaled to fit the remaining space.
‘below_target’ The window pans so that the current target TextInput widget requesting the keyboard is presented just above the soft keyboard.

softinput_mode is an OptionProperty and defaults to None.

注釈

The resize option does not currently work with SDL2 on Android.

バージョン 1.9.0 で追加.

バージョン 1.9.1 で変更: The ‘below_target’ option was added.

system_size

Real size of the window ignoring rotation.

バージョン 1.0.9 で追加.

system_size is an AliasProperty.

toggle_fullscreen()[ソース]

Toggle between fullscreen and windowed mode.

バージョン 1.9.0 で撤廃: Use fullscreen instead.

top

Top position of the window.

注釈

It’s an SDL2 property with [0, 0] in the top-left corner.

バージョン 1.10.0 で変更: top is now an AliasProperty

バージョン 1.9.1 で追加.

top is an AliasProperty and defaults to position set in Config.

ungrab_mouse()[ソース]

Ungrab mouse

バージョン 1.10.0 で追加.

注釈

This feature requires the SDL2 window provider.

width

Rotated window width.

width is a read-only AliasProperty.