如何在 python 文件中设置小部件的 ID?

Posted

技术标签:

【中文标题】如何在 python 文件中设置小部件的 ID?【英文标题】:How do I set Id of widget inside python file? 【发布时间】:2021-05-23 04:22:04 【问题描述】:

我正在尝试从另一个屏幕更改一个屏幕内的 MDLabel 的文本。我可以引用屏幕本身,但由于我没有使用 kv 语言,我无法设置我试图引用的 MDLabel 的 Id。

有没有办法在 Python 中设置 MDLabel 的 Id 并将其添加到它所在屏幕的 self.ids 中? -要么。还有其他方法可以引用另一个屏幕的小部件吗?

我的代码:

def main():
    class HomeScreen(Screen, GridLayout, MDApp):
        def __init__(self, **kwargs):
            super(HomeScreen, self).__init__(**kwargs)

            self.add_widget(MDRaisedButton(text='Read', size_hint=(.3, .2), font_size='30sp', on_press=lambda x:self.changerReadMail()))

        def changerReadMail(self, *args):
            self.manager.transition.direction = 'right'
            # It's here I want to change the text of label inside the ReadMail class.
            self.manager.current = 'read'

    class ReadMail(Screen, FloatLayout, MDApp):
        def __init__(self, **kwargs):
            super(ReadMail, self).__init__(**kwargs)
            label = (MDLabel(text='hej'))
            self.add_widget(label)
            self.add_widget(MDFillRoundFlatButton(text='Back', font_size='20sp', size_hint=(.1,.1), pos_hint='x':.01, 'y':.02, on_press=lambda x:self.changerInbox()))   

        def changerInbox(self, *args):
            self.manager.transition.direction = 'left'
            self.manager.current = 'home'

    class KivyApp(MDApp): 
        def build(self):
            Window.size = (1000, 600)
            self.sm = ScreenManager()
           
            self.sm.add_widget(HomeScreen(name='home'))
            self.sm.add_widget(ReadMail(name='read'))

            self.sm.current = 'home'
            return self.sm
        
    KivyApp().run()


if __name__ == '__main__':
    main()

【问题讨论】:

【参考方案1】:

我解决了这个问题!

这是我的解决方案(参见注释为“这是新行”的行):

import weakref #This is a new line

def main():
    class HomeScreen(Screen, GridLayout, MDApp):
        def __init__(self, **kwargs):
            super(HomeScreen, self).__init__(**kwargs)

            self.add_widget(MDRaisedButton(text='Read', size_hint=(.3, .2), font_size='30sp', on_press=lambda x:self.changerReadMail()))

        def changerReadMail(self, *args):
            self.manager.transition.direction = 'right'
            self.manager.get_screen('read').ids.test.text = 'test' #This is a new line
            self.manager.current = 'read'

    class ReadMail(Screen, FloatLayout, MDApp):
        def __init__(self, **kwargs):
            super(ReadMail, self).__init__(**kwargs)
            label = (MDLabel())
            self.ids['test'] = weakref.ref(label) #This is a new line
            self.add_widget(label)
            self.add_widget(MDFillRoundFlatButton(text='Back', font_size='20sp', size_hint=(.1,.1), pos_hint='x':.01, 'y':.02, on_press=lambda x:self.changerInbox()))   

        def changerInbox(self, *args):
            self.manager.transition.direction = 'left'
            self.manager.current = 'home'

    class KivyApp(MDApp): 
        def build(self):
            Window.size = (1000, 600)
            self.sm = ScreenManager()
           
            self.sm.add_widget(HomeScreen(name='home'))
            self.sm.add_widget(ReadMail(name='read'))

            self.sm.current = 'home'
            return self.sm
        
    KivyApp().run()


if __name__ == '__main__':
    main()

【讨论】:

以上是关于如何在 python 文件中设置小部件的 ID?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 QGridLayout 中设置小部件的顺序

如何在 Kivy 中设置小部件/布局的最小允许宽度/高度?

如何在tkinter中设置小部件的大小?

使用属性窗口在 QT 网格布局中设置小部件的行和列

Wordpress:如何在菜单中放置小部件区域

Android/MIUI 在通知栏中设置小图标不起作用?显示的默认图标