如何将一个变量从一个类传递到另一个类?

我正在使用 Kivy 和 Kivymd。class Admin我在和之间传递变量时遇到问题class EditArticle。我需要my_string从 Admin 传递到 EditArticle。我正在尝试这样做,但得到一个空字符串。所以,在class Admin我有my_string. 然后,在edit_articleclass Adminmy_string. 然后我试图edit以 class EditArticle. 但它一直是空的。我真的想不通。

  1. 如果您运行我的代码,您将点击顶部菜单admin

  2. 然后点击任何mdchip

  3. 然后单击对话窗口中的 etit 按钮。

  4. 然后点击按钮获取my_string(但它总是空的)。

这是我的 App.py

我的应用程序.kv


 <WindowManager>

    Container:

        id: scr_1

        name: 'container'

    Detail:

        id: scr_2

        name: 'detail'


    Admin:

        id: scr_3

        name: 'admin'

    EditArticle:

        id: scr_4

        name: 'edit-article'

        var: scr_3.my_string # <---------


    ResultSearch:

        id: scr_5

        name: 'result-search'

    UserSettings:

        id: scr_6

        name: 'settings'


<Admin>:

    BoxLayout:

        id: boxlayout_1

        orientation: 'vertical'


        MDToolbar:

            pos_hint: {'top': 1}

            title: 'Admin Blog'

            left_action_items: [["arrow-left", lambda x: app.callback()]]


        ScrollView:

            MDStackLayout:

                adaptive_height: True

                padding: 10

                spacing: dp(5)

                id: box


<EditArticle>

    MDToolbar:

        title: 'Admin Blog'

    MDLabel:

        text: str(root.var)

    MDRaisedButton:

        text: 'click me to see a variable in console'

        pos_hint: {'center_x': .5, 'center_y': .5}

        on_release: root.edit(root)


<MyToolbar@CustomToolbar>:

    size_hint_y: None

    height: self.theme_cls.standard_increment

    padding: "25dp"

    spacing: "12dp"


    MDLabel:

        id: label

        text: 'Blog'

        font_style: 'H6'

        theme_text_color: "Custom"

        text_color: 1,1,1,1


    Widget:


    MDIconButton:

        id: button_2

        icon: "dots-vertical"

        pos_hint: {"center_y": .5}

        theme_text_color: "Custom"

        text_color: 1,1,1,1

        on_release: root.menu.open()


<Container>




狐的传说
浏览 234回答 3
3回答

温温酱

根本原因:在您的应用程序中,有两个class Admin实例化的实例。WindowManager()当您在方法中实例化根时,第一个实例在 kv 文件中实例化build。片段:<WindowManager>:&nbsp; &nbsp; ...&nbsp; &nbsp; Admin:&nbsp; &nbsp; &nbsp; &nbsp; id: scr_3&nbsp; &nbsp; &nbsp; &nbsp; name: 'admin'&nbsp; &nbsp; ...第二个实例在class BlogChip.片段:class BlogChip(MDChip):&nbsp; &nbsp; get_admin = Admin()&nbsp; &nbsp; ...类属性my_string在 的第二个实例中更新class Admin。但是屏幕正在引用由根实例化的实例。因此,应用程序显示空字符串。解决方案:该解决方案需要更改 kv 和 python 文件。kv文件:替换 callback: root.get_admin.get_article 为 callback: app.root.ids.scr_3.get_article片段:<BlogChip>&nbsp; &nbsp; label: root.title&nbsp; &nbsp; icon: ''&nbsp; &nbsp; callback: app.root.ids.scr_3.get_article蟒蛇文件:删除get_admin = Admin()在class BlogChip(MDChip):片段:class BlogChip(MDChip):&nbsp; &nbsp; id = NumericProperty()&nbsp; &nbsp; title = StringProperty()&nbsp; &nbsp; body = StringProperty()在方法中,edit()初始化类属性,var片段:class EditArticle(Screen):&nbsp; &nbsp; var = StringProperty()&nbsp; &nbsp; def edit(self, instance):&nbsp; &nbsp; &nbsp; &nbsp; # Each screen has by default a property manager that&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; # gives you the instance of the ScreenManager used.&nbsp; &nbsp; &nbsp; &nbsp; self.var = self.manager.ids.scr_3.my_string&nbsp; &nbsp; &nbsp; &nbsp; print(self.var, ' << it is going to be <my_string> from Admin')输出:

隔江千里

我真的不明白你在你的代码中做了什么?你为什么不直接看看Admin.my_string你的内部EditArticle class?如果这不起作用,您是否尝试getter为您的my_string属性创建一个?抱歉,我不知道它是如何StringProperty工作的……但是你不能在 中做类似下面的事情吗Agent class?def&nbsp;get_my_string():&nbsp; &nbsp;&nbsp;&nbsp;return&nbsp;my_string.value()然后你就打电话Agent.get_my_string()给你的EditArticle班级。

茅侃侃

这是我的问题的解决方案。我必须添加EditArticle.my_string = self.my_string这样class Admin的内容:def edit_article(self, instance):&nbsp; &nbsp; EditArticle.my_string = self.my_string&nbsp; &nbsp; App.get_running_app().window_manager.current = 'edit-article'&nbsp; &nbsp; self.dialog_get_article.dismiss()在此之后我可以得到它class EditArticle()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python