Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

python - Kivy: Get widgets ids and accessing widgets by unique property

I'm new to Kivy and I have this little demo snippet that demonstrates my problem:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder


class KivyGuiApp(App):
    def build(self):
        return root_widget


class MyBox(BoxLayout):
    def print_ids(self, *args):
        print("
ids:")
        for widget in self.walk():
            print("{} -> {}".format(widget, widget.id))
    def print_names(self, *args):
        print("
names:")
        for widget in self.walk():
            print("{} -> {}".format(widget, widget.name))



root_widget = Builder.load_string("""
MyBox:
    id: screen_manager
    name: 'screen_manager'

    SimpleLayout:
        id: simple_layout
        name: 'simple_layout'


<SimpleLayout@BoxLayout>:
    id: simple_layout_rule
    name: 'simple_layout_rule'
    Button:
        id: button_ids
        name: 'button_ids'
        text: 'print ids to console'
        on_release: app.root.print_ids()
    Button:
        id: button_names
        name: 'button_names'
        text: 'print names to console'
        on_release: app.root.print_names()
""")


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

So When you run the code there will be two buttons:

  • first to walk over all widgets and print their names (which works as expected - returns 'name' for each widget),
  • second button to walk over all widgets as well, but instead of names print their ids (which doesn't work - returns None for each id).

My questions are:

  1. Isn't 'id' a property just like 'name'?
  2. How can I access id for each widget from python side?

Bonus question:

  1. Can I access a widget "globally" by it's id (assuming all id's are unique)? By "globally" I mean for example accessing (in code above) ids from 'MyBox:' widget without referencing parent-child, but just by ids (or maybe any other property that would be unique to every widget). I was thinking of creating a dictionary { id : widget object } of all widgets for easy access, unless there is another way I'm not aware of? To emphasize - I'm trying to avoid referencing by children-parent way (which is rather messy when you want to change your widget tree later) and I'd like to generate widgets in .kv language. So what would be the best way to do that?

EDIT:

So here's the easiest way I could think of to reference widgets by unique "global" id.

First I created a class which will be inherited by my App class:

class KivyWidgetInterface():
    ''' interface for  global widget access '''
    global_widgets = {}
    def register_widget(self, widget_object):
        ''' registers widget only if it has unique gid '''
        if widget_object.gid not in self.global_widgets:
            self.global_widgets[widget_object.gid] = widget_object

    def get_widget(self, widget_gid):
        ''' returns widget if it is registered '''
        if widget_gid in self.global_widgets:
            return self.global_widgets[widget_gid]
        else:
            return None

So the widget will be registered only if it has gid - a widget class variable - and it is unique. This way I can store only vital widgets in this dict. Also, it is easily accessible from both .kv and python side.

Now i create the gid variables and register them to the dict in .kv:

<PickDirectory>:
    gid: 'pick_directory'
    on_pos: app.register_widget(self)
    on_selection: app.get_widget('filelist').some_func()
<FileListView>:
    gid: 'filelist'
    on_pos: app.register_widget(self)   
    Button:
        name: 'not important'
    Button:
        gid: 'tab_browse_button1'
        on_pos: app.register_widget(self)

Thing that bothers me actually is that I register my widgets in this "global" dictionary with the 'on_pos' event... which I really don't like, but I was unable to find any reliable way of calling a register method after the widget was initialized (on_pos is called right after the init phase, when widget is positioned and later very rarely, so... seemed like the least bothering way of doing that with my knowledge of kivy api, the order widgets are initialized with .kv language etc; so if there is a better way I'd be very grafeul for any pointers).

Anyhow, this way I can easy bind any event to any method in any class right from the .kv

One thing to remember is that the gid (global id's) need to be unique globally, but I don't find that any more disturbing than keeping ids unique locally(which could be equally or even more confusing for me). And as I said - I'd like to register the widgets differently, but I couldn't find any other reliable way of doing this (and I don't find Clock to be reliable for such things).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Actually, no. name in your widgets is a variable and id is just a widget reference, weakref according to the docs. Maybe python docs will help you understand how it works. What you did was printing id, not a variable "id" inside a widget.

In the kivy docs it's explained that after kv is parsed, ids are collected into a ObservableDict. The id works like a python dict key id:Widget but only if accessed through the dictionary(ids). I think kv parser just takes all ids into dict and works only with the dict it creates.

Button:
    id: test
    text: 'self.id'
#or
Button:
    id: 'test'
    text: 'self.id'

Even if it's written like a string, nothing changes. So I expect parser to behave like this: grabs whatever whole word is after id:, turns to a string, appends to a ids dictionary <id_string>:Widget_weakref, forgets about id in your .kv or just ignores it if it works with .kv again. Therefore, when id is called directly(not dictionary-like d[key]), it behaves like an empty/None variable. I hope I'm right.


To answer the second and the third one:

If you mean accessing widget by id in MyBox directly for example SimpleLayout, then yes.

python class:

self.ids.simple_layout

kv MyBox rule:

MyBox:
    id: screen_manager
    name: 'screen_manager'
    BoxLayout:
        Label:
            id: my_label
            text: 'test'
        Button:
            text: 'button'
            on_release: self.text = root.ids.my_label.text

However, to access all widgets by their ids in way like python globals work, it's not possible. You'd need to access class/widget first and then its ids dictionary


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...