📜  动态分配 ids kivy - 任何代码示例

📅  最后修改于: 2022-03-11 14:56:21.449000             🧑  作者: Mango

代码示例1
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
import weakref


class MyLayout(Widget):
    def use_weakref_id_to_replace_text(self, *args):
        self.ids.AddUserTextBox.text = "shotta"
        print("released and renamed the text to shotta")

    def add_textinpt_with_weak_ref_dyn_id(self, *args):
        print('Pressed and added text input box')

        textinput = TextInput(pos=(380,380),text='123')
        
        self.add_widget(textinput)

        # We'll use a weak ref to add our dynamic id 
        self.ids['AddUserTextBox'] = weakref.ref(textinput)

class MdApp(App):
    def build(self):
        root = MyLayout()
        Btn = Button(size=(250,250), pos=(100,100),text="Dynamic id assign n use id to rename")
        Btn.bind(on_release=root.use_weakref_id_to_replace_text,on_press=root.add_textinpt_with_weak_ref_dyn_id)
        root.add_widget(Btn)
        return root 


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