从另一个小部件更改Kivy小部件属性 [英] Changing Kivy widget attribute from another widget

查看:65
本文介绍了从另一个小部件更改Kivy小部件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Screen小部件,其中的一个按钮(标识为display_name)具有文本属性.当我按下该按钮时,将显示一个模态,该模态具有一个文本输入"窗口小部件和一个按钮"窗口小部件.我想在模式的文本输入"小部件中输入文本,并在按下模式的按钮时在屏幕"小部件的按钮中显示该文本.我很难从模式中更改屏幕"按钮的文本属性.我该怎么做呢?我已经尝试了下面的代码,但收到此错误:

AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'text'

kv代码

<ProfileScreen>:
    display_name: display_name
    GeneralBoxLayout:
        BoxLayout:
            GridLayout:
                BackButton:
                    on_release: root.manager.current = 'home'
                    size_hint: (0.1,1.0)
                Image:
                    size_hint: (0.1,1.0)
                Image:
                    source: 'img/logo4.png'
                    size_hint: (0.60,1.0)
                Image:
                    size_hint: (0.05,1.0)
                MenuButton:
                    size_hint: (0.15,1.0)
                    on_release: app.build_profile_screen(); root.manager.current = 'profile'
        BoxLayout:
            ScrollView:
                size_hint: (1,.93)
                GridLayout:
                    BoxLayout:
                        Button:
                            id: display_name
                            font_size: '14sp'
                            text_size: (290, 40)
                            halign: 'left'
                            background_normal: 'img/white_button1.png'
                            background_down: 'img/white_button1.png'
                            border: 20,20,20,20
                            markup: True
                            on_release: app.update_display_name_popup()
<UpdateDisplayNamePopup>:
    updated_display_name: updated_display_name
    size_hint: .5, .3
    BoxLayout:
        padding: [10,10,10,10]
        orientation: 'vertical'
        TextInput:
            id: updated_display_name
            hint_text: 'Enter New Display Name'
        Button:
            font_size: '14sp'
            text: 'Update Display Name'
            background_normal: 'img/green_button5.png'
            background_down: 'img/green_button5.png'
            size_hint_y: None
            on_release: root.update_display_name(); root.dismiss()

main.py代码

from kivy.properties import ObjectProperty

class ProfileScreen(Screen):
    display_name = ObjectProperty(None)


class UpdateDisplayNamePopup(ModalView):
    def update_display_name(self):
        ProfileScreen.display_name.text = self.updated_display_name.text

解决方案

我解决了这一问题,方法是将update_display_name方法移至ProfileScreen类,从模式的按钮中调用它,然后将update_display_name.text传递给该方法,如下所示: /p>

main.py

class ProfileScreen(Screen):
    def update_display_name(self, updated_display_name):
        self.display_name.text = updated_display_name

kv文件

Button:
    on_release: app.root.get_screen('profile').update_display_name(updated_display_name.text); root.dismiss()

I have a Screen widget with a button (with id: display_name) that has a text attribute. When I press that button, a modal is displayed that has a Text Input widget and a Button widget. I want to enter text into the modal's Text Input widget and display that text in the Screen widget's button when I press the modal's button. I am having difficulty changing the Screen button's text attribute from the modal. How do I do this? I've tried the code below, but get this error:

AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'text'

kv code

<ProfileScreen>:
    display_name: display_name
    GeneralBoxLayout:
        BoxLayout:
            GridLayout:
                BackButton:
                    on_release: root.manager.current = 'home'
                    size_hint: (0.1,1.0)
                Image:
                    size_hint: (0.1,1.0)
                Image:
                    source: 'img/logo4.png'
                    size_hint: (0.60,1.0)
                Image:
                    size_hint: (0.05,1.0)
                MenuButton:
                    size_hint: (0.15,1.0)
                    on_release: app.build_profile_screen(); root.manager.current = 'profile'
        BoxLayout:
            ScrollView:
                size_hint: (1,.93)
                GridLayout:
                    BoxLayout:
                        Button:
                            id: display_name
                            font_size: '14sp'
                            text_size: (290, 40)
                            halign: 'left'
                            background_normal: 'img/white_button1.png'
                            background_down: 'img/white_button1.png'
                            border: 20,20,20,20
                            markup: True
                            on_release: app.update_display_name_popup()
<UpdateDisplayNamePopup>:
    updated_display_name: updated_display_name
    size_hint: .5, .3
    BoxLayout:
        padding: [10,10,10,10]
        orientation: 'vertical'
        TextInput:
            id: updated_display_name
            hint_text: 'Enter New Display Name'
        Button:
            font_size: '14sp'
            text: 'Update Display Name'
            background_normal: 'img/green_button5.png'
            background_down: 'img/green_button5.png'
            size_hint_y: None
            on_release: root.update_display_name(); root.dismiss()

main.py code

from kivy.properties import ObjectProperty

class ProfileScreen(Screen):
    display_name = ObjectProperty(None)


class UpdateDisplayNamePopup(ModalView):
    def update_display_name(self):
        ProfileScreen.display_name.text = self.updated_display_name.text

解决方案

I solved this by moving the update_display_name method to the ProfileScreen class, calling it from the modal's button, and passing updated_display_name.text to the method, as follows:

main.py

class ProfileScreen(Screen):
    def update_display_name(self, updated_display_name):
        self.display_name.text = updated_display_name

kv file

Button:
    on_release: app.root.get_screen('profile').update_display_name(updated_display_name.text); root.dismiss()

这篇关于从另一个小部件更改Kivy小部件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆