AttributeError: 'myApp' object has no attribute 'to_window'

22 views
Skip to first unread message

Frédéric

unread,
Jun 19, 2025, 1:40:06 PMJun 19
to Kivy users support
Hi everyone,

For a desktop application, I wanted to provide a way to have a default button, fired when I press enter.
The solution I found works perfectly, but has a strange (for me) effect: If I try to move the window, the program crash with the error :
AttributeError: 'myApp' object has no attribute 'to_window'

Here is a minimal exemple

file pbme_to_window.py
from kivy.app import App
from kivy.app import Builder
from kivy.core.window import Window

class myApp(App):
    def __init__(self, **kwargs):
        super(myApp, 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):
        self._keyboard.unbind(on_key_down=self._on_keyboard_down)
        self._keyboard = None

    def build(self):
        kv = Builder.load_file('pbme_to_window.kv')
        return kv

    def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
        if keycode[1] == 'enter':
           saisie()

    def saisie(self):
        print('Le bouton a été pressé')

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

file pbme_to_window.kv
BoxLayout:
    orientation: "vertical"
    BoxLayout:
        size_hint: 1, .1
        orientation: "horizontal"
        Label:
            size_hint : 3, 1
            text: "Titre"
    BoxLayout:
        orientation: "horizontal"
    BoxLayout:
        orientation: "horizontal"
        size_hint: 1, .2
        BoxLayout:
            orientation: "vertical"
            size_hint: (0.2, 1)
            Button:
                size_hint_x: 0.25
                pos_hint: {'center_x':0.5, 'center_y': 0.5}
                text: "Bouton"
                on_release: app.saisie()

If i delete the 2 lines keyboard from __init__ I can move the window without problem...

Frederic

ELLIOT GARBUS

unread,
Jun 19, 2025, 4:37:11 PMJun 19
to kivy-...@googlegroups.com


I don’t have a computer right now to test this but I suspect the problem is:

self._keyboard = Window.request_keyboard(self._keyboard_closed, self)

The last parameter should be a Widget, in this context self is the instance of App. Replace self with self.root

I also suggest you move this code from __init__() to on_start(). The root widget is set when build returns. 

Let me know if that helps. 


Sent from my iPhone

On Jun 19, 2025, at 1:40 PM, Frédéric <freder...@gmail.com> wrote:

Hi everyone,
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/kivy-users/78f1e78a-d141-4ca2-aba3-9232cb3018cdn%40googlegroups.com.

Frédéric

unread,
Jun 19, 2025, 5:50:45 PMJun 19
to Kivy users support
Thanks Elliot,
It works perfectly now!

You're amazing...


Frederic
Reply all
Reply to author
Forward
0 new messages