DGDays
DGDays

Reputation: 76

How to correctly hang an event in Kivy?

I try to make an application on Kivy like this:

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window


class Program(App):
    def build(self):
        Window.clearcolor = (184/255.0, 228/255.0, 107/255.0, 255/255.0)
        Activity = f'''
FloatLayout:
    
    Button:
        text: "Check product!"
        pos: 300, 250
        size_hint: .2, .2
        background_normal: ''
        background_color: 228/255.0, 202/255.0, 31/255.0, 1
        on_press: {self}.test()

        '''
        return Builder.load_string(Activity)
    def test(self):
        return("test is work!")
   
if __name__ in ('__main__', '__android__'):
    Program().run()

But unfortunately, it gives an error:

[INFO   ] [Logger      ] Record log in C:\Users\olegp\.kivy\logs\kivy_20-12-07_13.txt
[INFO   ] [deps        ] Successfully imported "kivy_deps.angle" 0.3.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.glew" 0.3.0
[INFO   ] [deps        ] Successfully imported "kivy_deps.sdl2" 0.3.1
[INFO   ] [Kivy        ] v2.0.0rc4, git-b714003, 20201206
[INFO   ] [Kivy        ] Installed at "C:\Python380\lib\site-packages\kivy\__init__.py"
[INFO   ] [Python      ] v3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)]
[INFO   ] [Python      ] Interpreter at "C:\Python380\python.exe"
[INFO   ] [Factory     ] 186 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL" graphics system
[INFO   ] [GL          ] GLEW initialization succeeded
[INFO   ] [GL          ] Backend used <glew>
[INFO   ] [GL          ] OpenGL version <b'4.6.0 - Build 26.20.100.7870'>
[INFO   ] [GL          ] OpenGL vendor <b'Intel'>
[INFO   ] [GL          ] OpenGL renderer <b'Intel(R) UHD Graphics 620'>
[INFO   ] [GL          ] OpenGL parsed version: 4, 6
[INFO   ] [GL          ] Shading version <b'4.60 - Build 26.20.100.7870'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <32>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
 Traceback (most recent call last):
   File "c:/Users/olegp/OneDrive/Рабочий стол/ki.py", line 26, in <module>
     Program().run()
   File "C:\Python380\lib\site-packages\kivy\app.py", line 949, in run
     self._run_prepare()
   File "C:\Python380\lib\site-packages\kivy\app.py", line 919, in _run_prepare
     root = self.build()
   File "c:/Users/olegp/OneDrive/Рабочий стол/ki.py", line 21, in build
     return Builder.load_string(Activity)
   File "C:\Python380\lib\site-packages\kivy\lang\builder.py", line 373, in load_string
     parser = Parser(content=string, filename=fn)
   File "C:\Python380\lib\site-packages\kivy\lang\parser.py", line 402, in __init__
     self.parse(content)
   File "C:\Python380\lib\site-packages\kivy\lang\parser.py", line 515, in parse
     rule.precompile()
   File "C:\Python380\lib\site-packages\kivy\lang\parser.py", line 264, in precompile
     x.precompile()
   File "C:\Python380\lib\site-packages\kivy\lang\parser.py", line 262, in precompile
     x.precompile()
   File "C:\Python380\lib\site-packages\kivy\lang\parser.py", line 187, in precompile
     self.co_value = compile(value, self.ctx.filename or '<string>', mode)
   File "<string>", line 10
     <__main__.Program object at 0x0326F8B0>.test()
     ^
 SyntaxError: invalid syntax

I need to hang the test method on a button so that when I click on it, it works, please tell me how to do it right? It is necessary to add this method through a line, at least preferably

PS Sorry if my English is sloppy😅

Upvotes: 1

Views: 40

Answers (1)

Fadi Abu Raid
Fadi Abu Raid

Reputation: 841

I assume you want to bind your button to test method to print "test is work!". This is how it is done in Kivy.

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window

Activity = f'''
FloatLayout:
    Button:
        text: "Check product!"
        pos: 300, 250
        size_hint: .2, .2
        background_normal: ''
        background_color: 228/255.0, 202/255.0, 31/255.0, 1
        on_press: app.test()

'''

class Program(App):
    def build(self):
        Window.clearcolor = (184/255.0, 228/255.0, 107/255.0, 255/255.0)
        return Builder.load_string(Activity)

    def test(self):
        print("test is work!")

Program().run()

Upvotes: 1

Related Questions