简体   繁体   中英

Kivy importing Python variables into inline KV

I've been banging my head off this particular wall for days now. None of the relevant articles here have worked, so I really need to know where I'm going wrong. The relevant sections of code are:

    def print_scb(myscb):
        print(myscb)
        scb_string = ''
        scb_list = [ 'Combat' , 'Communication' , 'Manipulation' , 'Mental' , 'Perception', 'Physical' ]
        for s in scb_list:
            zz = str(myscb[s])
            scb_string = scb_string + s + " " + zz + "% "
        return scb_string

And the inline KV section is:

<CharacterSheet>:
    BoxLayout:
        orientation: 'vertical'
        Label:
            text:'Stats'
        Label:
            text: app.print_stats()
        Label:
            text:'Rolls'
        Label:
            text: app.print_rolls()
        Label:
            text:'SCM'
        Label:
            text: app.print_scm()
        Label:
            text:'SCB'
        Label:
            text: app.print_scb()
        Button:
            text:'Quit'
            on_press: sys.exit()
        Button:
            text:'Back to Start'
            on_press: root.manager.current = 'welcome'

""")

The program crashes with:

 kivy.lang.BuilderException: Parser: File "<inline>", line 114:
 ...
     112:            text:'SCB'
     113:        Label:
 >>  114:            text: app.print_scb()
     115:        Button:
     116:            text:'Quit'
  ...
 BuilderException: Parser: File "<inline>", line 114:
 ...
     112:            text:'SCB'
     113:        Label:
 >>  114:            text: app.print_scb()
     115:        Button:
     116:            text:'Quit'
 ...
 AttributeError: 'NoneType' object has no attribute 'bind'
   File "/usr/lib64/python3.4/site-packages/kivy/lang.py", line 1742, in   create_handler
     return eval(value, idmap), bound_list
   File "<string>", line 114, in <module>
   File "/usr/lib64/python3.4/site-packages/kivy/lang.py", line 935, in __getattribute__
 object.__getattribute__(self, '_ensure_app')()
   File "/usr/lib64/python3.4/site-packages/kivy/lang.py", line 930, in _ensure_app
     app.bind(on_stop=lambda instance:

   File "/usr/lib64/python3.4/site-packages/kivy/lang.py", line 2115, in _apply_rule
     rctx['ids'])
   File "/usr/lib64/python3.4/site-packages/kivy/lang.py", line 1747, in create_handler
 cause=tb)

The whole code has been posted to dpaste at BrpGui.py

The print_scb() functions returns a string.

Any ideas where I'm going wrong? Colin

As you mentioned in the comments.

print_scb is not a global function. It is defined in the CharacterSheet(Screen): class.

When you defined the function you forgor self.

So instead of def print_scb(myscb): it shall be def print_scb(self, myscb):

Also in your kv file, dont use app.print_scb() but root.print_scb() Since it belongs to that class.

But since your function wants a parameter, it will throw an error. So you need to call it with a parameter. like this root.print_scb(some_variable)

From the docs https://kivy.org/docs/guide/lang.html -

There are three keywords specific to Kv language:

app: always refers to the instance of your application.
root: refers to the base widget/template in the current rule
self: always refer to the current widget

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM