kivy lang: why id not recognized in dynamic class?

31 views
Skip to first unread message

Bob Gailer

unread,
Apr 19, 2017, 3:34:34 PM4/19/17
to Kivy users support
Following works:
#:import call subprocess.call
<Btn@Button>:
size_hint: (0.1, 0.1)
StackLayout:
orientation: 'lr-tb'
Btn:
text: 'list'
on_press: fc.view_mode = self.text
Btn:
text: 'icon'
on_press: fc.view_mode = self.text
Label:
size_hint: .8, .1
FileChooser:
id: fc
path: "C:\python35\share\kivy-examples"
filters: ['*.py']
on_submit: call('c:\python35\python ' + self.selection[0])
FileChooserListLayout
FileChooserIconLayout
 If I move the on_press lines into the dynamic class it fails when I press a Btn: NameError: name 'fc' is not defined.
How can I fix that?

Fari Tigar

unread,
Apr 19, 2017, 5:06:52 PM4/19/17
to Kivy users support
Properties are used with an accessors like self, root, or app, you can't just use fc, this would be a local variable.
I would use for every main widget an id and make it available at the root level, like so:
 
Think about refactoring this to a method:
  • you could do validations, exceptions etc.
  • debugging, testing is much easier with python files
on_submit: call('c:\python35\python ' + self.selection[0])

on_submit:root.my_method(self.selection)

def my_method(self,selection):
..... 

#:import call subprocess.call
<Btn@Button>:
    fc
:fc
    size_hint: (0.1, 0.1)
StackLayout:
    orientation: 'lr-tb'
    Btn:
        text: 'list'
        on_press: root.fc.view_mode = self.text
    Btn:
        text: 'icon'
        on_press: root.fc.view_mode = self.text
    Label:
        size_hint: .8, .1
    FileChooser:
        id: fc
        path: "C:\python35\share\kivy-examples"
        filters: ['*.py']
        on_submit: call('c:\python35\python ' + self.selection[0])
        FileChooserListLayout
        FileChooserIconLayout

bob gailer

unread,
Apr 20, 2017, 6:05:38 PM4/20/17
to kivy-...@googlegroups.com
On 4/19/2017 5:06 PM, Fari Tigar wrote:
> Properties are used with an accessors like *self, root, or app*, you
> can't just use fc, this would be a local variable.
> I would use for every main widget an id and make it available at the
> root level, like so:
> **
> Think about refactoring this to a method:
>
> * *you could do validations, exceptions etc.*
> * *debugging, testing is much easier with python files*
>
> on_submit:call('c:\python35\python '+self.selection[0])
>
> on_submit:root.my_method(self.selection)
>
> def my_method(self,selection):
> .....
>
> |
> #:import call subprocess.call<Btn@Button>: fc:fc
> size_hint:(0.1,0.1)StackLayout: orientation:'lr-tb'Btn:
> text:'list' on_press:root.fc.view_mode =self.text Btn:
> text:'icon' on_press:root.fc.view_mode =self.text Label:
> size_hint:.8,.1FileChooser: id:fc
> path:"C:\python35\share\kivy-examples" filters:['*.py']
> on_submit:call('c:\python35\python
> '+self.selection[0])FileChooserListLayoutFileChooserIconLayout
> |
I don't think you understood my question. My goal is to move the 2
on_press: lines inside the Btn class. How do I refer to the fc id from
within the Btn class?

#:import call subprocess.call <Btn@Button>: size_hint: (0.1, 0.1)
on_press: fc.view_mode = self.text

I also don't understand fc: fc. What is that supposed to mean/do?
> On Wednesday, April 19, 2017 at 9:34:34 PM UTC+2, Bob Gailer wrote:
>
> Following works:
>
> #:import call subprocess.call <Btn@Button>: size_hint: (0.1,
> 0.1) StackLayout: orientation: 'lr-tb' Btn: text: 'list'
> on_press: fc.view_mode = self.text Btn: text: 'icon' on_press:
> fc.view_mode = self.text Label: size_hint: .8, .1 FileChooser:
> id: fc path: "C:\python35\share\kivy-examples" filters:
> ['*.py'] on_submit: call('c:\python35\python ' +
> self.selection[0]) FileChooserListLayout FileChooserIconLayout
>
> If I move the on_press lines into the dynamic class it fails when
> I press a Btn: NameError: name 'fc' is not defined. How can I fix
> that?
>
> -- You received this message because you are subscribed to a topic in
> the Google Groups "Kivy users support" group. To unsubscribe from this
> topic, visit
> https://groups.google.com/d/topic/kivy-users/coraR7tRzBQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> kivy-users+...@googlegroups.com
> <mailto:kivy-users+...@googlegroups.com>. For more options,
> visit https://groups.google.com/d/optout.

-- Image and video hosting by TinyPic
Reply all
Reply to author
Forward
0 new messages